This article from http://blog.csdn.net/sangni007/article/details/8112486
In many video tracking or segmentation, you always need to initialize the first frame, that is, to draw a frame on the first frame, or to mark the foreground and background. Today, we will initialize the first frame, I drew a box of code on the first frame to implement it. By the way, I will review the mouse recall event in opencv and paste the code here to share it with you. It will also facilitate future search.
[CPP]
View plaincopyprint?
- # Include "opencv2/opencv. HPP"
- # Include "opencv2/highgui. HPP"
- # Include <string>
- # Include <iostream>
- # Include <stdio. h>
- Using namespace CV;
- Using namespace STD;
- // You can trace up to one object;
- # Define max_objects 1
- // Class for passing Parameters
- Class Params
- {
- Public:
- Point loc1, loc2;
- String win_name;
- Mat SRC;
- Mat cur;
- Int N; // number of record objects
- Params (): n (0 ){}
- //~ Params ();
- };
- // Mouse event
- Void mouseevent (INT event, int X, int y, int flags, void * PARAM)
- {
- Static bool check_line_state = false;
- Mat TMP;
- Params * P = (Params *) Param;
- // Press the left mouse button to store the initial position;
- If (event = cv_event_lbuttondown)
- {
- If (p-> N = max_objects) // you can check whether a trail object exists;
- {
- Cout <"fail! \ N only can tracking one object! "<Endl;
- Return;
- }
- P-> loc1.x = X;
- P-> loc1.y = y;
- Check_line_state = true;
- }
- // Press the left mouse button and move the mouse to draw a rectangle;
- Else if (check_line_state & event = cv_event_mousemove)
- {
- If (p-> N = max_objects) // you can check whether a trail object exists;
- {
- Cout <"fail! \ N only can tracking one object! "<Endl;
- Return;
- }
- P-> SRC. copyto (TMP );
- Rectangle (TMP, p-> loc1, point (x, y), cv_rgb (255, 0), 2, 8 );
- Imshow (p-> win_name, TMP );
- }
- // The left button pops up and the frame ends;
- Else if (event = cv_event_lbuttonup)
- {
- If (p-> N = max_objects) // you can check whether a trail object exists;
- {
- Cout <"fail! \ N only can tracking one object! "<Endl;
- Return;
- }
- P-> loc2.x = X;
- P-> loc2.y = y;
- Rectangle (p-> SRC, p-> loc1, point (x, y), cv_rgb (255, 0, 0), 2, 8 );
- Imshow (p-> win_name, p-> SRC );
- P-> N ++;
- Check_line_state = false;
- }
- }
- // Initialize the first frame and draw the object to be tracked
- Void initialvideo (MAT & SRC, rect & rect)
- {
- Params P;
- SRC. copyto (P. SRC );
- P. win_name = "initial window ";
- Imshow (P. win_name, Src );
- // Mouse recall event
- Setmousecallback (P. win_name, & mouseevent, & P );
- Cout <"Draw rect & press any key to end" <Endl;
- Waitkey ();
- // Assign the obtained position to the rect
- Rect. x = min (P. loc1.x, P. loc2.x );
- Rect. Y = min (P. loc1.y, P. loc2.y );
- Rect. width = ABS (P. loc1.x-P. loc2.x );
- Rect. Height = ABS (P. loc1.y-P. loc2.y );
- Destroywindow ("initial window ");
- }
- Int main ()
- {
- // Input from default camera
- Videocapture cap (0 );
- // Input from the video
- // String filename = "D:/soccer. Avi ";
- // Videocapture cap (filename );
- If (! Cap. isopened () // check if we succeeded
- {
- Cout <"couldn't open video file" <Endl;
- Return-1;
- }
- Mat frame;
- Bool isfirstframe = true;
- Rect;
- Cap> frame; // the first frame cannot be displayed due to a computer problem.
- For (;;)
- {
- Cap> frame;
- // Initialize the first frame
- If (isfirstframe)
- {
- Initialvideo (frame, rect );
- Isfirstframe = false;
- }
- // Other operations
- Rectangle (frame, rect, cv_rgb (255, 0, 0), 2, 8 );
- Imshow ("showsrc", frame );
- Char key = (char) waitkey (5 );
- Switch (key)
- {
- Case 27:
- Return 0;
- Case '':
- Cout <"Save process" <Endl;
- Break;
- Default:
- Break;
- }
- }
- }
# Include "opencv2/opencv. HPP "# include" opencv2/highgui. HPP "# include <string> # include <iostream> # include <stdio. h> using namespace CV; using namespace STD; // trace up to one object; # define max_objects 1 // pass the classclass Params parameter {public: Point loc1, loc2; string win_name; mat src; MAT cur; int N; // number of record objects Params (): n (0 ){}//~ Params () ;}; // void mouseevent (INT event, int X, int y, int flags, void * PARAM) {static bool check_line_state = false; mat tmp; params * P = (Params *) Param; // press the left mouse button to store the initial position. If (event = cv_event_lbuttondown) {If (p-> N = max_objects) // determine whether a trail object exists; {cout <"fail! \ N only can tracking one object! "<Endl; return;} p-> loc1.x = x; P-> loc1.y = y; check_line_state = true;} // click the left mouse button and move the mouse to draw a rectangle; else if (check_line_state & event = cv_event_mousemove) {If (p-> N = max_objects) // you can check whether a trail object exists. {cout <"fail! \ N only can tracking one object! "<Endl; return;} p-> SRC. copyto (TMP); rectangle (TMP, p-> loc1, point (x, y), cv_rgb (255, 0, 0), 2, 8); imshow (p-> win_name, TMP);} // The left button pops up and the frame ends. Else if (event = cv_event_lbuttonup) {If (p-> N = max_objects) // determine whether a trail object exists; {cout <"fail! \ N only can tracking one object! "<Endl; return;} p-> loc2.x = x; P-> loc2.y = y; rectangle (p-> SRC, p-> loc1, point (X, y), cv_rgb (255, 0), 2, 8); imshow (p-> win_name, p-> SRC); P-> N ++; check_line_state = false ;}// initialize the first frame and draw the void initialvideo (MAT & SRC, rect & rect) {Params P; SRC. copyto (P. SRC); p. win_name = "initial window"; imshow (P. win_name, Src); // The mouse recall event setmousecallback (P. win_name, & mouseevent, & P); cout <"Draw rect & press any key to end "<Endl; waitkey (); // assign the obtained position to rectrect. X = min (P. loc1.x, P. loc2.x); rect. y = min (P. loc1.y, P. loc2.y); rect. width = ABS (P. loc1.x-P. loc2.x); rect. height = ABS (P. loc1.y-P. loc2.y); destroywindow ("initial window");} int main () {// input videocapture cap (0) from the default camera ); // input from the video // string filename = "D:/soccer. avi "; // videocapture cap (filename); If (! Cap. isopened () // check if we succeeded {cout <"couldn't open video file" <Endl; Return-1 ;}mat frame; bool isfirstframe = true; rect; Cap> frame; // It may be caused by a computer problem that the first frame cannot be displayed. Therefore, the first frame is read for (;) first (;;) {cap> frame; // initialize the first frame if (isfirstframe) {initialvideo (frame, rect); isfirstframe = false;} // other operations rectangle (frame, rect, cv_rgb (255, 0), 2, 8); imshow ("showsrc", frame); char key = (char) waitkey (5); Switch (key) {Case 27: return 0; Case '': cout <" Save process "<Endl; break; default: Break ;}}}