tutorial_introduction_to_tracker.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <opencv2/core/utility.hpp>
  2. #include <opencv2/imgproc.hpp>
  3. #include <opencv2/tracking.hpp>
  4. #include <opencv2/videoio.hpp>
  5. #include <opencv2/highgui.hpp>
  6. #include <iostream>
  7. #include <cstring>
  8. using namespace std;
  9. using namespace cv;
  10. int main( int argc, char** argv ){
  11. // show help
  12. //! [help]
  13. if(argc<2){
  14. cout<<
  15. " Usage: tracker <video_name>\n"
  16. " examples:\n"
  17. " example_tracking_kcf Bolt/img/%04d.jpg\n"
  18. " example_tracking_kcf faceocc2.webm\n"
  19. << endl;
  20. return 0;
  21. }
  22. //! [help]
  23. // declares all required variables
  24. //! [vars]
  25. Rect roi;
  26. Mat frame;
  27. //! [vars]
  28. // create a tracker object
  29. //! [create]
  30. Ptr<Tracker> tracker = TrackerKCF::create();
  31. //! [create]
  32. // set input video
  33. //! [setvideo]
  34. std::string video = argv[1];
  35. VideoCapture cap(video);
  36. //! [setvideo]
  37. // get bounding box
  38. //! [getframe]
  39. cap >> frame;
  40. //! [getframe]
  41. //! [selectroi]
  42. roi=selectROI("tracker",frame);
  43. //! [selectroi]
  44. //quit if ROI was not selected
  45. if(roi.width==0 || roi.height==0)
  46. return 0;
  47. // initialize the tracker
  48. //! [init]
  49. tracker->init(frame,roi);
  50. //! [init]
  51. // perform the tracking process
  52. printf("Start the tracking process, press ESC to quit.\n");
  53. for ( ;; ){
  54. // get frame from the video
  55. cap >> frame;
  56. // stop the program if no more images
  57. if(frame.rows==0 || frame.cols==0)
  58. break;
  59. // update the tracking result
  60. //! [update]
  61. tracker->update(frame,roi);
  62. //! [update]
  63. //! [visualization]
  64. // draw the tracked object
  65. rectangle( frame, roi, Scalar( 255, 0, 0 ), 2, 1 );
  66. // show image with the tracked object
  67. imshow("tracker",frame);
  68. //! [visualization]
  69. //quit on ESC button
  70. if(waitKey(1)==27)break;
  71. }
  72. return 0;
  73. }