Basic Learning note-based opencv (5): enables you to select a rectangle frame with the mouse

Source: Internet
Author: User

It is often used to select rectangular boxes with the mouse in the preparation of opencv. It seems like a very simple logic in programming. If we don't know much about the rect in opencv, then the implementation effect is not very satisfactory. For example, although we use the mouse to choose from the top left to the bottom right, but occasionally, you can choose from the lower left to the upper right ......

After implementing this function, I found that the code to be written is cumbersome, and there are too many if statements. After reading the routine of opencv, I feel that its code efficiency is very high. The following is used for practice.

Environment: opencv2.3.1 + vs2010

Function: Enable the camera, capture the video image, and select the video area with the mouse (various selection habits are supported ).

The code and comments are as follows:

1 # include "stdafx. H "2 # include" opencv2/imgproc. HPP "3 # include <opencv2/highgui. HPP> 4 # include <opencv2/CORE/core. HPP> 5 # include <stdio. h> 6 # include <iostream> 7 8 using namespace CV; 9 Using namespace STD; 10 rect select; 11 bool select_flag = false; 12 point origin; 13 mat frame; 14 15 /************************************* **************************************** ******************* * **********************/16/***** if this onmouse () function is used, you can only draw from top left to bottom right, or the rectangular box from the bottom right to the top left is *****/17 /************************** **************************************** **************************************** * *************/18 // void onmouse (INT event, int X, int y, Int, void *) 19 // {20 // If (event = cv_event_lbuttondown) 21 // {22 // select. X = x; 23 // select. y = y; 24 // tracking = false; 25 //} 26 // else if (event = cv_event _ Lbuttonup) 27 // {28 // select. width = x-select.x; // the values calculated in the following two rows are either greater than 0 or less than 029 // select. height = y-select.y; 30 // tracking = true; // After the left button is complete, start tracking 31 //} 32 //} 33 34 /*************************** **************************************** **************************************** * ************/35/***** if this onmouse () is used () function, you can draw four situations of the rectangular box by dragging the mouse *****/36 /*********************** **************************************** ***** **************************************** * **********/37 void onmouse (INT event, int X, int y, Int, void *) 38 {39 // point origin; // cannot be defined here, because this is a message response-based function, after the execution, the origin is released, so the results cannot be met. 40 if (select_flag) 41 {42 select. X = min (origin. x, X); // you may not need to wait for the mouse to pop up before calculating the rectangle frame. Instead, you should press the mouse to start and then start to play up the time to calculate the selected rectangle box 43 select. y = min (origin. y, Y); 44 select. width = ABS (x-origin.x); // calculates the width and height of the rectangle 45 select. height = ABS (y-origin.y); 46 Select & = rect (, frame. cols, frame. rows); // ensure that the selected rectangle is within the video display area 47} 48 if (event = cv_event_lbuttondown) 49 {50 select_flag = true; // The Mark pressed by the mouse assigns the true value 51 origin = point (x, y); // save and click the captured point 52 select = rect (X, Y ); // Initialization is required here, The width and height are () because the points in the rect Rectangular Box class in opencv contain the vertex in the upper left corner, but do not include the vertex 53} 54 else if (event = cv_event_lbuttonup) in the lower right corner) 55 {56 select_flag = false; 57} 58} 59 60 int main (INT argc, unsigned char * argv []) 61 {62 char C; 63 64 // open the camera 65 videocapture cam (0); 66 If (! Cam. isopened () 67 return-1; 68 69 // create window 70 namedwindow ("camera", 1 ); // display the original video image window 71 72 // capture the mouse 73 setmousecallback ("camera", onmouse, 0); 74 75 while (1) 76 {77 // read an image 78 cam> frame; 79 if (frame. empty () 80 return-1; 81 82 // draw a Rectangular Box 83 rectangle (frame, select, scalar (255, 0, 0), 0 ); // display the trace 84 85 in a rectangle window in real time // display the video image to the window 86 imshow ("camera", frame); 87 88 // select. zeros (); 89 // keyboard response 90 c = (char) waitkey (20); 91 If (27 = C) // ESC key 92 return-1; 93} 94 95 return 0; 96}

It can be seen that the select_flag in the program is very clever.

 

 

 

 

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.