在前面一个介绍《Real-Time Compressive Tracking》这个paper的感知跟踪算法的博文中,我说过后面会学习下它的C++源码,但是当时因为有些事,所以就没有看了。今天,上到博客,看到一朋友在这个博文中评论说,有个地方不太明白。然后,觉得该履行自己的承诺,去学习学习源码了。所以刚才就花了几个小时去看了C++的源码,做了详细的注释。希望对大家有点帮助。在这也感谢这位朋友。当然,因为自己也刚刚接触这个领域,所以也有很多地方我也看不懂或者理解错了,也渴望大家的指导。
下面是这个算法的工程网站:里面包含了上面这篇论文、Matlab和C++版本的代码,还有测试数据、demo等。
http://www4.comp.polyu.edu.hk/~cslzhang/CT/CT.htm
之前自己学习这个《Real-Time Compressive Tracking》介绍的感知跟踪算法:
http://blog.csdn.net/zouxy09/article/details/8118360
非常感谢Kaihua等的paper《Real-Time Compressive Tracking》,非常感谢它的C++代码的编写和贡献者Yang Xian。
这个C++代码编写的非常简洁、清晰和漂亮。另外,C++代码好像有对论文中提到的一些东西进行裁剪,例如多尺度的卷积等。不知道理解对不对啊,觉得有一些地方自己没找到相关的线索。望大家交流和指导。
好了,废话不多说了。下面是自己注释的源码。因为代码编写的流程非常清晰,所以我就不总结流程了。这个工程包含三个文件:CompressiveTracker.cpp、CompressiveTracker.h和RunTracker.cpp,其中因为RunTracker.cpp和TLD算法中的代码差不多,我这里就不注释了,大家可以参考我之前的:
TLD(Tracking-Learning-Detection)学习与源码理解之(四)
http://blog.csdn.net/zouxy09/article/details/7893032
下面是具体的源码:
CompressiveTracker.h
-
-
-
-
-
-
-
-
-
-
-
-
-
- #pragma once
- #include <opencv2/opencv.hpp>
- #include <vector>
-
- using std::vector;
- using namespace cv;
-
- class CompressiveTracker
- {
- public:
- CompressiveTracker(void);
- ~CompressiveTracker(void);
-
- private:
- int featureMinNumRect;
- int featureMaxNumRect;
- int featureNum;
- vector<vector<Rect>> features;
- vector<vector<float>> featuresWeight;
- int rOuterPositive;
- vector<Rect> samplePositiveBox;
- vector<Rect> sampleNegativeBox;
- int rSearchWindow;
- Mat imageIntegral;
- Mat samplePositiveFeatureValue;
- Mat sampleNegativeFeatureValue;
-
-
-
- vector<float> muPositive;
- vector<float> sigmaPositive;
- vector<float> muNegative;
- vector<float> sigmaNegative;
- float learnRate;
- vector<Rect> detectBox;
- Mat detectFeatureValue;
- RNG rng;
-
- private:
- void HaarFeature(Rect& _objectBox, int _numFeature);
- void sampleRect(Mat& _image, Rect& _objectBox, float _rInner, float _rOuter, int _maxSampleNum, vector<Rect>& _sampleBox);
- void sampleRect(Mat& _image, Rect& _objectBox, float _srw, vector<Rect>& _sampleBox);
- void getFeatureValue(Mat& _imageIntegral, vector<Rect>& _sampleBox, Mat& _sampleFeatureValue);
- void classifierUpdate(Mat& _sampleFeatureValue, vector<float>& _mu, vector<float>& _sigma, float _learnRate);
- void radioClassifier(vector<float>& _muPos, vector<float>& _sigmaPos, vector<float>& _muNeg, vector<float>& _sigmaNeg,
- Mat& _sampleFeatureValue, float& _radioMax, int& _radioMaxIndex);
- public:
- void processFrame(Mat& _frame, Rect& _objectBox);
- void init(Mat& _frame, Rect& _objectBox);
- };
CompressiveTracker.cpp
- #include "CompressiveTracker.h"
- #include <math.h>
- #include <iostream>
- using namespace cv;
- using namespace std;
-
-
-
- CompressiveTracker::CompressiveTracker(void)
- {
- featureMinNumRect = 2;
- featureMaxNumRect = 4;
- featureNum = 50;
- rOuterPositive = 4;
- rSearchWindow = 25;
- muPositive = vector<float>(featureNum, 0.0f);
- muNegative = vector<float>(featureNum, 0.0f);
- sigmaPositive = vector<float>(featureNum, 1.0f);
- sigmaNegative = vector<float>(featureNum, 1.0f);
- learnRate = 0.85f;
- }
-
- CompressiveTracker::~CompressiveTracker(void)
- {
- }
-
-
-
-
-
-
-
-
-
- void CompressiveTracker::HaarFeature(Rect& _objectBox, int _numFeature)
-
-
-
-
-
- {
-
-
- features = vector<vector<Rect>>(_numFeature, vector<Rect>());
-
-
-
-
- featuresWeight = vector<vector<float>>(_numFeature, vector<float>());
-
-
-
- int numRect;
- Rect rectTemp;
- float weightTemp;
-
- for (int i=0; i<_numFeature; i++)
- {
-
-
-
-
-
- numRect = cvFloor(rng.uniform((double)featureMinNumRect, (double)featureMaxNumRect));
-
-
- for (int j=0; j<numRect; j++)
- {
-
-
-
-
- rectTemp.x = cvFloor(rng.uniform(0.0, (double)(_objectBox.width - 3)));
- rectTemp.y = cvFloor(rng.uniform(0.0, (double)(_objectBox.height - 3)));
-
- rectTemp.width = cvCeil(rng.uniform(0.0, (double)(_objectBox.width - rectTemp.x - 2)));
- rectTemp.height = cvCeil(rng.uniform(0.0, (double)(_objectBox.height - rectTemp.y - 2)));
-
- features[i].push_back(rectTemp);
-
-
-
-
-
-
-
-
- weightTemp = (float)pow(-1.0, cvFloor(rng.uniform(0.0, 2.0))) / sqrt(float(numRect));
-
-
- featuresWeight[i].push_back(weightTemp);
-
- }
- }
- }
-
-
- void CompressiveTracker::sampleRect(Mat& _image, Rect& _objectBox, float _rInner, float _rOuter, int _maxSampleNum, vector<Rect>& _sampleBox)
-
-
-
-
-
-
-
-
-
- {
- int rowsz = _image.rows - _objectBox.height - 1;
- int colsz = _image.cols - _objectBox.width - 1;
-
-
-
-
-
- float inradsq = _rInner*_rInner;
- float outradsq = _rOuter*_rOuter;
-
- int dist;
-
-
- int minrow = max(0,(int)_objectBox.y-(int)_rInner);
- int maxrow = min((int)rowsz-1,(int)_objectBox.y+(int)_rInner);
- int mincol = max(0,(int)_objectBox.x-(int)_rInner);
- int maxcol = min((int)colsz-1,(int)_objectBox.x+(int)_rInner);
-
-
- int i = 0;
-
-
-
- float prob = ((float)(_maxSampleNum))/(maxrow-minrow+1)/(maxcol-mincol+1);
-
- int r;
- int c;
-
- _sampleBox.clear();
- Rect rec(0,0,0,0);
-
- for( r=minrow; r<=(int)maxrow; r++ )
- for( c=mincol; c<=(int)maxcol; c++ ){
-
- dist = (_objectBox.y-r)*(_objectBox.y-r) + (_objectBox.x-c)*(_objectBox.x-c);
-
-
-
-
-
-
-
- if( rng.uniform(0.,1.) < prob && dist < inradsq && dist >= outradsq ){
-
- rec.x = c;
- rec.y = r;
- rec.width = _objectBox.width;
- rec.height= _objectBox.height;
-
- _sampleBox.push_back(rec);
-
- i++;
- }
- }
-
- _sampleBox.resize(i);
-
- }
-
-
-
-
- void CompressiveTracker::sampleRect(Mat& _image, Rect& _objectBox, float _srw, vector<Rect>& _sampleBox)
-
- {
- int rowsz = _image.rows - _objectBox.height - 1;
- int colsz = _image.cols - _objectBox.width - 1;
- float inradsq = _srw*_srw;
-
- int dist;
-
- int minrow = max(0,(int)_objectBox.y-(int)_srw);
- int maxrow = min((int)rowsz-1,(int)_objectBox.y+(int)_srw);
- int mincol = max(0,(int)_objectBox.x-(int)_srw);
- int maxcol = min((int)colsz-1,(int)_objectBox.x+(int)_srw);
-
- int i = 0;
-
- int r;
- int c;
-
- Rect rec(0,0,0,0);
- _sampleBox.clear();
-
- for( r=minrow; r<=(int)maxrow; r++ )
- for( c=mincol; c<=(int)maxcol; c++ ){
- dist = (_objectBox.y-r)*(_objectBox.y-r) + (_objectBox.x-c)*(_objectBox.x-c);
-
- if( dist < inradsq ){
-
- rec.x = c;
- rec.y = r;
- rec.width = _objectBox.width;
- rec.height= _objectBox.height;
-
- _sampleBox.push_back(rec);
-
- i++;
- }
- }
-
- _sampleBox.resize(i);
-
- }
-
-
-
-
-
-
-
- void CompressiveTracker::getFeatureValue(Mat& _imageIntegral, vector<Rect>& _sampleBox, Mat& _sampleFeatureValue)
- {
- int sampleBoxSize = _sampleBox.size();
- _sampleFeatureValue.create(featureNum, sampleBoxSize, CV_32F);
- float tempValue;
- int xMin;
- int xMax;
- int yMin;
- int yMax;
-
- for (int i=0; i<featureNum; i++)
- {
- for (int j=0; j<sampleBoxSize; j++)
- {
- tempValue = 0.0f;
- for (size_t k=0; k<features[i].size(); k++)
- {
-
-
- xMin = _sampleBox[j].x + features[i][k].x;
- xMax = _sampleBox[j].x + features[i][k].x + features[i][k].width;
- yMin = _sampleBox[j].y + features[i][k].y;
- yMax = _sampleBox[j].y + features[i][k].y + features[i][k].height;
-
-
-
-
-
- tempValue += featuresWeight[i][k] *
- (_imageIntegral.at<float>(yMin, xMin) +
- _imageIntegral.at<float>(yMax, xMax) -
- _imageIntegral.at<float>(yMin, xMax) -
- _imageIntegral.at<float>(yMax, xMin));
- }
- _sampleFeatureValue.at<float>(i,j) = tempValue;
- }
- }
- }
-
-
-
-
-
- void CompressiveTracker::classifierUpdate(Mat& _sampleFeatureValue, vector<float>& _mu, vector<float>& _sigma, float _learnRate)
- {
- Scalar muTemp;
- Scalar sigmaTemp;
-
- for (int i=0; i<featureNum; i++)
- {
-
- meanStdDev(_sampleFeatureValue.row(i), muTemp, sigmaTemp);
-
-
- _sigma[i] = (float)sqrt( _learnRate*_sigma[i]*_sigma[i] + (1.0f-_learnRate)*sigmaTemp.val[0]*sigmaTemp.val[0]
- + _learnRate*(1.0f-_learnRate)*(_mu[i]-muTemp.val[0])*(_mu[i]-muTemp.val[0]));
-
- _mu[i] = _mu[i]*_learnRate + (1.0f-_learnRate)*muTemp.val[0];
- }
- }
-
-
- void CompressiveTracker::radioClassifier(vector<float>& _muPos, vector<float>& _sigmaPos, vector<float>& _muNeg, vector<float>& _sigmaNeg,
- Mat& _sampleFeatureValue, float& _radioMax, int& _radioMaxIndex)
- {
- float sumRadio;
-
-
- _radioMax = -FLT_MAX;
-
- _radioMaxIndex = 0;
- float pPos;
- float pNeg;
- int sampleBoxNum = _sampleFeatureValue.cols;
-
- for (int j=0; j<sampleBoxNum; j++)
- {
- sumRadio = 0.0f;
- for (int i=0; i<featureNum; i++)
- {
-
-
-
- pPos = exp( (_sampleFeatureValue.at<float>(i,j)-_muPos[i])*(_sampleFeatureValue.at<float>(i,j)-_muPos[i]) / -(2.0f*_sigmaPos[i]*_sigmaPos[i]+1e-30) ) / (_sigmaPos[i]+1e-30);
- pNeg = exp( (_sampleFeatureValue.at<float>(i,j)-_muNeg[i])*(_sampleFeatureValue.at<float>(i,j)-_muNeg[i]) / -(2.0f*_sigmaNeg[i]*_sigmaNeg[i]+1e-30) ) / (_sigmaNeg[i]+1e-30);
-
-
-
-
-
- sumRadio += log(pPos+1e-30) - log(pNeg+1e-30);
- }
- if (_radioMax < sumRadio)
- {
- _radioMax = sumRadio;
- _radioMaxIndex = j;
- }
- }
- }
-
-
- void CompressiveTracker::init(Mat& _frame, Rect& _objectBox)
- {
-
-
- HaarFeature(_objectBox, featureNum);
-
-
-
-
- sampleRect(_frame, _objectBox, rOuterPositive, 0, 1000000, samplePositiveBox);
- sampleRect(_frame, _objectBox, rSearchWindow*1.5, rOuterPositive+4.0, 100, sampleNegativeBox);
-
-
- integral(_frame, imageIntegral, CV_32F);
-
-
- getFeatureValue(imageIntegral, samplePositiveBox, samplePositiveFeatureValue);
- getFeatureValue(imageIntegral, sampleNegativeBox, sampleNegativeFeatureValue);
-
-
- classifierUpdate(samplePositiveFeatureValue, muPositive, sigmaPositive, learnRate);
- classifierUpdate(sampleNegativeFeatureValue, muNegative, sigmaNegative, learnRate);
- }
-
-
- void CompressiveTracker::processFrame(Mat& _frame, Rect& _objectBox)
- {
-
-
- sampleRect(_frame, _objectBox, rSearchWindow, detectBox);
-
- integral(_frame, imageIntegral, CV_32F);
-
- getFeatureValue(imageIntegral, detectBox, detectFeatureValue);
- int radioMaxIndex;
- float radioMax;
-
- radioClassifier(muPositive, sigmaPositive, muNegative, sigmaNegative, detectFeatureValue, radioMax, radioMaxIndex);
-
- _objectBox = detectBox[radioMaxIndex];
-
-
-
- sampleRect(_frame, _objectBox, rOuterPositive, 0.0, 1000000, samplePositiveBox);
- sampleRect(_frame, _objectBox, rSearchWindow*1.5, rOuterPositive+4.0, 100, sampleNegativeBox);
-
-
- getFeatureValue(imageIntegral, samplePositiveBox, samplePositiveFeatureValue);
- getFeatureValue(imageIntegral, sampleNegativeBox, sampleNegativeFeatureValue);
-
-
- classifierUpdate(samplePositiveFeatureValue, muPositive, sigmaPositive, learnRate);
- classifierUpdate(sampleNegativeFeatureValue, muNegative, sigmaNegative, learnRate);
- }