tutorial_customizing_cn_tracker.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include <opencv2/core/utility.hpp>
  2. #include <opencv2/tracking.hpp>
  3. #include <opencv2/videoio.hpp>
  4. #include <opencv2/highgui.hpp>
  5. #include <iostream>
  6. #include <cstring>
  7. #include "samples_utility.hpp"
  8. using namespace std;
  9. using namespace cv;
  10. // prototype of the functino for feature extractor
  11. void sobelExtractor(const Mat img, const Rect roi, Mat& feat);
  12. int main( int argc, char** argv ){
  13. // show help
  14. if(argc<2){
  15. cout<<
  16. " Usage: tracker <video_name>\n"
  17. " examples:\n"
  18. " example_tracking_kcf Bolt/img/%04d.jpg\n"
  19. " example_tracking_kcf faceocc2.webm\n"
  20. << endl;
  21. return 0;
  22. }
  23. // declares all required variables
  24. Rect roi;
  25. Mat frame;
  26. //! [param]
  27. TrackerKCF::Params param;
  28. param.desc_pca = TrackerKCF::GRAY | TrackerKCF::CN;
  29. param.desc_npca = 0;
  30. param.compress_feature = true;
  31. param.compressed_size = 2;
  32. //! [param]
  33. // create a tracker object
  34. //! [create]
  35. Ptr<TrackerKCF> tracker = TrackerKCF::create(param);
  36. //! [create]
  37. //! [setextractor]
  38. tracker->setFeatureExtractor(sobelExtractor);
  39. //! [setextractor]
  40. // set input video
  41. std::string video = argv[1];
  42. VideoCapture cap(video);
  43. // get bounding box
  44. cap >> frame;
  45. roi=selectROI("tracker",frame);
  46. //quit if ROI was not selected
  47. if(roi.width==0 || roi.height==0)
  48. return 0;
  49. // initialize the tracker
  50. tracker->init(frame,roi);
  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. tracker->update(frame,roi);
  61. // draw the tracked object
  62. rectangle( frame, roi, Scalar( 255, 0, 0 ), 2, 1 );
  63. // show image with the tracked object
  64. imshow("tracker",frame);
  65. //quit on ESC button
  66. if(waitKey(1)==27)break;
  67. }
  68. return 0;
  69. }
  70. void sobelExtractor(const Mat img, const Rect roi, Mat& feat){
  71. Mat sobel[2];
  72. Mat patch;
  73. Rect region=roi;
  74. //! [insideimage]
  75. // extract patch inside the image
  76. if(roi.x<0){region.x=0;region.width+=roi.x;}
  77. if(roi.y<0){region.y=0;region.height+=roi.y;}
  78. if(roi.x+roi.width>img.cols)region.width=img.cols-roi.x;
  79. if(roi.y+roi.height>img.rows)region.height=img.rows-roi.y;
  80. if(region.width>img.cols)region.width=img.cols;
  81. if(region.height>img.rows)region.height=img.rows;
  82. //! [insideimage]
  83. patch=img(region).clone();
  84. cvtColor(patch,patch, COLOR_BGR2GRAY);
  85. //! [padding]
  86. // add some padding to compensate when the patch is outside image border
  87. int addTop,addBottom, addLeft, addRight;
  88. addTop=region.y-roi.y;
  89. addBottom=(roi.height+roi.y>img.rows?roi.height+roi.y-img.rows:0);
  90. addLeft=region.x-roi.x;
  91. addRight=(roi.width+roi.x>img.cols?roi.width+roi.x-img.cols:0);
  92. copyMakeBorder(patch,patch,addTop,addBottom,addLeft,addRight,BORDER_REPLICATE);
  93. //! [padding]
  94. //! [sobel]
  95. Sobel(patch, sobel[0], CV_32F,1,0,1);
  96. Sobel(patch, sobel[1], CV_32F,0,1,1);
  97. merge(sobel,2,feat);
  98. //! [sobel]
  99. //! [postprocess]
  100. feat=feat/255.0-0.5; // normalize to range -0.5 .. 0.5
  101. //! [postprocess]
  102. }