压缩感知源码理解

压缩跟踪Compressive Tracking源码理解

zouxy09@qq.com

http://blog.csdn.net/zouxy09

       在前面一个介绍《Real-Time Compressive Tracking》这个paper的感知跟踪算法的博文中,我说过后面会学习下它的C++源码,但是当时因为有些事,所以就没有看了。今天,上到博客,看到一朋友在这个博文中评论说,有个地方不太明白。然后,觉得该履行自己的承诺,去学习学习源码了。所以刚才就花了几个小时去看了C++的源码,做了详细的注释。希望对大家有点帮助。在这也感谢这位朋友。当然,因为自己也刚刚接触这个领域,所以也有很多地方我也看不懂或者理解错了,也渴望大家的指导。

      下面是这个算法的工程网站:里面包含了上面这篇论文、MatlabC++版本的代码,还有测试数据、demo等。

            http://www4.comp.polyu.edu.hk/~cslzhang/CT/CT.htm

       之前自己学习这个《Real-Time Compressive Tracking》介绍的感知跟踪算法:

            http://blog.csdn.net/zouxy09/article/details/8118360

       非常感谢Kaihua等的paperReal-Time Compressive Tracking》,非常感谢它的C++代码的编写和贡献者Yang Xian

       这个C++代码编写的非常简洁、清晰和漂亮。另外,C++代码好像有对论文中提到的一些东西进行裁剪,例如多尺度的卷积等。不知道理解对不对啊,觉得有一些地方自己没找到相关的线索。望大家交流和指导。

       好了,废话不多说了。下面是自己注释的源码。因为代码编写的流程非常清晰,所以我就不总结流程了。这个工程包含三个文件:CompressiveTracker.cppCompressiveTracker.hRunTracker.cpp,其中因为RunTracker.cppTLD算法中的代码差不多,我这里就不注释了,大家可以参考我之前的:

        TLDTracking-Learning-Detection)学习与源码理解之(四)

            http://blog.csdn.net/zouxy09/article/details/7893032

下面是具体的源码:

CompressiveTracker.h

[cpp]  view plain copy
  1. /************************************************************************
  2. * File: CompressiveTracker.h
  3. * Brief: C++ demo for paper: Kaihua Zhang, Lei Zhang, Ming-Hsuan Yang,"Real-Time Compressive Tracking," ECCV 2012.
  4. * Version: 1.0
  5. * Author: Yang Xian
  6. * Email: yang_xian521@163.com
  7. * Date: 2012/08/03
  8. * History:
  9. * Revised by Kaihua Zhang on 14/8/2012
  10. * Email: zhkhua@gmail.com
  11. * Homepage: http://www4.comp.polyu.edu.hk/~cskhzhang/
  12. ************************************************************************/ 
  13. //这是一个比较常用的C/C++杂注,只要在头文件的最开始加入这条杂注,就能够保证头文件只被插入和编译一次 
  14. #pragma once 
  15. #include <opencv2/opencv.hpp> 
  16. #include <vector> 
  17.  
  18. using std::vector; 
  19. using namespace cv; 
  20. //--------------------------------------------------- 
  21. class CompressiveTracker 
  22. public
  23.     CompressiveTracker(void); 
  24.     ~CompressiveTracker(void); 
  25.  
  26. private
  27.     int featureMinNumRect; 
  28.     int featureMaxNumRect; 
  29.     int featureNum;  //每个box的harr特征个数(也就是弱分类器个数) 
  30.     vector<vector<Rect>> features; 
  31.     vector<vector<float>> featuresWeight; 
  32.     int rOuterPositive; //在离上一帧跟踪到的目标位置的距离小于rOuterPositive的范围内采集 正样本 
  33.     vector<Rect> samplePositiveBox;  //采集的正样本box集 
  34.     vector<Rect> sampleNegativeBox;  //采集的负样本box集 
  35.     int rSearchWindow;   //扫描窗口的大小,或者说检测box的大小 
  36.     Mat imageIntegral;   //图像的积分图 
  37.     Mat samplePositiveFeatureValue;  //采集的正样本的harr特征值 
  38.     Mat sampleNegativeFeatureValue;  //采集的负样本的harr特征值 
  39.     //对每个样本z(m维向量),它的低维表示是v(n维向量,n远小于m)。假定v中的各元素是独立分布的。 
  40.     //假定在分类器H(v)中的条件概率p(vi|y=1)和p(vi|y=0)属于高斯分布,并且可以用以下四个参数来描述: 
  41.     //分别是描述正负样本的高斯分布的均值u和方差sigma 
  42.     vector<float> muPositive; 
  43.     vector<float> sigmaPositive; 
  44.     vector<float> muNegative; 
  45.     vector<float> sigmaNegative; 
  46.     float learnRate;   //学习速率,控制分类器参数更新的步长 
  47.     vector<Rect> detectBox;  //需要检测的box 
  48.     Mat detectFeatureValue; 
  49.     RNG rng;  //随机数 
  50.  
  51. private
  52.     void HaarFeature(Rect& _objectBox, int _numFeature); 
  53.     void sampleRect(Mat& _image, Rect& _objectBox, float _rInner, float _rOuter, int _maxSampleNum, vector<Rect>& _sampleBox); 
  54.     void sampleRect(Mat& _image, Rect& _objectBox, float _srw, vector<Rect>& _sampleBox); 
  55.     void getFeatureValue(Mat& _imageIntegral, vector<Rect>& _sampleBox, Mat& _sampleFeatureValue); 
  56.     void classifierUpdate(Mat& _sampleFeatureValue, vector<float>& _mu, vector<float>& _sigma, float _learnRate); 
  57.     void radioClassifier(vector<float>& _muPos, vector<float>& _sigmaPos, vector<float>& _muNeg, vector<float>& _sigmaNeg, 
  58.                         Mat& _sampleFeatureValue, float& _radioMax, int& _radioMaxIndex); 
  59. public
  60.     void processFrame(Mat& _frame, Rect& _objectBox); 
  61.     void init(Mat& _frame, Rect& _objectBox); 
  62. }; 
[cpp]  view plain copy
  1. /************************************************************************ 
  2. * File: CompressiveTracker.h 
  3. * Brief: C++ demo for paper: Kaihua Zhang, Lei Zhang, Ming-Hsuan Yang,"Real-Time Compressive Tracking," ECCV 2012. 
  4. * Version: 1.0 
  5. * Author: Yang Xian 
  6. * Email: yang_xian521@163.com 
  7. * Date: 2012/08/03 
  8. * History: 
  9. * Revised by Kaihua Zhang on 14/8/2012 
  10. * Email: zhkhua@gmail.com 
  11. * Homepage: http://www4.comp.polyu.edu.hk/~cskhzhang/ 
  12. ************************************************************************/  
  13. //这是一个比较常用的C/C++杂注,只要在头文件的最开始加入这条杂注,就能够保证头文件只被插入和编译一次  
  14. #pragma once  
  15. #include <opencv2/opencv.hpp>  
  16. #include <vector>  
  17.   
  18. using std::vector;  
  19. using namespace cv;  
  20. //---------------------------------------------------  
  21. class CompressiveTracker  
  22. {  
  23. public:  
  24.     CompressiveTracker(void);  
  25.     ~CompressiveTracker(void);  
  26.   
  27. private:  
  28.     int featureMinNumRect;  
  29.     int featureMaxNumRect;  
  30.     int featureNum;  //每个box的harr特征个数(也就是弱分类器个数)  
  31.     vector<vector<Rect>> features;  
  32.     vector<vector<float>> featuresWeight;  
  33.     int rOuterPositive; //在离上一帧跟踪到的目标位置的距离小于rOuterPositive的范围内采集 正样本  
  34.     vector<Rect> samplePositiveBox;  //采集的正样本box集  
  35.     vector<Rect> sampleNegativeBox;  //采集的负样本box集  
  36.     int rSearchWindow;   //扫描窗口的大小,或者说检测box的大小  
  37.     Mat imageIntegral;   //图像的积分图  
  38.     Mat samplePositiveFeatureValue;  //采集的正样本的harr特征值  
  39.     Mat sampleNegativeFeatureValue;  //采集的负样本的harr特征值  
  40.     //对每个样本z(m维向量),它的低维表示是v(n维向量,n远小于m)。假定v中的各元素是独立分布的。  
  41.     //假定在分类器H(v)中的条件概率p(vi|y=1)和p(vi|y=0)属于高斯分布,并且可以用以下四个参数来描述:  
  42.     //分别是描述正负样本的高斯分布的均值u和方差sigma  
  43.     vector<float> muPositive;  
  44.     vector<float> sigmaPositive;  
  45.     vector<float> muNegative;  
  46.     vector<float> sigmaNegative;  
  47.     float learnRate;   //学习速率,控制分类器参数更新的步长  
  48.     vector<Rect> detectBox;  //需要检测的box  
  49.     Mat detectFeatureValue;  
  50.     RNG rng;  //随机数  
  51.   
  52. private:  
  53.     void HaarFeature(Rect& _objectBox, int _numFeature);  
  54.     void sampleRect(Mat& _image, Rect& _objectBox, float _rInner, float _rOuter, int _maxSampleNum, vector<Rect>& _sampleBox);  
  55.     void sampleRect(Mat& _image, Rect& _objectBox, float _srw, vector<Rect>& _sampleBox);  
  56.     void getFeatureValue(Mat& _imageIntegral, vector<Rect>& _sampleBox, Mat& _sampleFeatureValue);  
  57.     void classifierUpdate(Mat& _sampleFeatureValue, vector<float>& _mu, vector<float>& _sigma, float _learnRate);  
  58.     void radioClassifier(vector<float>& _muPos, vector<float>& _sigmaPos, vector<float>& _muNeg, vector<float>& _sigmaNeg,  
  59.                         Mat& _sampleFeatureValue, float& _radioMax, int& _radioMaxIndex);  
  60. public:  
  61.     void processFrame(Mat& _frame, Rect& _objectBox);  
  62.     void init(Mat& _frame, Rect& _objectBox);  
  63. };  


CompressiveTracker.cpp

[cpp]  view plain copy
  1. #include "CompressiveTracker.h" 
  2. #include <math.h> 
  3. #include <iostream> 
  4. using namespace cv; 
  5. using namespace std; 
  6.  
  7. //------------------------------------------------ 
  8. //构造函数,初始化各参数 
  9. CompressiveTracker::CompressiveTracker(void
  10.     featureMinNumRect = 2; 
  11.     featureMaxNumRect = 4;  // number of rectangle from 2 to 4 
  12.     featureNum = 50;    // number of all weaker classifiers, i.e,feature pool 
  13.     rOuterPositive = 4; // radical scope of positive samples 
  14.     rSearchWindow = 25; // size of search window 
  15.     muPositive = vector<float>(featureNum, 0.0f); 
  16.     muNegative = vector<float>(featureNum, 0.0f); 
  17.     sigmaPositive = vector<float>(featureNum, 1.0f); 
  18.     sigmaNegative = vector<float>(featureNum, 1.0f); 
  19.     learnRate = 0.85f;  // Learning rate parameter 
  20.  
  21. CompressiveTracker::~CompressiveTracker(void
  22.  
  23. //通过积分图来计算采集到的每一个样本的harr特征,这个特征通过与featuresWeight来相乘 
  24. //就相当于投影到随机测量矩阵中了,也就是进行稀疏表达了。这里不明白的话,可以看下 
  25. //论文中的图二,就比较直观了。 
  26. //还有一点:实际上这里采用的不属于真正的harr特征,我博客中翻译有误。这里计算的是 
  27. //在box中采样得到的不同矩形框的灰度加权求和(当权重是负数的时候就是灰度差) 
  28. //当为了表述方便,我下面都用harr特征来描述。 
  29. //每一个样本有50个harr特征,每一个harr特征是由2到3个随机选择的矩形框来构成的, 
  30. //对这些矩形框的灰度加权求和作为这一个harr特征的特征值。 
  31. void CompressiveTracker::HaarFeature(Rect& _objectBox, int _numFeature) 
  32. /*Description: compute Haar features
  33.   Arguments:
  34.   -_objectBox: [x y width height] object rectangle
  35.   -_numFeature: total number of features. The default is 50.
  36. */ 
  37.     //_numFeature是一个样本box的harr特征个数,共50个。而上面说到, 
  38.     //每一个harr特征是由2到3个随机选择的矩形框(vector<Rect>()类型)来构成的。 
  39.     features = vector<vector<Rect>>(_numFeature, vector<Rect>()); 
  40.     //每一个反应特征的矩形框对应于一个权重,实际上就是随机测量矩阵中相应的元素,用它来与对应的特征 
  41.     //相乘,表示以权重的程度来感知这个特征。换句话说,featuresWeight就是随机测量矩阵。 
  42.     //这个矩阵的元素的赋值看论文中的第二部分。或者也可以参考下我的博文:(呵呵,好像博文也没说清楚) 
  43.     //http://blog.csdn.net/zouxy09/article/details/8118360 
  44.     featuresWeight = vector<vector<float>>(_numFeature, vector<float>()); 
  45.      
  46.     //numRect是每个特征的矩形框个数还是论文中说的随机测量矩阵中的s?还有兼备两种功能? 
  47.     //论文中说s取2或者3时,矩阵就满足Johnson-Lindenstrauss推论。 
  48.     int numRect; 
  49.     Rect rectTemp; 
  50.     float weightTemp; 
  51.        
  52.     for (int i=0; i<_numFeature; i++) 
  53.     { 
  54.         //如何生成服从某个概率分布的随机数(或者说 sample)的问题。 
  55.         //比如,你想要从一个服从正态分布的随机变量得到 100 个样本,那么肯定抽到接近其均值的样本的 
  56.         //概率要大许多,从而导致抽到的样本很多是集中在那附近的。 
  57.         //rng.uniform()返回一个从[ 1,2)范围均匀采样的随机数,即在[ 1,2)内服从均匀分布(取不同值概率相同) 
  58.         //那么下面的功能就是得到[2,4)范围的随机数,然后用cvFloor返回不大于参数的最大整数值,那要么是2,要么是3。 
  59.         numRect = cvFloor(rng.uniform((double)featureMinNumRect, (double)featureMaxNumRect)); 
  60.          
  61.         //int c = 1; 
  62.         for (int j=0; j<numRect; j++) 
  63.         { 
  64.             //我在一个box中随机生成一个矩形框,那和你这个box的x和y坐标就无关了,但我必须保证我选择 
  65.             //的这个矩形框不会超出你这个box的范围啊,是吧 
  66.             //但这里的3和下面的2是啥意思呢?我就不懂了,个人理解是为了避免这个矩形框太靠近box的边缘了 
  67.             //要离边缘最小2个像素,不知道这样理解对不对,恳请指导 
  68.             rectTemp.x = cvFloor(rng.uniform(0.0, (double)(_objectBox.width - 3))); 
  69.             rectTemp.y = cvFloor(rng.uniform(0.0, (double)(_objectBox.height - 3))); 
  70.             //cvCeil 返回不小于参数的最小整数值 
  71.             rectTemp.width = cvCeil(rng.uniform(0.0, (double)(_objectBox.width - rectTemp.x - 2))); 
  72.             rectTemp.height = cvCeil(rng.uniform(0.0, (double)(_objectBox.height - rectTemp.y - 2))); 
  73.             //保存得到的特征模板。注意哦,这里的矩形框是相对于box的相对位置哦,不是针对整幅图像的哦 
  74.             features[i].push_back(rectTemp); 
  75.              
  76.             //weightTemp = (float)pow(-1.0, c); 
  77.             //pow(-1.0, c)也就是-1的c次方,而c随机地取0或者1,也就是说weightTemp是随机的正或者负。 
  78.             //随机测量矩阵中,矩阵元素有三种,sqrt(s)、-sqrt(s)和零。为正和为负的概率是相等的, 
  79.             //这就是为什么是[2,4)均匀采样的原因,就是取0或者1概率一样。 
  80.             //但是这里为什么是sqrt(s)分之一呢?还有什么时候是0呢?论文中是0的概率不是挺大的吗? 
  81.             //没有0元素,哪来的稀疏表达和压缩呢?不懂,恳请指导!(当然稀疏表达的另一个好处 
  82.             //就是只需保存非零元素。但这里和这个有关系吗?) 
  83.             weightTemp = (float)pow(-1.0, cvFloor(rng.uniform(0.0, 2.0))) / sqrt(float(numRect)); 
  84.              
  85.             //保存每一个特征模板对应的权重 
  86.             featuresWeight[i].push_back(weightTemp); 
  87.             
  88.         } 
  89.     } 
  90.  
  91. //在上一帧跟踪的目标box的周围采集若干正样本和负样本,来初始化或者更新分类器的 
  92. void CompressiveTracker::sampleRect(Mat& _image, Rect& _objectBox, float _rInner, float _rOuter, int _maxSampleNum, vector<Rect>& _sampleBox) 
  93. /* Description: compute the coordinate of positive and negative sample image templates
  94.    Arguments:
  95.    -_image:        processing frame
  96.    -_objectBox:    recent object position
  97.    -_rInner:       inner sampling radius
  98.    -_rOuter:       Outer sampling radius
  99.    -_maxSampleNum: maximal number of sampled images
  100.    -_sampleBox:    Storing the rectangle coordinates of the sampled images.
  101. */ 
  102.     int rowsz = _image.rows - _objectBox.height - 1; 
  103.     int colsz = _image.cols - _objectBox.width - 1; 
  104.     //我们是在上一帧跟踪的目标box的周围采集正样本和负样本的,而这个周围是通过以 
  105.     //这个目标为中心的两个圆来表示,这两个圆的半径是_rInner和_rOuter。 
  106.     //我们在离上一帧跟踪的目标box的小于_rInner距离的范围内采集正样本, 
  107.     //在大于_rOuter距离的范围内采集负样本(论文中还有一个上界,但好像 
  108.     //这里没有,其实好像也没什么必要噢) 
  109.     float inradsq = _rInner*_rInner; 
  110.     float outradsq = _rOuter*_rOuter; 
  111.      
  112.     int dist; 
  113.  
  114.     //这四个是为了防止采集的框超出图像范围的,对采集的box的x和y坐标做限制 
  115.     int minrow = max(0,(int)_objectBox.y-(int)_rInner); 
  116.     int maxrow = min((int)rowsz-1,(int)_objectBox.y+(int)_rInner); 
  117.     int mincol = max(0,(int)_objectBox.x-(int)_rInner); 
  118.     int maxcol = min((int)colsz-1,(int)_objectBox.x+(int)_rInner); 
  119.          
  120.      
  121.     int i = 0; 
  122.     //分母相当于x能采集的范围乘以y能采集的范围,也就是可以采集的最大box个数, 
  123.     //那么_maxSampleNum(我们需要采集的box的最大个数)肯定得小于或者等于它。 
  124.     //那这个prob是干嘛的呢?到下面用到它的地方说 
  125.     float prob = ((float)(_maxSampleNum))/(maxrow-minrow+1)/(maxcol-mincol+1); 
  126.  
  127.     int r; 
  128.     int c; 
  129.      
  130.     _sampleBox.clear();//important 
  131.     Rect rec(0,0,0,0); 
  132.  
  133.     for( r=minrow; r<=(int)maxrow; r++ ) 
  134.         for( c=mincol; c<=(int)maxcol; c++ ){ 
  135.             //计算生成的box到目标box的距离 
  136.             dist = (_objectBox.y-r)*(_objectBox.y-r) + (_objectBox.x-c)*(_objectBox.x-c); 
  137.  
  138.             //后两个条件是保证距离需要在_rInner和_rOuter的范围内 
  139.             //那么rng.uniform(0.,1.) < prob 这个是干嘛的呢? 
  140.             //连着上面看,如果_maxSampleNum大于那个最大个数,prob就大于1,这样, 
  141.             //rng.uniform(0.,1.) < prob这个条件就总能满足,表示在这个范围产生的 
  142.             //所以box我都要了(因为我本身想要更多的,但是你给不了我那么多,那么你能给的,我肯定全要了)。 
  143.             //那如果你给的太多了,我不要那么多,也就是prob<1,那我就随机地跳几个走好了 
  144.             if( rng.uniform(0.,1.) < prob && dist < inradsq && dist >= outradsq ){ 
  145.  
  146.                 rec.x = c; 
  147.                 rec.y = r; 
  148.                 rec.width = _objectBox.width;  //没有做尺度不变?至此至终box的大小都没变化 
  149.                 rec.height= _objectBox.height; 
  150.                  
  151.                 _sampleBox.push_back(rec);               
  152.                  
  153.                 i++; 
  154.             } 
  155.         } 
  156.      
  157.         _sampleBox.resize(i); 
  158.          
  159.  
  160. //这个sampleRect的重载函数是用来在上一帧跟踪的目标box的周围(距离小于_srw)采集若干box来待检测。 
  161. //与上面的那个不一样,上面那个是在这一帧已经检测出目标的基础上,采集正负样本来更新分类器的。 
  162. //上面那个属于论文中提到的算法的第四个步骤,这个是第一个步骤。然后过程差不多,没什么好说的了 
  163. void CompressiveTracker::sampleRect(Mat& _image, Rect& _objectBox, float _srw, vector<Rect>& _sampleBox) 
  164. /* Description: Compute the coordinate of samples when detecting the object.*/ 
  165.     int rowsz = _image.rows - _objectBox.height - 1; 
  166.     int colsz = _image.cols - _objectBox.width - 1; 
  167.     float inradsq = _srw*_srw;       
  168.  
  169.     int dist; 
  170.  
  171.     int minrow = max(0,(int)_objectBox.y-(int)_srw); 
  172.     int maxrow = min((int)rowsz-1,(int)_objectBox.y+(int)_srw); 
  173.     int mincol = max(0,(int)_objectBox.x-(int)_srw); 
  174.     int maxcol = min((int)colsz-1,(int)_objectBox.x+(int)_srw); 
  175.  
  176.     int i = 0; 
  177.  
  178.     int r; 
  179.     int c; 
  180.  
  181.     Rect rec(0,0,0,0); 
  182.     _sampleBox.clear();//important 
  183.  
  184.     for( r=minrow; r<=(int)maxrow; r++ ) 
  185.         for( c=mincol; c<=(int)maxcol; c++ ){ 
  186.             dist = (_objectBox.y-r)*(_objectBox.y-r) + (_objectBox.x-c)*(_objectBox.x-c); 
  187.  
  188.             if( dist < inradsq ){ 
  189.  
  190.                 rec.x = c; 
  191.                 rec.y = r; 
  192.                 rec.width = _objectBox.width; 
  193.                 rec.height= _objectBox.height; 
  194.  
  195.                 _sampleBox.push_back(rec);               
  196.  
  197.                 i++; 
  198.             } 
  199.         } 
  200.      
  201.         _sampleBox.resize(i); 
  202.  
  203.  
  204. // Compute the features of samples 
  205. //通过积分图来计算采集到的每一个样本的harr特征,这个特征通过与featuresWeight来相乘 
  206. //就相当于投影到随机测量矩阵中了,也就是进行稀疏表达了。这里不明白的话,可以看下 
  207. //论文中的图二,就比较直观了。所以这里得到的是:每个样本的稀疏表达后的harr特征。 
  208. //还有一点:实际上这里采用的不属于真正的harr特征,我博客中翻译有误。这里计算的是 
  209. //在box中采样得到的不同矩形框的灰度加权求和 
  210. void CompressiveTracker::getFeatureValue(Mat& _imageIntegral, vector<Rect>& _sampleBox, Mat& _sampleFeatureValue) 
  211.     int sampleBoxSize = _sampleBox.size(); 
  212.     _sampleFeatureValue.create(featureNum, sampleBoxSize, CV_32F); 
  213.     float tempValue; 
  214.     int xMin; 
  215.     int xMax; 
  216.     int yMin; 
  217.     int yMax; 
  218.  
  219.     for (int i=0; i<featureNum; i++) 
  220.     { 
  221.         for (int j=0; j<sampleBoxSize; j++) 
  222.         { 
  223.             tempValue = 0.0f; 
  224.             for (size_t k=0; k<features[i].size(); k++) 
  225.             { 
  226.                 //features中保存的特征模板(矩形框)是相对于box的相对位置的, 
  227.                 //所以需要加上box的坐标才是其在整幅图像中的坐标 
  228.                 xMin = _sampleBox[j].x + features[i][k].x; 
  229.                 xMax = _sampleBox[j].x + features[i][k].x + features[i][k].width; 
  230.                 yMin = _sampleBox[j].y + features[i][k].y; 
  231.                 yMax = _sampleBox[j].y + features[i][k].y + features[i][k].height; 
  232.                 //通过积分图来快速计算一个矩形框的像素和,积分图不了解的话,可以看下我的这个博文: 
  233.                 //http://blog.csdn.net/zouxy09/article/details/7929570 
  234.                 //那么这里tempValue就是经过稀释矩阵加权后的灰度和了。 
  235.                 //每一个harr特征是由2到3个矩形框来构成的,对这些矩形框的灰度加权求和 
  236.                 //作为这一个harr特征的特征值。然后一个样本有50个harr特征 
  237.                 tempValue += featuresWeight[i][k] *  
  238.                     (_imageIntegral.at<float>(yMin, xMin) + 
  239.                     _imageIntegral.at<float>(yMax, xMax) - 
  240.                     _imageIntegral.at<float>(yMin, xMax) - 
  241.                     _imageIntegral.at<float>(yMax, xMin)); 
  242.             } 
  243.             _sampleFeatureValue.at<float>(i,j) = tempValue; 
  244.         } 
  245.     } 
  246.  
  247. // Update the mean and variance of the gaussian classifier 
  248. //论文中是通过用高斯分布去描述样本的每一个harr特征的概率分布的。高斯分布就可以通过期望和方差 
  249. //两个参数来表征。然后通过正负样本的每一个harr特征高斯概率分布的对数比值,来构建分类器决策 
  250. //该box属于目标还是背景。这里计算新采集到的正负样本的特征的期望和标准差,并用其来更新分类器 
  251. void CompressiveTracker::classifierUpdate(Mat& _sampleFeatureValue, vector<float>& _mu, vector<float>& _sigma, float _learnRate) 
  252.     Scalar muTemp; 
  253.     Scalar sigmaTemp; 
  254.      
  255.     for (int i=0; i<featureNum; i++) 
  256.     { 
  257.         //计算所有正样本或者负样本的某个harr特征的期望和标准差 
  258.         meanStdDev(_sampleFeatureValue.row(i), muTemp, sigmaTemp); 
  259.         
  260.         //这个模型参数更新的公式见论文的公式6 
  261.         _sigma[i] = (float)sqrt( _learnRate*_sigma[i]*_sigma[i] + (1.0f-_learnRate)*sigmaTemp.val[0]*sigmaTemp.val[0]  
  262.         + _learnRate*(1.0f-_learnRate)*(_mu[i]-muTemp.val[0])*(_mu[i]-muTemp.val[0]));  // equation 6 in paper 
  263.  
  264.         _mu[i] = _mu[i]*_learnRate + (1.0f-_learnRate)*muTemp.val[0];   // equation 6 in paper 
  265.     } 
  266.  
  267. // Compute the ratio classifier  
  268. void CompressiveTracker::radioClassifier(vector<float>& _muPos, vector<float>& _sigmaPos, vector<float>& _muNeg, vector<float>& _sigmaNeg, 
  269.                                          Mat& _sampleFeatureValue, float& _radioMax, int& _radioMaxIndex) 
  270.     float sumRadio; 
  271.     //FLT_MAX是最大的浮点数的宏定义,那么-FLT_MAX就是最小的浮点数了 
  272.     //这个是拿来存放 那么多box中最大的分类分数的 
  273.     _radioMax = -FLT_MAX;  
  274.     //这个是对应于上面那个,是存放分类分数最大的那个box的 
  275.     _radioMaxIndex = 0; 
  276.     float pPos; 
  277.     float pNeg; 
  278.     int sampleBoxNum = _sampleFeatureValue.cols; 
  279.  
  280.     for (int j=0; j<sampleBoxNum; j++)  //每帧采样得到的需要检测的box 
  281.     { 
  282.         sumRadio = 0.0f; 
  283.         for (int i=0; i<featureNum; i++)  //每个box的需要匹配的特征数 
  284.         { 
  285.             //计算每个特征的概率,特征分布近似于高斯分布,故将描述该特征的均值和标准差代入高斯模型就可以 
  286.             //得到,分别在正样本和负样本的基础上,出现该特征的概率是多少。如果正样本时候的概率大,那么 
  287.             //我们就说,这个特征对应的样本是正样本。数学上比较大小,就是减法或者除法了,这里是取对数比值 
  288.             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); 
  289.             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); 
  290.              
  291.             //paper的方程4:计算分类结果,得到一个分数,这个分数是由一个样本或者box的50个特征(弱分类) 
  292.             //进入分类器分类得到的结果总和(强分类?)。表征的是目前这个box的特征属于正样本(目标)的 
  293.             //可能性大小。哪个分数最大,自然我就认为你是目标了。(当然,在具体应用中需要加一些策略去 
  294.             //改善误跟踪的情况。例如如果最高的分数都达不到一个阈值,那就不存在目标等) 
  295.             sumRadio += log(pPos+1e-30) - log(pNeg+1e-30);  // equation 4 
  296.         } 
  297.         if (_radioMax < sumRadio) //拿到最大的分数和相应的box索引 
  298.         { 
  299.             _radioMax = sumRadio; 
  300.             _radioMaxIndex = j; 
  301.         } 
  302.     } 
  303.  
  304. //传入第一帧和要跟踪的目标box(由文件读入或者用户鼠标框选),来初始化分类器 
  305. void CompressiveTracker::init(Mat& _frame, Rect& _objectBox) 
  306.     // compute feature template 
  307.     //计算box的harr特征模板,先存着 
  308.     HaarFeature(_objectBox, featureNum); 
  309.  
  310.     // compute sample templates 
  311.     //因为这是第一帧,目标box是由由文件读入或者用户鼠标框选的,是已知的, 
  312.     //所以我们通过在这个目标box周围,采集正样本和负样本来初始化我们的分类器 
  313.     sampleRect(_frame, _objectBox, rOuterPositive, 0, 1000000, samplePositiveBox); 
  314.     sampleRect(_frame, _objectBox, rSearchWindow*1.5, rOuterPositive+4.0, 100, sampleNegativeBox); 
  315.  
  316.     //计算积分图,用以快速的计算harr特征 
  317.     integral(_frame, imageIntegral, CV_32F); 
  318.  
  319.     //通过上面的积分图,计算我们采样到的正负样本的box的harr特征 
  320.     getFeatureValue(imageIntegral, samplePositiveBox, samplePositiveFeatureValue); 
  321.     getFeatureValue(imageIntegral, sampleNegativeBox, sampleNegativeFeatureValue); 
  322.      
  323.     //通过上面的正负样本的特征来初始化分类器 
  324.     classifierUpdate(samplePositiveFeatureValue, muPositive, sigmaPositive, learnRate); 
  325.     classifierUpdate(sampleNegativeFeatureValue, muNegative, sigmaNegative, learnRate); 
  326.  
  327. //传入上一帧跟踪到的box,来处理新的一帧 
  328. void CompressiveTracker::processFrame(Mat& _frame, Rect& _objectBox) 
  329.     // predict 
  330.     //在上一帧跟踪到的boxbox周围,采集需要检测的box框 
  331.     sampleRect(_frame, _objectBox, rSearchWindow, detectBox); 
  332.     //计算这一帧的积分图 
  333.     integral(_frame, imageIntegral, CV_32F); 
  334.     //用积分图来计算上面采集到的每个box的harr特征 
  335.     getFeatureValue(imageIntegral, detectBox, detectFeatureValue); 
  336.     int radioMaxIndex; 
  337.     float radioMax; 
  338.     //对上面的每个box进行匹配分类 
  339.     radioClassifier(muPositive, sigmaPositive, muNegative, sigmaNegative, detectFeatureValue, radioMax, radioMaxIndex); 
  340.     //得到分数最高的那个目标box 
  341.     _objectBox = detectBox[radioMaxIndex]; 
  342.  
  343.     // update 
  344.     //在新跟踪到的这个目标box的周围,采集正样本和负样本来更新我们的分类器 
  345.     sampleRect(_frame, _objectBox, rOuterPositive, 0.0, 1000000, samplePositiveBox); 
  346.     sampleRect(_frame, _objectBox, rSearchWindow*1.5, rOuterPositive+4.0, 100, sampleNegativeBox); 
  347.      
  348.     //通过上面的积分图,计算我们采样到的正负样本的box的harr特征 
  349.     getFeatureValue(imageIntegral, samplePositiveBox, samplePositiveFeatureValue); 
  350.     getFeatureValue(imageIntegral, sampleNegativeBox, sampleNegativeFeatureValue); 
  351.      
  352.     //通过上面的正负样本的特征来更新我们的分类器 
  353.     classifierUpdate(samplePositiveFeatureValue, muPositive, sigmaPositive, learnRate); 
  354.     classifierUpdate(sampleNegativeFeatureValue, muNegative, sigmaNegative, learnRate); 

http://blog.csdn.net/xiaojidan2011/article/details/8213985
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值