svm 行人识别 训练 基于Opencv

来源:互联网 发布:看图识车软件 编辑:程序博客网 时间:2024/06/09 17:18

参考 http://blog.csdn.net/zouwen198317/article/details/8198578


写得很好的svm+ hog 的分类器训练


[cpp] view plaincopy
  1. class Mysvm: public CvSVM  
  2. {  
  3. public:  
  4.     int get_alpha_count()  
  5.     {  
  6.         return this->sv_total;  
  7.     }  
  8.   
  9.     int get_sv_dim()  
  10.     {  
  11.         return this->var_all;  
  12.     }  
  13.   
  14.     int get_sv_count()  
  15.     {  
  16.         return this->decision_func->sv_count;  
  17.     }  
  18.   
  19.     double* get_alpha()  
  20.     {  
  21.         return this->decision_func->alpha;  
  22.     }  
  23.   
  24.     float** get_sv()  
  25.     {  
  26.         return this->sv;  
  27.     }  
  28.   
  29.     float get_rho()  
  30.     {  
  31.         return this->decision_func->rho;  
  32.     }  
  33. };  
  34.   
  35. void Train()  
  36. {  
  37.     char classifierSavePath[256] = "c:/pedestrianDetect-peopleFlow.txt";  
  38.   
  39.     string positivePath = "E:\\pictures\\train1\\pos\\";  
  40.     string negativePath = "E:\\pictures\\train1\\neg\\";  
  41.   
  42.     int positiveSampleCount = 4900;  
  43.     int negativeSampleCount = 6192;  
  44.     int totalSampleCount = positiveSampleCount + negativeSampleCount;  
  45.   
  46.     cout<<"//////////////////////////////////////////////////////////////////"<<endl;  
  47.     cout<<"totalSampleCount: "<<totalSampleCount<<endl;  
  48.     cout<<"positiveSampleCount: "<<positiveSampleCount<<endl;  
  49.     cout<<"negativeSampleCount: "<<negativeSampleCount<<endl;  
  50.   
  51.     CvMat *sampleFeaturesMat = cvCreateMat(totalSampleCount , 1764, CV_32FC1);  
  52.     //64*128的训练样本,该矩阵将是totalSample*3780,64*64的训练样本,该矩阵将是totalSample*1764  
  53.     cvSetZero(sampleFeaturesMat);    
  54.     CvMat *sampleLabelMat = cvCreateMat(totalSampleCount, 1, CV_32FC1);//样本标识    
  55.     cvSetZero(sampleLabelMat);    
  56.   
  57.     cout<<"************************************************************"<<endl;  
  58.     cout<<"start to training positive samples..."<<endl;  
  59.   
  60.     char positiveImgName[256];  
  61.     string path;  
  62.     for(int i=0; i<positiveSampleCount; i++)    
  63.     {    
  64.         memset(positiveImgName, '\0', 256*sizeof(char));  
  65.         sprintf(positiveImgName, "%d.jpg", i);  
  66.         int len = strlen(positiveImgName);  
  67.         string tempStr = positiveImgName;  
  68.         path = positivePath + tempStr;  
  69.   
  70.         cv::Mat img = cv::imread(path);  
  71.         if( img.data == NULL )  
  72.         {  
  73.             cout<<"positive image sample load error: "<<i<<" "<<path<<endl;  
  74.             system("pause");  
  75.             continue;  
  76.         }  
  77.   
  78.         cv::HOGDescriptor hog(cv::Size(64,64), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9);  
  79.         vector<float> featureVec;   
  80.   
  81.         hog.compute(img, featureVec, cv::Size(8,8));    
  82.         int featureVecSize = featureVec.size();  
  83.   
  84.         for (int j=0; j<featureVecSize; j++)    
  85.         {         
  86.             CV_MAT_ELEM( *sampleFeaturesMat, float, i, j ) = featureVec[j];   
  87.         }    
  88.         sampleLabelMat->data.fl[i] = 1;  
  89.     }  
  90.     cout<<"end of training for positive samples..."<<endl;  
  91.   
  92.     cout<<"*********************************************************"<<endl;  
  93.     cout<<"start to train negative samples..."<<endl;  
  94.   
  95.     char negativeImgName[256];  
  96.     for (int i=0; i<negativeSampleCount; i++)  
  97.     {    
  98.         memset(negativeImgName, '\0', 256*sizeof(char));  
  99.         sprintf(negativeImgName, "%d.jpg", i);  
  100.         path = negativePath + negativeImgName;  
  101.         cv::Mat img = cv::imread(path);  
  102.         if(img.data == NULL)  
  103.         {  
  104.             cout<<"negative image sample load error: "<<path<<endl;  
  105.             continue;  
  106.         }  
  107.   
  108.         cv::HOGDescriptor hog(cv::Size(64,64), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9);    
  109.         vector<float> featureVec;   
  110.   
  111.         hog.compute(img,featureVec,cv::Size(8,8));//计算HOG特征  
  112.         int featureVecSize = featureVec.size();    
  113.   
  114.         for ( int j=0; j<featureVecSize; j ++)    
  115.         {    
  116.             CV_MAT_ELEM( *sampleFeaturesMat, float, i + positiveSampleCount, j ) = featureVec[ j ];  
  117.         }    
  118.   
  119.         sampleLabelMat->data.fl[ i + positiveSampleCount ] = -1;  
  120.     }    
  121.   
  122.     cout<<"end of training for negative samples..."<<endl;  
  123.     cout<<"********************************************************"<<endl;  
  124.     cout<<"start to train for SVM classifier..."<<endl;  
  125.   
  126.     CvSVMParams params;    
  127.     params.svm_type = CvSVM::C_SVC;    
  128.     params.kernel_type = CvSVM::LINEAR;    
  129.     params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 1000, FLT_EPSILON);  
  130.     params.C = 0.01;  
  131.   
  132.     Mysvm svm;  
  133.     svm.train( sampleFeaturesMat, sampleLabelMat, NULL, NULL, params ); //用SVM线性分类器训练  
  134.     svm.save(classifierSavePath);  
  135.   
  136.     cvReleaseMat(&sampleFeaturesMat);  
  137.     cvReleaseMat(&sampleLabelMat);  
  138.   
  139.     int supportVectorSize = svm.get_support_vector_count();  
  140.     cout<<"support vector size of SVM:"<<supportVectorSize<<endl;  
  141.     cout<<"************************ end of training for SVM ******************"<<endl;  
  142.   
  143.     CvMat *sv,*alp,*re;//所有样本特征向量   
  144.     sv  = cvCreateMat(supportVectorSize , 1764, CV_32FC1);  
  145.     alp = cvCreateMat(1 , supportVectorSize, CV_32FC1);  
  146.     re  = cvCreateMat(1 , 1764, CV_32FC1);  
  147.     CvMat *res  = cvCreateMat(1 , 1, CV_32FC1);  
  148.   
  149.     cvSetZero(sv);  
  150.     cvSetZero(re);  
  151.     
  152.     for(int i=0; i<supportVectorSize; i++)  
  153.     {  
  154.         memcpy( (float*)(sv->data.fl+i*1764), svm.get_support_vector(i), 1764*sizeof(float));      
  155.     }  
  156.   
  157.     double* alphaArr = svm.get_alpha();  
  158.     int alphaCount = svm.get_alpha_count();  
  159.   
  160.     for(int i=0; i<supportVectorSize; i++)  
  161.     {  
  162.         alp->data.fl[i] = alphaArr[i];  
  163.     }  
  164.     cvMatMul(alp, sv, re);  
  165.   
  166.     int posCount = 0;  
  167.     for (int i=0; i<1764; i++)  
  168.     {  
  169.         re->data.fl[i] *= -1;  
  170.     }  
  171.   
  172.     FILE* fp = fopen("c:/hogSVMDetector-peopleFlow.txt","wb");  
  173.     if( NULL == fp )  
  174.     {  
  175.         return 1;  
  176.     }  
  177.     for(int i=0; i<1764; i++)  
  178.     {  
  179.         fprintf(fp,"%f \n",re->data.fl[i]);  
  180.     }  
  181.     float rho = svm.get_rho();  
  182.     fprintf(fp, "%f", rho);  
  183.     cout<<"c:/hogSVMDetector.txt 保存完毕"<<endl;//保存HOG能识别的分类器  
  184.     fclose(fp);  
  185.   
  186.     return 1;  
  187. }  


检测代码那和opencv的差不多了

[cpp] view plaincopy
  1. void Detect()  
  2. {  
  3.     CvCapture* cap = cvCreateFileCapture("E:\\02.avi");  
  4.     if (!cap)  
  5.     {  
  6.         cout<<"avi file load error..."<<endl;  
  7.         system("pause");  
  8.         exit(-1);  
  9.     }  
  10.   
  11.     vector<float> x;  
  12.     ifstream fileIn("c:/hogSVMDetector-peopleFlow.txt", ios::in);  
  13.     float val = 0.0f;  
  14.     while(!fileIn.eof())  
  15.     {  
  16.         fileIn>>val;  
  17.         x.push_back(val);  
  18.     }  
  19.     fileIn.close();  
  20.   
  21.     vector<cv::Rect>  found;  
  22.     cv::HOGDescriptor hog(cv::Size(64,64), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9);  
  23.     hog.setSVMDetector(x);  
  24.   
  25.     IplImage* img = NULL;  
  26.     cvNamedWindow("img", 0);  
  27.     while(img=cvQueryFrame(cap))  
  28.     {  
  29.         hog.detectMultiScale(img, found, 0, cv::Size(8,8), cv::Size(32,32), 1.05, 2);  
  30.         if (found.size() > 0)  
  31.         {  
  32.   
  33.             for (int i=0; i<found.size(); i++)  
  34.             {  
  35.                 CvRect tempRect = cvRect(found[i].x, found[i].y, found[i].width, found[i].height);  
  36.   
  37.                 cvRectangle(img, cvPoint(tempRect.x,tempRect.y),  
  38.                     cvPoint(tempRect.x+tempRect.width,tempRect.y+tempRect.height),CV_RGB(255,0,0), 2);  
  39.             }  
  40.         }  
  41.     }  
  42.     cvReleaseCapture(&cap);  
  43. }  


svm + hog算法,做行人识别,效果其实不错的,和haar +adaboost应该有得比,在dm3730 arm + dsp 双核的,我实现了两种方法,感觉svm+hog速度慢了点,haar + adaboost 速度还可以,做了行人识别, 分类器自己训练的,18个 stage那样,640X480.  如果在DM8148或 Dm8168做,应该很实用了。如果是纯的dsp,以前在dm6437上做,感觉速度也不怎么行。目前正在研究dm8148的架构,和omap或Dm3730差不多,多了一些OpenMax之类,估计是照顾android开发用户,我还是喜欢直接基于linux开发,代码直接是c的,新的opencv多了一些 vector之类,估计ti的dsp环境可能不支持。


0 0
原创粉丝点击