算法的计算过程主要是利用了傅立叶快速变换,目前作者已经提供了matlab源代码,该代码在i7机器上运行速度可以达到350FPS,速度效果着实明显!同时,在博客 http://blog.csdn.net/zouxy09/article/details/16889905 上,博主给出了他写出的单尺度c++版STC代码。在本篇博客的最后,我也对代码进行了整理,完善,代码中有什么不足的地方,希望大家能够积极指正。
本论文官方主页:http://www4.comp.polyu.edu.hk/~cslzhang/STC/STC.htm
#pragma once #include <opencv2/opencv.hpp> using namespace cv; class STC { public: STC(); ~STC(); public: void init(Mat& _frame,Rect& _rect); void processFrame(Mat& _frame, Rect& _rect, int _frameNum); private: void creatHammingWindow(); void getContextPrior(Mat& _frame,Rect& _rect); void updateStcModel(); void complexOperation(Mat& _src1,Mat& _src2,Mat& _hscf,int flag); void calcuConfidenceMap(Mat& _stcModel, Mat& _contextPrior,Point& _target,double& _maxVal); private: Point centerPoint; double rho; double alpha; double sigma; double lambda; int num; double scale; vector<double> maxValue; Rect ctxRegion; //context region Mat conf; //confidence map Mat weight; Mat hmwindow; Mat contextPrior; Mat scModel; Mat stcModel; };
STC::STC() { rho=0.075; // learning parameter alpha=2.25; // the parameters of the map function scale=1.0; //initial scale ratio lambda=0.25; //Eq.(15) num=5; //num consecutive frames }
void STC::processFrame(Mat& _frame,Rect& _rect,int _frameNum) { getContextPrior(_frame,ctxRegion); Point target; double maxVal; calcuConfidenceMap(stcModel,contextPrior,target,maxVal); maxValue.push_back(maxVal); /***********update scale by Eq.(15)**********/ if (_frameNum%(num+2)==0) { double scale_curr=0.0; for (int k=0;k<num;k++) { scale_curr+=sqrt(maxValue[_frameNum-k-1]/maxValue[_frameNum-k-2]); } scale=(1-lambda)*scale+lambda*(scale_curr/num); sigma=sigma*scale; } centerPoint.x=target.x+ctxRegion.x; centerPoint.y=target.y+ctxRegion.y; _rect.x=centerPoint.x-_rect.width*0.5; _rect.y=centerPoint.y-_rect.height*0.5; _rect&=Rect(0,0,_frame.cols,_frame.rows); ctxRegion.x=centerPoint.x-ctxRegion.width*0.5; ctxRegion.y=centerPoint.y-ctxRegion.height*0.5; ctxRegion&=Rect(0,0,_frame.cols,_frame.rows); getContextPrior(_frame,ctxRegion); updateStcModel(); }