[OpenCV Getting Started Guide] 11th mouse drawing

Source: Internet
Author: User
Document directory
  • 1.1 cvSetMouseCallback
  • 1.2 CvMouseCallback
[OpenCV Getting Started Guide] 11th mouse drawing

In the previous section, we introduced the existing image processing methods, such as edge detection, contour detection, histogram, and histogram equalization. This article introduces more interactive content-drawing with the mouse. It is also very easy to draw the mouse in OpenCV, mainly using the cvSetMouseCallback function. For more information, see.

 

1. Key function 1.1 cvSetMouseCallback

Function: sets the callback function for processing mouse messages.

Function prototype:

/* Assign callback for mouse events */

CVAPI (void) cvSetMouseCallback (

Const char * window_name,

CvMouseCallback on_mouse,

Void * param CV_DEFAULT (NULL)

);

Function Description:

The first parameter indicates the window name.

The second parameter indicates the message processing function of the mouse message.

The third parameter indicates that the user defines the parameters for passing in the mouse to specify the message processing function.

 

1.2 CvMouseCallback

Function: callback function for mouse messages

Function prototype:

Typedef void (CV_CDECL * CvMouseCallback) (int event, int x, int y, int flags, void * param );

Function Description:

The first parameter indicates the type of the mouse message. The values are as follows:

Enum

{

CV_EVENT_MOUSEMOVE = 0,

CV_EVENT_LBUTTONDOWN = 1,

CV_EVENT_RBUTTONDOWN = 2,

CV_EVENT_MBUTTONDOWN = 3,

CV_EVENT_LBUTTONUP = 4,

CV_EVENT_RBUTTONUP = 5,

CV_EVENT_MBUTTONUP = 6,

CV_EVENT_LBUTTONDBLCLK = 7,

CV_EVENT_RBUTTONDBLCLK = 8,

CV_EVENT_MBUTTONDBLCLK = 9

};

Second, the three parameters represent the coordinates of the mouse.

The fourth parameter indicates an additional event. The value is as follows:

Enum

{

CV_EVENT_FLAG_LBUTTON = 1,

CV_EVENT_FLAG_RBUTTON = 2,

CV_EVENT_FLAG_MBUTTON = 4,

CV_EVENT_FLAG_CTRLKEY = 8,

CV_EVENT_FLAG_SHIFTKEY = 16,

CV_EVENT_FLAG_ALTKEY = 32

};

The fifth parameter is the parameter that will be received in cvSetMouseCallback.

 

Ii. Sample Code

The code example is as follows. Press 'R' to redraw, and press 's' To save the image to the disk.

// Mouse drawing // MoreWindows (http://blog.csdn.net/MoreWindows) # include <opencv2/opencv. hpp> using namespace std; # pragma comment (linker, "/subsystem: \" windows \ "/entry: \" mainCRTStartup \"") const char * pstrWindowsMouseDrawTitle = "mouse drawing (http://blog.csdn.net/MoreWindows)"; // callback function of the mouse message void on_mouse (int event, int x, int y, int flags, void * param) {static bool s_bMouseLButtonDown = false; static CvPoint s_cvPreP Oint = cvPoint (0, 0); switch (event) {case CV_EVENT_LBUTTONDOWN: s_bMouseLButtonDown = true; s_cvPrePoint = cvPoint (x, y); break; case when: s_bMouseLButtonDown = false; break; case CV_EVENT_MOUSEMOVE: if (s_bMouseLButtonDown) {CvPoint cvCurrPoint = cvPoint (x, y); cvLine (IplImage *) param, s_cvPrePoint, cvCurrPoint, CV_RGB (0, 0, 20), 3); s_cvPrePoint = cvCurrPoint; cvShowImage (pstrWindowsMouseDrawT Itle, (IplImage *) param);} break ;}int main () {const int MAX_WIDTH = 500, MAX_HEIGHT = 400; const char * pstrSaveImageName = "MouseDraw.jpg "; iplImage * pSrcImage = cvCreateImage (cvSize (MAX_WIDTH, MAX_HEIGHT), IPL_DEPTH_8U, 3); cvSet (pSrcImage, CV_RGB (255,255,255); // you can use cvSet () fill the image with white cvNamedWindow (pstrWindowsMouseDrawTitle, CV_WINDOW_AUTOSIZE), cvShowImage (pstrWindowsMouseDrawTitle, pSrcImage), and cvSetMou SeCallback (pstrWindowsMouseDrawTitle, on_mouse, (void *) pSrcImage); int c; do {c = cvWaitKey (0); switch (char) c) {case 'r ': cvSet (pSrcImage, CV_RGB (255,255,255); cvShowImage (pstrWindowsMouseDrawTitle, pSrcImage); break; case's ': cvSaveImage (pstrSaveImageName, pSrcImage); break ;}} while (c> 0 & c! = 27); cvDestroyWindow (pstrWindowsMouseDrawTitle); cvReleaseImage (& pSrcImage); return 0 ;}

Shows the running effect:

 

You can also draw complex pictures.

Bottle flower:

ROSE:

Mickey Mouse:

 

Haha, because the program can only draw black and white images, it is much better to support the color paint brush to estimate the drawing effect.

 

What should we do if we want to write our own OpenCV and share it with others? See [OpenCV Getting Started Guide] Article 12th sharing OpenCV Program

 

 

 

OpenCV Getting Started Guide series Article address: http://blog.csdn.net/morewindows/article/category/863841

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8426283

Welcome to Weibo: http://weibo.com/MoreWindows

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.