OpenCV Mouse and key values using the

Source: Internet
Author: User

Introduction
This chapter is to learn the keyboard and mouse OpenCV on the corresponding manipulation and examples.

Keyboard response
#include <math.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <opencv2/core/core.  hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv/cv.h> int main (int agrc, char* argv[]) {int    C    int exit = 0;     Cv::mat Src=cv::imread (argv[1],1);    Cv::imshow ("n", SRC);        while (!exit) {c = cv::waitkey (0);                Switch (c) {case ' W ': printf ("w \ n");            Break                Case ' a ': printf ("a \ n");            Break                Case ' s ': printf ("s \ n");              Break                Case ' d ': printf ("D \ n");            Break                Case ' Q ': printf ("exit\n");                Exit = 1;            Break        default:printf ("c:%d\n", c);     }} return 0;                                                                                                                   } 

As the code shows, the main is the parameters in the Waitkey, less than or equal to 0 means waiting for the key value, greater than 0 of the number of milliseconds to wait, time-out to return 1, otherwise the corresponding key value is returned.

Mouse Response
#include <math.h>                                                                                                                    #include <string.h> #include <stdio.h> #include <stdlib.h> #include < opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv/cv.h> void On_mouse ( int event, int x, int y, int flags, void* USTC)  {      char *temp = (char *) USTC;       printf ("temp:%s, event:%d, flags:%d\n", temp, event, flags);}   int main (int agrc, char* argv[]) {    char str[4] = "Tao";     Cv::mat Src=cv::imread (argv[1],1);      Cv::imshow ("src", src);     Cvsetmousecallback ("src", on_mouse, str);    Cv::waitkey (0);    Cv::waitkey (0);                                                                                                                      Cvdestroyallwindows ();       

 The corresponding code, as shown above, through the function Cvsetmousecallback, the corresponding window that binds the mouse event is:" SRC ", the response function is: On_mouse, and the parameter pointer that can be passed to the response function: Str.  If no parameters need to be passed in, it can be directly set to 0 or null.  Then look at its response function On_mouse, first look at the function of several parameters, first of all: The event is used to describe the mouse click events. The following variables are defined in highgui/highgui_c.h: 
enum{    cv_event_mousemove      =0,        //mouse movement    cv_event_lbuttondown    =1,        //left mouse button pressed    cv_event_ Rbuttondown    =2,        //right mouse button press    cv_event_mbuttondown    =3,        //middle mouse button press    cv_event_lbuttonup      =4,        //left mouse button bouncing up    cv_event_rbuttonup      =5,        //right mouse button bouncing    cv_event_mbuttonup      =6,        // The middle mouse button bounces up    cv_event_lbuttondblclk  =7,        //left mouse button double-click    cv_event_rbuttondblclk  =8,//        right mouse button double-click    cv_event_mbuttondblclk  =9         //middle mouse button double-click};
These variables are triggered by the mouse event, the corresponding event according to the different actions of the mouse to return the corresponding value, the corresponding meaning of each value as described above.
Then: x, y corresponds to the current mouse coordinates.  Next: Flag describes the mouse drag-and-drop event. The following variables are defined in HIGHGUI/HIGHGUI_C.H:
enum{    Cv_event_flag_lbutton   =1,     //left mouse button to drag (press the mouse button, and then drag the mouse in the window)    Cv_event_flag_rbutton   =2,     right mouse button Drag    cv_event_flag_mbutton   =4,     //mouse button drag    cv_event_flag_ctrlkey   =8,     //press the key value Ctrl not put    cv_event_flag_shiftkey  =16,    //press the key value shift not to put    Cv_event_flag_altkey    =32     //Press the key value Alt not put} ;
Flag corresponding to return response parameters such as above, need to pay attention to some combination: for example, press the key value CTRL while pressing the left mouse button drag, flag becomes: 9=1+8.  If you press CTRL + the right mouse button becomes: 10=8+2. The last void* type USTC, that is, corresponds to the previous STR, I passed the STR type char*, then ustc back to char*, if it is passed to 0 or null, there is no need to process, if it is another type of pointer, it is converted to the corresponding type.

Working with Instances
Here is an example of a program: Draw a rectangle on the image according to the mouse drag and drop, and then press the key value ' C ', the corresponding rectangle is drawn to the position of the new image display; press the ' Q ' key to exit the program.
#include <math.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include < opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv/cv.h> using namespace Cv Cv::mat Src;char Pic_name[20];int pic_info[4];   void On_mouse (int event, int x, int y, int flags, void* USTC) {if (event = = Cv_event_lbuttondown) {pic_info[0] = x;   /*width1*/pic_info[1] = y;   /*HEIGHT1*/PIC_INFO[2] = 0;   /*WIDTH2*/PIC_INFO[3] = 0;  /*height2*/}else if (event = = Cv_event_lbuttonup) {pic_info[2] = x;pic_info[3] = Y;src=cv::imread (pic_name,1); Cv::rectangle (Src,cvpoint (pic_info[0], pic_info[1]), Cvpoint (Pic_info[2], pic_info[3]), Cvscalar (255,0,0), 2); CV:: Imshow ("src", src);}  if (flags = = Cv_event_flag_lbutton) {pic_info[2] = x;pic_info[3] = Y;src=cv::imread (pic_name,1); Cv::rectangle (Src,cvpoint (pic_info[0], pic_info[1]), Cvpoint (Pic_info[2], pic_info[3]), Cvscalar (255,0,0), 2); CV:: Imshow ("src", src);} printf ("%d%d%d\n", pic_info[0], pic_info[1], pIC_INFO[2], pic_info[3]);} int main (int agrc, char* argv[]) {bool exit = False;char C;cv::mat imageroi; memcpy (pic_name,argv[1],sizeof (argv[1])); SRC  =cv::imread (pic_name,1); Cv::imshow ("src", SRC); Cvsetmousecallback ("src", On_mouse, NULL), while (!exit) {c = cv::waitkey (0); if (c = = ' C ') {if ((pic_info[0]! = 0) && (pic_info[1]! = 0) && (pic_info[2]! = 0) && (pic_info[3]! = 0)) {imageroi = src (cv::rect (pic_info[0), pic_i NFO[1], pic_info[2]-pic_info[0], pic_info[3]-pic_info[1]); Cv::imshow ("ROI", Imageroi);}} else if (c = = ' Q ') {exit = true;}}   Cvdestroyallwindows ();  return 0;   }


Effect Demo
The corresponding effect shows as follows:
                                

OpenCV Mouse and key values using the

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.