To Crop by Mouse Pointer Void Mousehandler (Int Event, Int X, Int Y, Int, Void ) (
To Crop by Mouse Pointer Void Mousehandler (Int Event, Int X, Int Y, Int, Void ) (
To Crop by Mouse Pointer Void Mousehandler (Int Event, Int X, Int Y, Int, Void ) (
imshow("Source", img1);
if (event == EVENT_RBUTTONUP)
{
flag = var;
final = Mat::zeros(img0.size(), CV_8UC3);
}
if (event == EVENT_MBUTTONDOWN)
{
for (int i = 0; i < numpts; i++)
{
pts[i].x = 0;
pts[i].y = 0;
}
var = 0;
flag1 = 0;
minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
imshow("Source", img0);
drag = 0;
}
}
int main() {
Mat src = imread("abc.jpg");
minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
img0 = src;
channel = img0.channels();
res1 = Mat::zeros(img0.size(), CV_8UC1);
final = Mat::zeros(img0.size(), CV_8UC3);
//////////// source image ///////////////////
namedWindow("Source", 1);
setMouseCallback("Source", mouseHandler, NULL);
imshow("Source", img0);
imshow("mask", res1);
waitKey(0);
img0.release();
img1.release();
DRAWING POLYGONS
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
vector<vector<Point>> polygons;
bool bDraw;
vector<Point> poly;
Mat3b img;
Mat3b layer;
Mat3b working;
void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
if (event == EVENT_LBUTTONDOWN)
{
//cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")"
<< endl;
// Refresh
working = layer.clone();
if (!bDraw)
{
// Init your polygon
poly.clear();
bDraw = true;
}
// Add Current Point
poly.push_back(Point(x, y));
// Draw Poly
for (size_t i = 1; i < poly.size(); ++i) {
line(working, poly[i - 1], poly[i], Scalar(0, 255, 0));
}
// Draw Points
for (size_t i = 0; i < poly.size(); ++i) {
circle(working, poly[i], 3, Scalar(0, 0, 255));
}
// Update
imshow("My Window", working);
}
else if (event == EVENT_MOUSEMOVE)
{
//cout << "Mouse move over the window - position (" << x << ", " << y << ")" <<
endl;
// If drawing, update rect width and height
if (!bDraw) return;
// Refresh
working = layer.clone();
// Draw Poly
for (size_t i = 1; i < poly.size(); ++i) {
line(working, poly[i - 1], poly[i], Scalar(0, 255, 0));
}
// Draw Points
for (size_t i = 0; i < poly.size(); ++i) {
circle(working, poly[i], 3, Scalar(0, 0, 255));
}
// Draw Current line
line(working, poly.back(), Point(x, y), Scalar(0, 255, 0));
// Update
imshow("My Window", working);
}
else if (event == EVENT_LBUTTONDBLCLK)
{
//cout << "Left button double clicked" << endl;
// Refresh
working = layer.clone();
// Add Current Point
poly.push_back(Point(x, y));
// Save poly, draw it on layer
polygons.push_back(poly);
// Draw Poly
for (size_t i = 1; i < poly.size(); ++i)
{
line(working, poly[i - 1], poly[i], Scalar(0, 255, 255));
}
// Draw closed poly
line(working, poly.back(), poly.front(), Scalar(0, 255, 255));
// Draw Points
for (size_t i = 0; i < poly.size(); ++i) {
circle(working, poly[i], 3, Scalar(0, 0, 255));
}
layer = working.clone();
bDraw = false;
// Update
imshow("My Window", working);
}
img = imread("path_to_image");
// initialize your temp images
layer = img.clone();
working = img.clone();
//if fail to read the image
if (img.empty())
{
cout << "Error loading the image" << endl;
return -1;
}
//Create a window
namedWindow("My Window", 1);
//set the callback function for any mouse event
setMouseCallback("My Window", CallBackFunc, NULL);
//show the image
imshow("My Window", working);
// Wait until user press 'q'
while ((waitKey(1) & 0xFF) != 'q');
// Create cropped images and show / save
for (size_t i = 0; i < polygons.size(); ++i)
{
Mat3b out(img.rows, img.cols, Vec3b(0, 0, 0));
Mat1b mask(img.rows, img.cols, uchar(0));
drawContours(mask, polygons, i, Scalar(255), CV_FILLED);
img.copyTo(out, mask);
Rect box = boundingRect(polygons[i]);
out = out(box).clone();
imshow("Crop #" + to_string(i), out);
waitKey(1);
//imwrite("Crop #" + to_string(i), out);
}
waitKey();
return 0;
}
import cv2
import numpy as np
im = cv2.imread('images/img5.jpg')
gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,0)
_,contours,_ = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
areas = [cv2.contourArea(c) for c in contours]
max_index = np.argmax(areas)
cnt=contours[max_index]
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow("Show",im)
cv2.imwrite("images/img5_rect.jpg", im)
cv2.waitKey(0)
import numpy as np
import cv2
im = cv2.imread('1.jpg')
im3 = im.copy()
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)
contours,hierarchy =
cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
squares = []
for cnt in contours:
if cv2.contourArea(cnt)>50:
[x,y,w,h] = cv2.boundingRect(cnt)
if h>28 and h<35:
cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),2)
cv2.imwrite('norm1.jpg',im)
crop_img = [[[255, 255, 255] for x in xrange(377)] for x in xrange(377) ] #newly added
code starts here
for s in squares:
s = squares[0]
x = s[0]
y = s[1]
w = s[2]
h = s[3]
img = im[y:y+h,x:x+w]
for col in range(y,y+h):
for row in range(x,x+w):
if img[col - y][row - x].tolist() == [0,0,0]:
crop_img[col][row] = [0,0,0]
cv2.imwrite("cropped.jpg", np.array(crop_img))