//including libraries #include#include #include using namespace std; using namespace cv; Mat img; //Advice:Refer 'main()' function first. //create boolean variables bool lup=false, ldown=false; //opencv allows to create 'Point' variables, which stores the x,y coordinates //think of the point as an Object and coordinates as its Property. Point corner1, corner2; Rect box; //self-explanatory static void mouse_callback(int event, int x,int y, int, void *){ if(event==EVENT_LBUTTONDOWN){ ldown=true; corner1.x=x; corner1.y=y; } if(event==EVENT_LBUTTONUP){ lup=true; corner2.x=x; corner2.y=y; } if(ldown==true && lup==false){ Point pt; pt.x=x; pt.y=y; //clone the original image Mat local_img=img.clone(); rectangle(local_img, corner1,pt, Scalar(0,0,255)); imshow("Image",local_img); } if(ldown==true && lup==true){ box.width = abs(corner1.x - corner2.x); box.height = abs(corner1.y - corner2.y); box.x = min(corner1.x, corner2.x); box.y = min(corner1.y, corner2.y); //the 'crop' function crops the image 'img' in the dimensions of 'box' Mat crop(img,box); namedWindow("Crop"); imshow("Crop",crop); ldown=false; lup=false; } } int main() { //read an image in Mat variable 'img' img=imread("images/image.jpg"); namedWindow("Image"); imshow("Image",img); //setMouseCallback is a function which, in simple language, records the mouse events, namely, // clicks and position of clicks. setMouseCallback("Image",mouse_callback); while(char(waitKey(0))!='q'){ } return 0; }
Sunday, 9 March 2014
#1. Crop an Image
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment