kcf.cpp 1.8 KB

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