2014新跟踪算法KCF笔记 --续(代码部分)

KCF跟踪在opencv3.1中集成了,在opencv_contrib/tracking中有,opencv_contrib这个需要重新编译一下opencv3.1才能get.windows下的编译方法如下网址

http://blog.csdn.net/yomo127/article/details/50474955

可以在git上直接下载也可,地址如下

https://github.com/Itseez/opencv_contrib


如果你是直接从git下载的,可以看到关于KCF部分添加的内容如下(git log可)

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /*---------------STEP 1---------------------*/  
  2. /* modify this file 
  3. *  opencv2/tracking/tracker.hpp 
  4. *  and put several lines of snippet similar to  
  5. *  the following: 
  6. */   
  7. /*------------------------------------------*/  
  8.   
  9. class CV_EXPORTS_W TrackerKCF : public Tracker  
  10. {  
  11.  public:  
  12.   struct CV_EXPORTS Params  
  13.   {  
  14.     Params();  
  15.     void read( const FileNode& /*fn*/ );  
  16.     void write( FileStorage& /*fs*/ ) const;  
  17.   };  
  18.   
  19.   /** @brief Constructor 
  20.     @param parameters KCF parameters TrackerKCF::Params 
  21.      */  
  22.   BOILERPLATE_CODE("KCF",TrackerKCF);  
  23. };  
  24.   
  25.   
  26. /*---------------STEP 2---------------------*/  
  27. /* modify this file 
  28. *  src/tracker.cpp 
  29. *  add one line in function   
  30. *  Ptr<Tracker> Tracker::create( const String& trackerType ) 
  31. */   
  32. /*------------------------------------------*/  
  33.   
  34. Ptr<Tracker> Tracker::create( const String& trackerType )  
  35. {  
  36.   BOILERPLATE_CODE("MIL",TrackerMIL);  
  37.   BOILERPLATE_CODE("BOOSTING",TrackerBoosting);  
  38.   BOILERPLATE_CODE("MEDIANFLOW",TrackerMedianFlow);  
  39.   BOILERPLATE_CODE("TLD",TrackerTLD);  
  40.   BOILERPLATE_CODE("KCF",TrackerKCF); // add this line!  
  41.   return Ptr<Tracker>();  
  42. }  
  43.   
  44.   
  45. /*---------------STEP 3---------------------*/  
  46. /* make a new file and paste the snippet below 
  47. *  and modify it according to your needs. 
  48. *  also make sure to put the LICENSE part. 
  49. *  src/trackerKCF.cpp 
  50. */  
  51. /*------------------------------------------*/  
  52.   
  53. /*--------------------------- 
  54. |  TrackerKCFModel 
  55. |---------------------------*/  
  56. namespace cv{  
  57.    /** 
  58.   * \brief Implementation of TrackerModel for MIL algorithm 
  59.   */  
  60.   class TrackerKCFModel : public TrackerModel{  
  61.   public:  
  62.     TrackerKCFModel(TrackerKCF::Params /*params*/){}  
  63.     ~TrackerKCFModel(){}  
  64.   protected:  
  65.     void modelEstimationImpl( const std::vector<Mat>& responses ){}  
  66.     void modelUpdateImpl(){}  
  67.   };  
  68. /* namespace cv */  
  69.   
  70.   
  71. /*--------------------------- 
  72. |  TrackerKCF 
  73. |---------------------------*/  
  74. namespace cv{  
  75.     
  76.   /* 
  77.  * Prototype 
  78.  */  
  79.   class TrackerKCFImpl : public TrackerKCF{  
  80.   public:  
  81.     TrackerKCFImpl( const TrackerKCF::Params ¶meters = TrackerKCF::Params() );  
  82.     void read( const FileNode& fn );  
  83.     void write( FileStorage& fs ) const;  
  84.     
  85.   protected:  
  86.     bool initImpl( const Mat& image, const Rect2d& boundingBox );  
  87.     bool updateImpl( const Mat& image, Rect2d& boundingBox );  
  88.       
  89.     TrackerKCF::Params params;  
  90.   };  
  91.       
  92.   /* 
  93.  * Constructor 
  94.  */  
  95.   Ptr<TrackerKCF> TrackerKCF::createTracker(const TrackerKCF::Params ¶meters){  
  96.       return Ptr<TrackerKCFImpl>(new TrackerKCFImpl(parameters));  
  97.   }  
  98.   TrackerKCFImpl::TrackerKCFImpl( const TrackerKCF::Params ¶meters ) :  
  99.       params( parameters )  
  100.   {  
  101.     isInit = false;  
  102.   }  
  103.     
  104.   void TrackerKCFImpl::read( const cv::FileNode& fn ){  
  105.     params.read( fn );  
  106.   }  
  107.   
  108.   void TrackerKCFImpl::write( cv::FileStorage& fs ) const{  
  109.     params.write( fs );  
  110.   }  
  111.     
  112.     
  113.   bool TrackerKCFImpl::initImpl( const Mat& image, const Rect2d& boundingBox ){  
  114.     model=Ptr<TrackerKCFModel>(new TrackerKCFModel(params));  
  115.     return true;  
  116.   }  
  117.   bool TrackerKCFImpl::updateImpl( const Mat& image, Rect2d& boundingBox ){return true;}  
  118.     
  119.   /* 
  120.  * Parameters 
  121.  */  
  122.   TrackerKCF::Params::Params(){  
  123.         
  124.   }  
  125.   
  126.   void TrackerKCF::Params::read( const cv::FileNode& fn ){  
  127.       
  128.   }  
  129.   
  130.   void TrackerKCF::Params::write( cv::FileStorage& fs ) const{  
  131.       
  132.   }  
  133.     
  134. /* namespace cv */  
然后再来看看/modules/tracking/include/opencv2/tracking/tracker.hpp中关于kcf的定义,多说两句,这里面定义了很多跟踪的方法,包括在线跟踪的MILtracker,OBAtracker,还有,particle filtering tracker,TLD tracker,KCF tracker,他们的基类叫做Tracker,这个Tracker的声明如下:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. class CV_EXPORTS_W Tracker : public virtual Algorithm  
  2. {  
  3.  public:  
  4.   
  5.   virtual ~Tracker();  
  6.   
  7.   /** @brief Initialize the tracker with a know bounding box that surrounding the target 
  8.     @param image The initial frame 
  9.     @param boundingBox The initial boundig box 
  10.  
  11.     @return True if initialization went succesfully, false otherwise 
  12.      */  
  13.   CV_WRAP bool init( const Mat& image, const Rect2d& boundingBox );  
  14.   
  15.   /** @brief Update the tracker, find the new most likely bounding box for the target 
  16.     @param image The current frame 
  17.     @param boundingBox The boundig box that represent the new target location, if true was returned, not 
  18.     modified otherwise 
  19.  
  20.     @return True means that target was located and false means that tracker cannot locate target in 
  21.     current frame. Note, that latter *does not* imply that tracker has failed, maybe target is indeed 
  22.     missing from the frame (say, out of sight) 
  23.      */  
  24.   CV_WRAP bool update( const Mat& image, CV_OUT Rect2d& boundingBox );  
  25.   
  26.   /** @brief Creates a tracker by its name. 
  27.     @param trackerType Tracker type 
  28.  
  29.     The following detector types are supported: 
  30.  
  31.     -   "MIL" -- TrackerMIL 
  32.     -   "BOOSTING" -- TrackerBoosting 
  33.      */  
  34.   CV_WRAP static Ptr<Tracker> create( const String& trackerType );  
  35.   
  36.   virtual void read( const FileNode& fn )=0;  
  37.   virtual void write( FileStorage& fs ) const=0;  
  38.   
  39.   Ptr<TrackerModel> getModel()  
  40.   {  
  41.       return model;  
  42.   }  
  43.   
  44.  protected:  
  45.   
  46.   virtual bool initImpl( const Mat& image, const Rect2d& boundingBox ) = 0;  
  47.   virtual bool updateImpl( const Mat& image, Rect2d& boundingBox ) = 0;  
  48.   
  49.   bool isInit;  
  50.   
  51.   Ptr<TrackerFeatureSet> featureSet;  
  52.   Ptr<TrackerSampler> sampler;  
  53.   Ptr<TrackerModel> model;  
  54. };  

下面看一下继承了Tracker这个KCF tracker,这个算法的论文链接在上一篇博客里

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** @brief KCF is a novel tracking framework that utilizes properties of circulant matrix to enhance the processing speed. 
  2.  * This tracking method is an implementation of @cite KCF_ECCV which is extended to KFC with color-names features (@cite KCF_CN). 
  3.  * The original paper of KCF is available at <http://home.isr.uc.pt/~henriques/circulant/index.html> 
  4.  * as well as the matlab implementation. For more information about KCF with color-names features, please refer to 
  5.  * <http://www.cvl.isy.liu.se/research/objrec/visualtracking/colvistrack/index.html>. 
  6.  */  
  7. class CV_EXPORTS TrackerKCF : public Tracker  
  8. {  
  9. public:  
  10.     /** 
  11.     * \brief Feature type to be used in the tracking grayscale, colornames, compressed color-names 
  12.     * The modes available now: 
  13.     -   "GRAY" -- Use grayscale values as the feature 
  14.     -   "CN" -- Color-names feature 
  15.     */  
  16.     enum MODE {  
  17.         GRAY = (1u << 0),  
  18.         CN = (1u << 1),  
  19.         CUSTOM = (1u << 2)  
  20.     };  
  21.   
  22.     struct CV_EXPORTS Params  
  23.     {  
  24.         /** 
  25.         * \brief Constructor 
  26.         */  
  27.         Params();  
  28.   
  29.         /** 
  30.         * \brief Read parameters from file, currently unused 
  31.         */  
  32.         void read(const FileNode& /*fn*/);  
  33.   
  34.         /** 
  35.         * \brief Read parameters from file, currently unused 
  36.         */  
  37.         void write(FileStorage& /*fs*/const;  
  38.   
  39.         double sigma;                 //!<  gaussian kernel bandwidth  
  40.         double lambda;                //!<  regularization  
  41.         double interp_factor;         //!<  linear interpolation factor for adaptation  
  42.         double output_sigma_factor;   //!<  spatial bandwidth (proportional to target)  
  43.         double pca_learning_rate;     //!<  compression learning rate  
  44.         bool resize;                  //!<  activate the resize feature to improve the processing speed  
  45.         bool split_coeff;             //!<  split the training coefficients into two matrices  
  46.         bool wrap_kernel;             //!<  wrap around the kernel values  
  47.         bool compress_feature;        //!<  activate the pca method to compress the features  
  48.         int max_patch_size;           //!<  threshold for the ROI size  
  49.         int compressed_size;          //!<  feature size after compression  
  50.         unsigned int desc_pca;        //!<  compressed descriptors of TrackerKCF::MODE  
  51.         unsigned int desc_npca;       //!<  non-compressed descriptors of TrackerKCF::MODE  
  52.     };  
  53.   
  54.     virtual void setFeatureExtractor(void(*)(const Mat, const Rect, Mat&), bool pca_func = false);  
  55.   
  56.     /** @brief Constructor 
  57.     @param parameters KCF parameters TrackerKCF::Params 
  58.     */  
  59.     BOILERPLATE_CODE("KCF", TrackerKCF);  
  60. };  

好,上面这些只是介绍了在opencv中 KCF的接口,下面举一个例子来看看怎么调用

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //安装配置好opencv3.1  
  2. #include <opencv2/core/utility.hpp>  
  3. #include <opencv2/tracking.hpp>  
  4. #include <opencv2/videoio.hpp>  
  5. #include <opencv2/highgui.hpp>  
  6. #include <iostream>  
  7. #include <cstring>  
  8.   
  9. using namespace std;  
  10. using namespace cv;  
  11.   
  12. int main( ){  
  13.   // declares all required variables  
  14.   //! [vars]  
  15.   Rect2d roi;  
  16.   Mat frame;  
  17.   //! [vars]  
  18.   
  19.   // create a tracker object  
  20.   //! create这个函数的参数也可以是-   "MIL" -- TrackerMIL  
  21.    //! -   "BOOSTING" -- TrackerBoosting ,还可以是 "TLD","MEDIANFLOW"  
  22.   Ptr<Tracker> tracker = Tracker::create( "KCF" );  
  23.   //! [create]  
  24.   
  25.   // set input video  
  26.   //! [setvideo]  
  27.   std::string video = "test.avi";  
  28.   VideoCapture cap(video);  
  29.   //! [setvideo]  
  30.   
  31.   // get bounding box  
  32.   //! [getframe]  
  33.   cap >> frame;  
  34.   //! [getframe]  
  35.   //! [selectroi]选择目标roi以GUI的形式  
  36.   roi=selectROI("tracker",frame);  
  37.   //! [selectroi]  
  38.   
  39.   //quit if ROI was not selected  
  40.   if(roi.width==0 || roi.height==0)  
  41.     return 0;  
  42.   
  43.   // initialize the tracker  
  44.   //! [init]  
  45.   tracker->init(frame,roi);  
  46.   //! [init]  
  47.   
  48.   // perform the tracking process  
  49.   printf("Start the tracking process\n");  
  50.   for ( ;; ){  
  51.     // get frame from the video  
  52.     cap >> frame;  
  53.   
  54.     // stop the program if no more images  
  55.     if(frame.rows==0 || frame.cols==0)  
  56.       break;  
  57.   
  58.     // update the tracking result  
  59.     //! [update]  
  60.     tracker->update(frame,roi);  
  61.     //! [update]  
  62.   
  63.     //! [visualization]  
  64.     // draw the tracked object  
  65.     rectangle( frame, roi, Scalar( 255, 0, 0 ), 2, 1 );  
  66.   
  67.     // show image with the tracked object  
  68.     imshow("tracker",frame);  
  69.     //! [visualization]  
  70.     //quit on ESC button  
  71.     if(waitKey(1)==27)  
  72.   break;  
  73.   }  
  74.   
  75.   return 0;  
  76. }  
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值