Learning opencv -- initialize the first frame of the video.

Source: Internet
Author: User

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?
  1. # Include "opencv2/opencv. HPP"
  2. # Include "opencv2/highgui. HPP"
  3. # Include <string>
  4. # Include <iostream>
  5. # Include <stdio. h>
  6. Using namespace CV;
  7. Using namespace STD;
  8. // You can trace up to one object;
  9. # Define max_objects 1
  10. // Class for passing Parameters
  11. Class Params
  12. {
  13. Public:
  14. Point loc1, loc2;
  15. String win_name;
  16. Mat SRC;
  17. Mat cur;
  18. Int N; // number of record objects
  19. Params (): n (0 ){}
  20. //~ Params ();
  21. };
  22. // Mouse event
  23. Void mouseevent (INT event, int X, int y, int flags, void * PARAM)
  24. {
  25. Static bool check_line_state = false;
  26. Mat TMP;
  27. Params * P = (Params *) Param;
  28. // Press the left mouse button to store the initial position;
  29. If (event = cv_event_lbuttondown)
  30. {
  31. If (p-> N = max_objects) // you can check whether a trail object exists;
  32. {
  33. Cout <"fail! \ N only can tracking one object! "<Endl;
  34. Return;
  35. }
  36. P-> loc1.x = X;
  37. P-> loc1.y = y;
  38. Check_line_state = true;
  39. }
  40. // Press the left mouse button and move the mouse to draw a rectangle;
  41. Else if (check_line_state & event = cv_event_mousemove)
  42. {
  43. If (p-> N = max_objects) // you can check whether a trail object exists;
  44. {
  45. Cout <"fail! \ N only can tracking one object! "<Endl;
  46. Return;
  47. }
  48. P-> SRC. copyto (TMP );
  49. Rectangle (TMP, p-> loc1, point (x, y), cv_rgb (255, 0), 2, 8 );
  50. Imshow (p-> win_name, TMP );
  51. }
  52. // The left button pops up and the frame ends;
  53. Else if (event = cv_event_lbuttonup)
  54. {
  55. If (p-> N = max_objects) // you can check whether a trail object exists;
  56. {
  57. Cout <"fail! \ N only can tracking one object! "<Endl;
  58. Return;
  59. }
  60. P-> loc2.x = X;
  61. P-> loc2.y = y;
  62. Rectangle (p-> SRC, p-> loc1, point (x, y), cv_rgb (255, 0, 0), 2, 8 );
  63. Imshow (p-> win_name, p-> SRC );
  64. P-> N ++;
  65. Check_line_state = false;
  66. }
  67. }
  68. // Initialize the first frame and draw the object to be tracked
  69. Void initialvideo (MAT & SRC, rect & rect)
  70. {
  71. Params P;
  72. SRC. copyto (P. SRC );
  73. P. win_name = "initial window ";
  74. Imshow (P. win_name, Src );
  75. // Mouse recall event
  76. Setmousecallback (P. win_name, & mouseevent, & P );
  77. Cout <"Draw rect & press any key to end" <Endl;
  78. Waitkey ();
  79. // Assign the obtained position to the rect
  80. Rect. x = min (P. loc1.x, P. loc2.x );
  81. Rect. Y = min (P. loc1.y, P. loc2.y );
  82. Rect. width = ABS (P. loc1.x-P. loc2.x );
  83. Rect. Height = ABS (P. loc1.y-P. loc2.y );
  84. Destroywindow ("initial window ");
  85. }
  86. Int main ()
  87. {
  88. // Input from default camera
  89. Videocapture cap (0 );
  90. // Input from the video
  91. // String filename = "D:/soccer. Avi ";
  92. // Videocapture cap (filename );
  93. If (! Cap. isopened () // check if we succeeded
  94. {
  95. Cout <"couldn't open video file" <Endl;
  96. Return-1;
  97. }
  98. Mat frame;
  99. Bool isfirstframe = true;
  100. Rect;
  101. Cap> frame; // the first frame cannot be displayed due to a computer problem.
  102. For (;;)
  103. {
  104. Cap> frame;
  105. // Initialize the first frame
  106. If (isfirstframe)
  107. {
  108. Initialvideo (frame, rect );
  109. Isfirstframe = false;
  110. }
  111. // Other operations
  112. Rectangle (frame, rect, cv_rgb (255, 0, 0), 2, 8 );
  113. Imshow ("showsrc", frame );
  114. Char key = (char) waitkey (5 );
  115. Switch (key)
  116. {
  117. Case 27:
  118. Return 0;
  119. Case '':
  120. Cout <"Save process" <Endl;
  121. Break;
  122. Default:
  123. Break;
  124. }
  125. }
  126. }
# 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 ;}}}

 

 

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.