The Division of area of interest, in the video processing has an important application, with OPENCV introduction of two, in the video annotated area of interest method:
Original video:
--------------------------------------------------------------------------------------------------------------- --------------------
First: Pause the video or draw an area of interest in the first frame of the video stream
#include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\ Imgproc.hpp>using namespace CV; #include <iostream> #include <vector>using namespace std;/*---- Define mouse events-Draw rectangular areas: function as two headlights----*///first step: global variable bool Drawing_box = False;bool Gotbox = false; Rect box; Point downpoint;/*void mouserecthandler (int event, int x, int y, int. flags, void *param) {switch (event) {case cv_event_mous Emove:if (drawing_box) {box.width = X-box.x;box.height = y-box.y;} Break;case Cv_event_lbuttondown:drawing_box = True;box = Rect (x, y, 0, 0); Break;case cv_event_lbuttonup:drawing_box = Fal Se;gotbox = true;if (Box.width < 0) {box.x + = Box.width;box.width *=-1;} if (Box.height < 0) {box.y + = Box.height;box.height *=-1;} Break;default:break;}} */void Mouserecthandler (int event, int x, int y, int flags, void *param) {switch (event) {Case Cv_event_mousemove:if (Drawin G_box) {//The mouse moves to the lower-right corner of Downpoint if (x >=downpoint.x && y >= downpoint.y) {box.x = Downpoint.x;box.y = Downpoint.y;box.width = X-downpoint.x;box.height = Y-downpoint.y;} Move the mouse to the upper-right corner of the Downpoint if (x >= downpoint.x && y <= downpoint.y) {box.x = downpoint.x;box.y = Y;box.width = X -downpoint.x;box.height = downpoint.y-y;} Move the mouse to the upper-left corner of the Downpoint if (x <= downpoint.x && y <= downpoint.y) {box.x = x;box.y = Y;box.width = Downpoint.x- X;box.height = downpoint.y-y;} Move the mouse to the lower-left corner of the Downpoint if (x <= downpoint.x && y >= downpoint.y) {box.x = X;BOX.Y = Downpoint.y;box.width = Dow Npoint.x-x;box.height = Y-downpoint.y;}} Break;case cv_event_lbuttondown://Press the mouse to indicate that the rectangle can be started Drawing_box = true;//record Start Downpoint = Point (x, y); Break;case cv_ event_lbuttonup://release the mouse, representing the end of the draw rectangle Drawing_box = False;gotbox = True;break;default:break;}} int main (int argc,char*argv[]) {//Read videocapture video (argv[1]); Determine if the video is turned on if (!video.isopened ()) return 0; The first frame of the video is Mat firstframe; Mat frame; Read videoThe first frame of video>>frame; Copy to Firstframe Frame.copyto (firstframe); Register Namedwindow ("video", 1); Setmousecallback ("video", Mouserecthandler,null); Draw the area of interest while (!gotbox) {Firstframe.copyto (frame); Rectangle (Frame,box,scalar (255,0,0), 2);//Draw an area of interest imshow ("video", frame); if (waitkey () = = ' Q ')//---------very important break; }//remove callback Setmousecallback ("video", Null,null); Video continues for (;;) {//Read video video>>frame; Determines if there is a current frame if (!frame.data) break; Draw the area of interest rectangle (Frame,box,scalar (255,255,0), 2); Imshow ("video", frame); if (waitkey) = = ' Q ') break; } return 0; }
"Results display":
Second case: Without affecting the video playback, draw the area of interest
#include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\ Imgproc.hpp>using namespace CV; #include <iostream> #include <vector>using namespace std;/*---- Define mouse events-Draw rectangular areas: function as two headlights----*///first step: global variable bool Drawing_box = False;bool Gotbox = false; Rect box; Point downpoint;/*void mouserecthandler (int event, int x, int y, int. flags, void *param) {switch (event) {case cv_event_mous Emove:if (drawing_box) {box.width = X-box.x;box.height = y-box.y;} Break;case Cv_event_lbuttondown:drawing_box = True;box = Rect (x, y, 0, 0); Break;case cv_event_lbuttonup:drawing_box = Fal Se;gotbox = true;if (Box.width < 0) {box.x + = Box.width;box.width *=-1;} if (Box.height < 0) {box.y + = Box.height;box.height *=-1;} Break;default:break;}} */void Mouserecthandler (int event, int x, int y, int flags, void *param) {switch (event) {Case Cv_event_mousemove:if (Drawin G_box) {//The mouse moves to the lower-right corner of Downpoint if (x >=downpoint.x && y >= downpoint.y) {box.x = Downpoint.x;box.y = Downpoint.y;box.width = X-downpoint.x;box.height = Y-downpoint.y;} Move the mouse to the upper-right corner of the Downpoint if (x >= downpoint.x && y <= downpoint.y) {box.x = downpoint.x;box.y = Y;box.width = X -downpoint.x;box.height = downpoint.y-y;} Move the mouse to the upper-left corner of the Downpoint if (x <= downpoint.x && y <= downpoint.y) {box.x = x;box.y = Y;box.width = Downpoint.x- X;box.height = downpoint.y-y;} Move the mouse to the lower-left corner of the Downpoint if (x <= downpoint.x && y >= downpoint.y) {box.x = X;BOX.Y = Downpoint.y;box.width = Dow Npoint.x-x;box.height = Y-downpoint.y;}} Break;case cv_event_lbuttondown://Press the mouse to indicate that the rectangle can be started Drawing_box = true;//record Start Downpoint = Point (x, y); Break;case cv_ event_lbuttonup://release the mouse, representing the end of the draw rectangle Drawing_box = False;gotbox = True;break;default:break;}} int main (int argc,char*argv[]) {//Read videocapture video (argv[1]); Determine if the video is turned on if (!video.isopened ()) return 0; Video frame Mat frame; Register Namedwindow ("video", 1); Setmousecallback ("video", Mouserecthandler,null); Draw an area of interest for (;;) {//Read the first frame of the video video>>frame;if (!frame.data) break; Rectangle (Frame,box,scalar (255,255,0), 2);//Draw an area of interest Imshow ("video", frame); if (gotbox) break; if (waitkey () = = ' Q ')//---------very important break; }//remove callback Setmousecallback ("video", Null,null); Video continues for (;;) {//Read video video>>frame; Determines if there is a current frame if (!frame.data) break; Draw the area of interest rectangle (Frame,box,scalar (0,255,0), 2); Imshow ("video", frame); if (waitkey) = = ' Q ') break; } return 0; }
Results
As for the difference between the two, try it out!
opencv-Video Processing-draw area of interest (ROI)