基于HOG特征的Adaboost行人检测

申明,本文非笔者原创,原文转载自:http://blog.csdn.net/van_ruin/article/details/9166591


1.方向梯度直方图Histogramof Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子。它通过计算和统计图像局部区域的梯度方向直方图来构成特征。基本知识可以参考博客:http://blog.csdn.net/zouxy09/article/details/7929348

  2.Adaboost的基础知识可以参考书籍:统计学习方法,第八章-提升方法adaboost。

  这里利用HOG来训练Adaboost行人检测。Haar-Adaboost算法中,弱分类器仅对一维分类。但是在Hog特征中,特征是每个block的串联。如果仅对一维分类(一个cell的其中一个方向的权值),就不能有效利用block的归一化效果。所以我们使用logistic弱分类器对每个block进行分类(实验中,每个block包含4cell,每个cell9bin,即36维特征)。

  本实验需要注意的地方:

   1.  adaboost误差率需要计算权重 

   2.  logistic回归需要使用带权重的logistic分类器 

   3.  logistic分类可能与数据分布相反。需要计算两次。(相反的情况下,拟合没有意义,需要将数据反转(1->0,0->1))

  发现总结与问题

  1. 公理 1.   对于任何数据的二值分类,能够得到大于等于 0.5 的线性分类器。
  2.  推论 对于任何带权重数据的二值分类,能够得到大于等于 0.5 的线性分类器。
  3. 推论?    对于任何带权重数据的 n 值分类,能够得到大于等于 1/n 的线性分类器。
  4.  对于与 logistic 函数分布相反的数据,应该如何处理?(本实验的处理方式如前面所述)。

 实验结果后的猜想

ß 猜想 1 Adaboost 弱分类器所选取的特征仍然要保持一定的颗粒度。像素级的特征是无效的。

 实验结果与分析

  训练集 : 500/500 ;测试集 : 19/22(200个弱分类器)
  测试数据较少,但是训练集的高正确率至少证明其能够由弱分类器(错误率普遍在 0.25 左右)提高样本数据集的精度。
  17 张图片中,有部分图片较模糊,行人影像较小,可能导致难以分辨。

下面给出代码,希望各位能够指正错误。说明:本代码全部由自己编写,所用函数未调用OpenCV实用库函数,及机器学习库函数(基本数据除外)。
[cpp]  view plain copy
  1. /***********************************************************/  
  2. /** Copyright by Weidi Xu, S.C.U.T in Guangzhou, Guangdong**/  
  3. /***********************************************************/  
  4.   
  5. #include <opencv2\opencv.hpp>  
  6. #include <cstring>  
  7. #include <cstdio>  
  8. #include <cmath>  
  9. #include <ctime>  
  10.   
  11. using std::clock_t;  
  12. using std::clock;  
  13. using namespace cv;  
  14.   
  15. //const parameters for image  
  16. const int NUM_NEGIMAGE = 1000;  
  17. const int NUM_POSIMAGE = 500;  
  18. const int NUM_IMAGE = 1500;  
  19. const int NUM_TESTIMAGE = 22;  
  20. const int MAX_DIMENSION = 3781;  
  21. const int IMAGE_ROWS = 128;  
  22. const int IMAGE_COLS = 64;  
  23. const int CELLSIZE = 8;  
  24. const int BLOCKSIZE = 16;  
  25. const int MOVELENGTH = 8;  
  26. const int BINSIZE = 9;  
  27. const double PI = 2*acos(0.0);  
  28. const double eps = 1e-8;  
  29.   
  30. //mediate parameter  
  31. const int NUM_BLOCK_ROWS = (IMAGE_ROWS-BLOCKSIZE)/MOVELENGTH+1;  
  32. const int NUM_BLOCK_COLS = (IMAGE_COLS-BLOCKSIZE)/MOVELENGTH+1;  
  33. const int NUM_BLOCK_FEATURES = (BLOCKSIZE/CELLSIZE)*(BLOCKSIZE/CELLSIZE)*BINSIZE+1;//zero for theta[0]  
  34.   
  35. //data from image  
  36. //since the features in the adaboost should contain a single block, it's better to define the feature of 3-dimension;  
  37. double features[NUM_IMAGE][NUM_BLOCK_ROWS][NUM_BLOCK_COLS][NUM_BLOCK_FEATURES];  
  38. double type[NUM_IMAGE]; //1 - pos, 0 - neg  
  39. double y[NUM_IMAGE]; //1 - pos, -1 - neg  
  40.   
  41. //number of weak classifier(changing in experiment)  
  42. const int NUM_WEAKCLASSIFIER = 100;  
  43.   
  44. //data for adaboost  
  45. double weight[NUM_IMAGE];  
  46.   
  47. //logistic function(dimension is given by NUM_BLOCK_FEATURES(37 in this setting))  
  48. double logistic(double theta[], double x[])  
  49. {  
  50.     double ans = 0;  
  51.     for(int i = 0 ; i < NUM_BLOCK_FEATURES; i++)  
  52.     {  
  53.         ans += theta[i]*x[i];  
  54.     }  
  55.     return 1/(1+std::exp(-ans));  
  56. }  
  57.   
  58. struct WeakClassifier  
  59. {  
  60.     double _theta[NUM_BLOCK_FEATURES]; //threshold classifier  
  61.     int _index_row;  //classify by the features in block[_block_row][_block_y]  
  62.     int _index_col;  
  63.     int _isreverse; //1 for (> := pos, < := neg); 0 for (< := pos, >:= neg)  
  64.     double _alpha;   
  65.     double _error;  
  66.     void clear()  
  67.     {  
  68.         memset(_theta, 0.0, NUM_BLOCK_FEATURES*sizeof(double));  
  69.         _alpha = 0.0;  
  70.         _error = 1;  
  71.         _index_row = -1;  
  72.         _index_col = -1;  
  73.         _isreverse = true;  
  74.     }  
  75.   
  76.     //return 1 or -1  
  77.     int cal(double x[NUM_BLOCK_ROWS][NUM_BLOCK_COLS][NUM_BLOCK_FEATURES])  
  78.     {  
  79.         int ans = logistic(_theta, x[_index_row][_index_col]);  
  80.         if(ans > 0.5)  
  81.         {  
  82.             if(_isreverse)  
  83.                 return -1;  
  84.             else  
  85.                 return 1;  
  86.         }  
  87.         else  
  88.         {  
  89.             if(_isreverse)  
  90.                 return 1;  
  91.             else  
  92.                 return -1;  
  93.         }  
  94.     }  
  95.   
  96.     void print()  
  97.     {  
  98.         //theta  
  99.         for(int i = 0 ; i < NUM_BLOCK_FEATURES; i++)  
  100.         printf("%lf ", _theta[i]);  
  101.         printf("\n");  
  102.   
  103.         //int _index_row;  
  104.         printf("%d ",_index_row);  
  105.   
  106.         //int _index_col;  
  107.         printf("%d ",_index_col);  
  108.   
  109.         //int _isreverse; //1 for (> := pos, < := neg); 0 for (< := pos, >:= neg)  
  110.         printf("%d ",_isreverse);  
  111.   
  112.         //double _alpha;  
  113.         printf("%lf ",_alpha);  
  114.           
  115.         //double _error;  
  116.         printf("%lf \n",_error);  
  117.     }  
  118. }weakClassifier[NUM_WEAKCLASSIFIER];  
  119.   
  120. //Util Function  
  121. double arc2angle(double arc)  
  122. {  
  123.     return arc/PI*180.0;  
  124. }  
  125.   
  126. double angle2arc(double angle)  
  127. {  
  128.     return angle/180.0*PI;  
  129. }  
  130.   
  131. void posfilename(int i, char* filename)  
  132. {  
  133.     sprintf(filename, "pos/pos (%d).png", i);  
  134.     return;  
  135. }  
  136.   
  137. void negfilename(int i, char* filename)  
  138. {  
  139.     sprintf(filename, "neg/neg (%d).png", i);  
  140.     return;  
  141. }  
  142.   
  143. void testfilename(int i, char* filename)  
  144. {  
  145.     sprintf(filename, "test_pos/test (%d).png", i);  
  146.     return ;  
  147. }  
  148.   
  149. //I(x,y) = sqrt(I(x,y))  
  150. void normalizeImage(Mat& inputImage)  
  151. {  
  152.     // accept only char type matrices    
  153.     CV_Assert(inputImage.depth() != sizeof(uchar));    
  154.     int channels = inputImage.channels();    
  155.     int nRows = inputImage.rows ;    
  156.     int nCols = inputImage.cols* channels;    
  157.     if (inputImage.isContinuous())    
  158.     {    
  159.         nCols *= nRows;    
  160.         nRows = 1;    
  161.     }    
  162.     int i,j;    
  163.     uchar* p;    
  164.     for( i = 0; i < nRows; ++i)    
  165.     {    
  166.         p = inputImage.ptr<uchar>(i);    
  167.         for ( j = 0; j < nCols; ++j)    
  168.         {    
  169.             p[j] = int(sqrt(p[j]*1.0));    
  170.         }    
  171.     }  
  172.     return;  
  173. }  
  174.   
  175. //I(x,y) 第一维的梯度为xGradient  
  176. void calGredient(const Mat& inputImage, double xGradient[IMAGE_ROWS][IMAGE_COLS], double yGradient[IMAGE_ROWS][IMAGE_COLS])  
  177. {  
  178.     uchar* dataptr = inputImage.data;  
  179.     int nrows = inputImage.rows;  
  180.     int ncols = inputImage.cols;  
  181.   
  182.     //cal xgradient  
  183.     for(int i = 1 ; i < nrows - 1; i++)  
  184.     {  
  185.         for(int j = 0 ; j < ncols; j++)  
  186.         {  
  187.             xGradient[i][j] = inputImage.at<uchar>(i+1,j) - inputImage.at<uchar>(i-1,j);  
  188.         }  
  189.     }  
  190.   
  191.     //cal margin  
  192.     for(int i = 0 ; i < ncols; i++)  
  193.     {  
  194.         xGradient[0][i] = (inputImage.at<uchar>(1,i) - inputImage.at<uchar>(0,i))*2;  
  195.         xGradient[nrows-1][i] = (inputImage.at<uchar>(nrows-1,i) - inputImage.at<uchar>(nrows-2,i))*2;  
  196.     }  
  197.   
  198.     //cal ygradient  
  199.     for(int i = 0 ; i < nrows ; i++)  
  200.     {  
  201.         for(int j = 1 ; j < ncols - 1; j++)  
  202.         {  
  203.             yGradient[i][j] = inputImage.at<uchar>(i,j+1) - inputImage.at<uchar>(i,j-1);  
  204.         }  
  205.     }  
  206.   
  207.     //cal margin  
  208.     for(int i = 0 ; i < nrows; i++)  
  209.     {  
  210.         xGradient[i][0] = (inputImage.at<uchar>(i,1) - inputImage.at<uchar>(i,0))*2;  
  211.         xGradient[i][ncols-1] = (inputImage.at<uchar>(i,ncols-1) - inputImage.at<uchar>(i,ncols-2))*2;  
  212.     }  
  213. }  
  214.   
  215. //cal the HogFeatures by block  
  216. void calHogFeatures(Mat& inputImage, double outputFeature[NUM_BLOCK_ROWS][NUM_BLOCK_COLS][NUM_BLOCK_FEATURES])  
  217. {  
  218.     int nrows = inputImage.rows;  
  219.     int ncols = inputImage.cols;  
  220.     int type = inputImage.type();  
  221.   
  222.     if(nrows != IMAGE_ROWS || ncols != IMAGE_COLS)  
  223.         abort();  
  224.   
  225.     //cal x,yGradient  
  226.     double xGradient[IMAGE_ROWS][IMAGE_COLS];  
  227.     double yGradient[IMAGE_ROWS][IMAGE_COLS];  
  228.     calGredient(inputImage, xGradient, yGradient);  
  229.   
  230.     //computation median  
  231.     double gradient[IMAGE_ROWS][IMAGE_COLS];  
  232.     double direction[IMAGE_ROWS][IMAGE_COLS];  
  233.   
  234.     for(int i = 0 ; i < nrows; i++)  
  235.     {  
  236.         for(int j = 0 ; j < ncols; j++)  
  237.         {  
  238.             double gx = xGradient[i][j];  
  239.             double gy = yGradient[i][j];  
  240.             gradient[i][j] = sqrt(gx*gx + gy*gy);  
  241.             direction[i][j] = arc2angle(atan2(gy, gx));  
  242.         }  
  243.     }  
  244.   
  245.     //compute cellinfo 8*8  
  246.     double cellinfo[IMAGE_ROWS/CELLSIZE][IMAGE_COLS/CELLSIZE][BINSIZE];  
  247.     memset(cellinfo, 0, sizeof(cellinfo));  
  248.   
  249.     for(int i = 0; i < IMAGE_ROWS/CELLSIZE; i++)  
  250.     {  
  251.         for(int j = 0 ; j < IMAGE_COLS/CELLSIZE; j++)  
  252.         {  
  253.             double* cell = cellinfo[i][j];  
  254.               
  255.             //cal single cellinfo of 8*8  
  256.             for(int ci = 0 ; ci < CELLSIZE; ci++)  
  257.             {  
  258.                 for(int cj = 0; cj < CELLSIZE; cj++)  
  259.                 {  
  260.                     //find org pix;  
  261.                     int px = i*CELLSIZE + ci;  
  262.                     int py = j*CELLSIZE + cj;  
  263.   
  264.                     int binindex = int((direction[px][py]+180.0)/(360.0/BINSIZE));  
  265.                     //handle bound   
  266.                     if(fabs(direction[px][py]-180) < eps)  
  267.                     {  
  268.                         binindex = BINSIZE-1;  
  269.                     }  
  270.                     if(fabs(direction[px][py]+180) < eps)  
  271.                     {  
  272.                         binindex = 0;  
  273.                     }  
  274.                     if(binindex < 0 || binindex >= BINSIZE)  
  275.                     {  
  276.                         printf("Wrong binindex: %d %lf %lf %lf", binindex, xGradient[px][py], yGradient[px][py], direction[px][py]);  
  277.                         abort();  
  278.                     }  
  279.   
  280.                     cell[binindex] += gradient[px][py];  
  281.                 }  
  282.             }  
  283.         }  
  284.     }  
  285.   
  286.     /*double blockinfo[(IMAGE_ROWS-BLOCKSIZE)/MOVELENGTH+1][(IMAGE_COLS-BLOCKSIZE)/MOVELENGTH+1][(BLOCKSIZE/CELLSIZE)*(BLOCKSIZE/CELLSIZE)*BINSIZE];*/  
  287.   
  288.     if(MOVELENGTH%CELLSIZE != 0)  
  289.     {  
  290.         printf("MOVELENGTH%CELLSIZE != 0");  
  291.         abort();  
  292.     }  
  293.   
  294.     //cal blockinfo  
  295.     for(int i = 0 ; i < (IMAGE_ROWS-BLOCKSIZE)/MOVELENGTH + 1; i++)  
  296.     {  
  297.         for(int j = 0 ; j < (IMAGE_COLS-BLOCKSIZE)/MOVELENGTH + 1; j++)  
  298.         {  
  299.             int bfindex = 0; outputFeature[i][j][bfindex++] = 1;  
  300.   
  301.             //cal the position of this block  
  302.             for(int c1 = 0; c1 < BLOCKSIZE/CELLSIZE; c1++)  
  303.             {  
  304.                 for(int c2 = 0 ; c2 < BLOCKSIZE/CELLSIZE; c2++)  
  305.                 {  
  306.                     //cal the index of cell  
  307.                     int cx = i*MOVELENGTH/CELLSIZE+c1;  
  308.                     int cy = j*MOVELENGTH/CELLSIZE+c2;  
  309.   
  310.                     for(int binindex = 0 ; binindex < BINSIZE; binindex++)  
  311.                     {  
  312.                         outputFeature[i][j][bfindex++] = cellinfo[cx][cy][binindex];  
  313.                     }  
  314.                 }  
  315.             }  
  316.         }  
  317.     }  
  318.     return;  
  319. }  
  320.   
  321. //use global variables  
  322. void trainLogisticRegression(int block_row,int block_col, double theta[], double& errorrate, int& isreverse)  
  323. {  
  324.     double theta1[NUM_BLOCK_FEATURES], theta2[NUM_BLOCK_FEATURES];  
  325.     memset(theta1, 0, NUM_BLOCK_FEATURES*sizeof(double));  
  326.     memset(theta2, 0, NUM_BLOCK_FEATURES*sizeof(double));  
  327.     double errorrate1 = 0;  
  328.     double errorrate2 = 0;  
  329.     double rightnum1 = 0;  
  330.     double rightnum2 = 0;  
  331.     isreverse = 0;  
  332.   
  333.     //cal parameter thetas  
  334.     for(int k = 0 ; k < 100000; k++)  
  335.     {  
  336.         int i = rand()%NUM_IMAGE;  
  337.         int j = rand()%NUM_BLOCK_FEATURES;  
  338.         theta1[j] = theta1[j] + weight[i]*0.01*(type[i] - logistic(theta1, features[i][block_row][block_col]))*features[i][block_row][block_col][j];  
  339.     }  
  340.   
  341.     for(int i = 0 ; i < NUM_IMAGE; i++)  
  342.     {  
  343.         double tmp = logistic(theta1, features[i][block_row][block_col]);  
  344.         if(tmp > 0.5 && fabs(type[i] - 1) < eps)  
  345.             rightnum1 += 1.0*weight[i];  
  346.         if(tmp < 0.5 && fabs(type[i] - 0) < eps)  
  347.             rightnum1 += 1.0*weight[i];  
  348.     }  
  349.     errorrate1 = 1 - rightnum1;  
  350.   
  351.     //calreverse  
  352.     for(int k = 0 ; k < 100000; k++)  
  353.     {  
  354.         int i = rand()%NUM_IMAGE;  
  355.         int j = rand()%NUM_BLOCK_FEATURES;  
  356.         theta2[j] = theta2[j] + weight[i]*0.01*(1- type[i] - logistic(theta2, features[i][block_row][block_col]))*features[i][block_row][block_col][j];  
  357.     }  
  358.   
  359.     for(int i = 0 ; i < NUM_IMAGE; i++)  
  360.     {  
  361.         double tmp = logistic(theta2, features[i][block_row][block_col]);  
  362.         if(tmp > 0.5 && fabs(type[i] - 0) < eps)  
  363.             rightnum2 += 1.0*weight[i];  
  364.         if(tmp < 0.5 && fabs(type[i] - 1) < eps)  
  365.             rightnum2 += 1.0*weight[i];  
  366.     }  
  367.     errorrate2 = 1 - rightnum2;  
  368.   
  369.     if(errorrate1 < errorrate2)  
  370.     {  
  371.         for(int i = 0 ; i < NUM_BLOCK_FEATURES; i++)  
  372.         {  
  373.             theta[i] = theta1[i];  
  374.         }  
  375.         isreverse = 0;  
  376.         errorrate = errorrate1 + eps;  
  377.     }  
  378.     else  
  379.     {  
  380.         for(int i = 0 ; i < NUM_BLOCK_FEATURES; i++)  
  381.         {  
  382.             theta[i] = theta2[i];  
  383.         }  
  384.         isreverse = 1;  
  385.         errorrate = errorrate2 + eps;  
  386.     }  
  387.     return;  
  388. }  
  389.   
  390. WeakClassifier trainClassifier()  
  391. {  
  392.     WeakClassifier ansclassifier;  
  393.     double theta[NUM_BLOCK_FEATURES];  
  394.     double errorrate = 1;  
  395.     int isreverse = 0;  
  396.     double best_theta[NUM_BLOCK_FEATURES];  
  397.     double best_errorrate = 1;  
  398.     int best_row = -1;  
  399.     int best_col = -1;  
  400.     int best_isreverse = 0;  
  401.   
  402.     //select best weak classifier  
  403.     for(int i = 0 ; i < NUM_BLOCK_ROWS; i++)  
  404.     {  
  405.         for(int j = 0 ; j < NUM_BLOCK_COLS; j++)  
  406.         {  
  407.             trainLogisticRegression(i,j,theta,errorrate, isreverse);  
  408.               
  409.             if(errorrate < 0)  
  410.             {  
  411.                 printf("Wrong errorrate < 0 : %lf", errorrate);  
  412.                 abort();  
  413.             }  
  414.   
  415.             if(errorrate < best_errorrate)  
  416.             {  
  417.                 for(int tempi = 0 ; tempi < NUM_BLOCK_FEATURES; tempi++)  
  418.                 {  
  419.                     best_theta[tempi] = theta[tempi];  
  420.                 }  
  421.                 best_errorrate = errorrate;  
  422.                 best_row = i;  
  423.                 best_col = j;  
  424.                 best_isreverse = isreverse;  
  425.             }  
  426.         }  
  427.     }  
  428.   
  429.     if(best_errorrate > 0.5)  
  430.     {  
  431.         printf("The best_errorrate is greater than 0.5.\n");  
  432.         abort();  
  433.     }  
  434.   
  435.     //set parameters;  
  436.     ansclassifier._alpha = 1.0/2*std::log((1-best_errorrate)/best_errorrate);  
  437.     ansclassifier._error = best_errorrate;  
  438.     ansclassifier._index_col = best_col;  
  439.     ansclassifier._index_row = best_row;  
  440.     ansclassifier._isreverse = best_isreverse;  
  441.     for(int i = 0 ; i < NUM_BLOCK_FEATURES; i++) ansclassifier._theta[i] = best_theta[i];  
  442.   
  443.     return ansclassifier;  
  444. }  
  445.   
  446. int calByStrongClassifier(double x[NUM_BLOCK_ROWS][NUM_BLOCK_COLS][NUM_BLOCK_FEATURES])  
  447. {  
  448.     double ans = 0;  
  449.     for(int i = 0 ; i < NUM_WEAKCLASSIFIER; i++)  
  450.     {  
  451.         ans += weakClassifier[i]._alpha * weakClassifier[i].cal(x);  
  452.     }  
  453.     if(ans > 0)  
  454.         return 1;  
  455.     else  
  456.         return -1;  
  457. }  
  458.   
  459.   
  460.   
  461. /* 
  462. size: 128*64; 
  463. type: CV_8UC1; 
  464. Block大小为18*18; 
  465. Cell大小为6*6; 
  466. Block在检测窗口中上下移动尺寸为6*6; 
  467. 1个cell的梯度直方图化成9个bin; 
  468. 滑动窗口在检测图片中滑动的尺寸为6*6; 
  469. */  
  470.   
  471. int main()  
  472. {  
  473.     char filename[100];  
  474.     IplImage* inputImage = NULL;  
  475.     clock_t timecount = clock();  
  476.   
  477.     //load posimage  
  478.     for(int i = 0 ; i < NUM_POSIMAGE; i++)  
  479.     {  
  480.         posfilename(i+1 ,filename);  
  481.           
  482.         //load grey image: set the parameter to 0;  
  483.         inputImage = cvLoadImage(filename, 0);  
  484.           
  485.           
  486.         //cal features;  
  487.         Mat inputMat(inputImage);  
  488.         calHogFeatures(inputMat, features[i]);  
  489.         type[i] = 1;  
  490.         y[i] = 1;  
  491.         //printf("%d \n", inputMat.cols);  
  492.   
  493.         //release memory  
  494.         inputMat.release();  
  495.         cvReleaseImage(&inputImage);  
  496.         inputImage = NULL;  
  497.     }  
  498.   
  499.     printf("The feature process of pos-image have done in %d second.\n", (clock()-timecount)/1000);  
  500.     timecount = clock();  
  501.   
  502.     //load neg images  
  503.     for(int i = 0; i < NUM_NEGIMAGE; i++)  
  504.     {  
  505.         negfilename(i+1, filename);  
  506.   
  507.         //load grey image: set the parameter to 0;  
  508.         inputImage = cvLoadImage(filename, 0);  
  509.         type[NUM_POSIMAGE+i] = 0;  
  510.         y[NUM_POSIMAGE+i] = -1;  
  511.   
  512.         Mat inputMat(inputImage);  
  513.         calHogFeatures(inputMat, features[NUM_POSIMAGE+i]);  
  514.           
  515.         //release memory  
  516.         inputMat.release();  
  517.         cvReleaseImage(&inputImage);  
  518.         inputImage = NULL;  
  519.     }  
  520.   
  521.     printf("The feature process of neg-image have done in %d second.\n", (clock()-timecount)/1000);  
  522.     timecount = clock();  
  523.   
  524.     //init weight array  
  525.     for(int i = 0 ; i < NUM_IMAGE; i++)  
  526.     {  
  527.         weight[i] = 1.0/NUM_IMAGE;  
  528.     }  
  529.   
  530.     //freopen  
  531.     freopen("HOG_CLASSIFIER.txt""w", stdout);  
  532.   
  533.     //print number of weakclassifiers;  
  534.     printf("%d\n", NUM_WEAKCLASSIFIER);  
  535.   
  536.     //adaboost framework  
  537.     for(int classifierindex = 0 ; classifierindex < NUM_WEAKCLASSIFIER; classifierindex++)  
  538.     {  
  539.         weakClassifier[classifierindex] = trainClassifier();  
  540.   
  541.         double error = weakClassifier[classifierindex]._error;  
  542.         double alpha = weakClassifier[classifierindex]._alpha;  
  543.   
  544.         //printf("%d classifier: %lf ====\n",classifierindex, error);  
  545.         //printf("_index_row %d _index_col %d\n", weakClassifier[classifierindex]._index_row, weakClassifier[classifierindex]._index_col);  
  546.   
  547.         double identitysum = 0;  
  548.         for(int sampleindex = 0 ; sampleindex < NUM_IMAGE; sampleindex++)  
  549.         {  
  550.             weight[sampleindex] *= std::exp(-alpha*y[sampleindex]*weakClassifier[classifierindex].cal(features[sampleindex]));  
  551.             identitysum += weight[sampleindex];  
  552.         }  
  553.   
  554.         //reweight  
  555.         for(int sampleindex = 0 ; sampleindex < NUM_IMAGE; sampleindex++)  
  556.         {  
  557.             weight[sampleindex] /= identitysum;  
  558.         }  
  559.   
  560.         weakClassifier[classifierindex].print();  
  561.     }  
  562.   
  563.     freopen("CON""w", stdout);  
  564.     int rightnum = 0;  
  565.     for(int testindex = 0 ;testindex < NUM_TESTIMAGE; testindex ++)  
  566.     {  
  567.         //posfilename(testindex+1, filename);  
  568.         testfilename(testindex+1, filename);  
  569.         inputImage = cvLoadImage(filename, 0);  
  570.   
  571.         double testfeatures[NUM_BLOCK_ROWS][NUM_BLOCK_COLS][NUM_BLOCK_FEATURES];  
  572.         memset(testfeatures, 0, sizeof(testfeatures));  
  573.   
  574.         Mat inputMat(inputImage);  
  575.         calHogFeatures(inputMat, testfeatures);  
  576.   
  577.         if(calByStrongClassifier(testfeatures) == 1)  
  578.         {  
  579.             rightnum++;  
  580.             //printf("Yes\n");  
  581.         }  
  582.         else  
  583.             //printf("No\n");  
  584.   
  585.         inputMat.release();  
  586.     }  
  587.     printf("Accuracy: %d\n", rightnum);  
  588. }  

//测试数据是网上流行的128*64灰度行人图像数据。

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值