Simple plotting tool implemented by Opencv and opencv plotting tool
The first time I wrote a blog, I was a newbie. I recently started learning Opencv and tried to write a simple drawing tool (currently I only wrote the line and eraser sections, and I still need to add other images to it ), it is also a way to record your learning path.
# Include "stdafx. h "# include" cv. h "# include" highgui. h "# define SHAPE_LINE 1 # define SHAPE_ERASER 2int g_style; CvRect rect; CvPoint g_StartPoint; CvPoint g_EndPoint; CvPoint p_Start; // records the CvPoint p_End at the top left of the mouse position; // record bool drawing = false at the bottom right of the mouse position; // whether bool erasering is in the drawing state; // whether it is in the erased state void callback (int event, int x, int y, int flags, void * param); void DrawLine (IplImage * img); // a simple drawing tool that implements the draw line and eraser functions, the drawing function also has the int main (int argc, char ** argv) {IplImage * img = cvCreateImage (cvSize (512,512), IPL_DEPTH_8U, 3); cvSet (img, cvScalar (255,255,255); IplImage * temp = cvCloneImage (img); cvCopy (img, temp); cvNamedWindow ("simple drawing tool"); cvSetMouseCallback ("simple drawing tool ", callback, img); printf ("type the operation you want to select: 'l' --" Draw line ", 'E' --" rubber "\ n "); char select = 'l'; while (1) {cvCopyImage (img, temp); // always draw results on the original image (bound to the mouse event ), first copy the image to temp, then draw the Drawing Process in the temporary graph temp (drawing the Drawing Process in the main function), and then use temp to display the image switch (select) {case 'l': g_style = SHAPE_LINE; break; case 'E': g_style = SHAPE_ERASER; break; case 27: return 0;} if (g_style = SHAPE_LINE & drawing) // draw the cvDrawLine (temp, g_StartPoint, g_EndPoint, cvScalar (, 0); if (g_style = SHAPE_ERASER) {cvRectangle (temp, p_Start, p_End, cvScalar (, 0); // draw an eraser rectangular border} cvShowImage ("simple drawing tool", temp); select = cvWaitKey (30 );} cvReleaseImage (& img); cvReleaseImage (& temp); cvDestroyWindow ("simple drawing tool"); return 0;} void callback (int event, int x, int y, int flags, void * param) {IplImage * img = (IplImage *) param; switch (event) {case CV_EVENT_LBUTTONDOWN: {if (SHAPE_LINE = g_style) {drawing = true; g_StartPoint = cvPoint (x, y); g_EndPoint = g_StartPoint; // set the end point coordinate to the same start point to avoid remembering the end point coordinate of the previous line} if (SHAPE_ERASER = g_style) {erasering = true; // note that you must modify the rect of the valid region of the ROI for the boundary issue. * = x-20; rect. y = y-20; // pay attention to coordinate calculation, generally the origin in the upper left corner of the window (this is related to operating system and other factors, IplImage structure has an origin attribute can set the origin of the image) rect. width = 40; rect. height = 40; p_Start.x = x-20; p_Start.y = y-20; p_End.x = x + 20; p_End.y = y + 20; if (x>-20 & x <532 & y>-20 & y <532) {cvSetImageROI (img, rect); cvSet (img, cvScalar (255,255,255); cvResetImageROI (img) ;}} break; case CV_EVENT_MOUSEMOVE: {p_Start.x = x-20; p_Start.y = y-20; // draw the rubber rectangle p_End.x = x + 20; p_End.y = y + 20; if (SHAPE_LINE = g_style) {if (drawing) {g_EndPoint = cvPoint (x, y) ;}} if (SHAPE_ERASER = g_style) {rect. * = x-20; rect. y = y-20; rect. width = 40; rect. height = 40; if (erasering) {if (x>-20 & x <532 & y>-20 & y <532) {cvSetImageROI (img, rect ); cvSet (img, cvScalar (255,255,255); cvResetImageROI (img) ;}}} break; case CV_EVENT_LBUTTONUP: {if (SHAPE_LINE = g_style) {drawing = false; cvDrawLine (img, g_StartPoint, g_EndPoint, cvScalar (255, 0); // (255, 0) the blue color is drawn here, that is, BGR} if (SHAPE_ERASER = g_style) {erasering = false ;}}}}