质量声明:原创文章,内容质量问题请评论吐槽。如对您产生干扰,可私信删除。
主要参考:opencv_contrib/modules/tracking/src/trackerKCF.cpp



摘要: 介绍opencv4.x版本的TrackerKCF的调用接口与示例


参数声明

  • tracker.hpp:
struct CV_EXPORTS Params
{
  /** * \brief Constructor */
  Params();

  /** * \brief Read parameters from a file */
  void read(const FileNode& /*fn*/);

  /** * \brief Write parameters to a file */
  void write(FileStorage& /*fs*/) const;

  float detect_thresh;          //!< detection confidence threshold
  float sigma;                  //!< gaussian kernel bandwidth
  float lambda;                 //!< regularization
  float interp_factor;          //!< linear interpolation factor for adaptation
  float output_sigma_factor;    //!< spatial bandwidth (proportional to target)
  float pca_learning_rate;      //!< compression learning rate
  bool resize;                  //!< activate the resize feature to improve the processing speed
  bool split_coeff;             //!< split the training coefficients into two matrices
  bool wrap_kernel;             //!< wrap around the kernel values
  bool compress_feature;        //!< activate the pca method to compress the features
  int max_patch_size;           //!< threshold for the ROI size
  int compressed_size;          //!< feature size after compression
  int desc_pca;        //!< compressed descriptors of TrackerKCF::MODE
  int desc_npca;       //!< non-compressed descriptors of TrackerKCF::MODE
};
  • 对应构造函数:
  static Ptr<TrackerKCF> create(const TrackerKCF::Params &parameters);

参数默认值

  • trackerKCF.cpp
TrackerKCF::Params::Params()
{
      detect_thresh = 0.5f;
      sigma=0.2f;
      lambda=0.0001f;
      interp_factor=0.075f;
      output_sigma_factor=1.0f / 16.0f;
      resize=true;
      max_patch_size=80*80;
      split_coeff=true;
      wrap_kernel=false;
      desc_npca = GRAY;
      desc_pca = CN;

      //feature compression
      compress_feature=true;
      compressed_size=2;
      pca_learning_rate=0.15f;
  }

参数具体含义

判别阈值 detect_thresh = 0.5f

// extract the maximum response
minMaxLoc( response, &minVal, &maxVal, &minLoc, &maxLoc );
if (maxVal < params.detect_thresh)
{
    return false;
}

高斯核带宽 sigma = 0.2f

//compute the gaussian kernel
denseGaussKernel(params.sigma,x,z,k,layers,vxf,vyf,vxyf,xy_data,xyf_data);

尺寸缩小1/2 resize = true

//resize the ROI whenever needed
if(params.resize && roi.width*roi.height>params.max_patch_size)
{
	 resizeImage=true;
	 roi.x/=2.0;
	 roi.y/=2.0;
	 roi.width/=2.0;
	 roi.height/=2.0;
}
// resize the image whenever needed
if(resizeImage)
    resize(img, img, Size(img.cols / 2, img.rows / 2), 0, 0, INTER_LINEAR_EXACT);

代码示例

默认参数

cv::Ptr<cv::Tracker> tracker;
tracker = cv::TrackerKCF::create();
tracker->init(curFrame, roi_rect);

自定义参数

我遇到的问题是将视频大小(1280x720)缩减一半时,频繁出现跟踪失败。当置信度阈值调整到0.3时,跟踪正常。

cv::Ptr<cv::Tracker> tracker;
cv::TrackerKCF::Params params;
params.detect_thresh = 0.3f;
tracker = cv::TrackerKCF::create(params);
tracker->init(curFrame, roi_rect);

附:其他Tracker默认参数

Boosting

TrackerBoosting::Params::Params()
{
  numClassifiers = 100;
  samplerOverlap = 0.99f;
  samplerSearchFactor = 1.8f;
  iterationInit = 50;
  featureSetNumFeatures = ( numClassifiers * 10 ) + iterationInit;
}

MIL

TrackerMIL::Params::Params()
{
  samplerInitInRadius = 3;
  samplerSearchWinSize = 25;
  samplerInitMaxNegNum = 65;
  samplerTrackInRadius = 4;
  samplerTrackMaxPosNum = 100000;
  samplerTrackMaxNegNum = 65;
  featureSetNumFeatures = 250;
}