vs2005hog+svm,vs2008 hog+svm 中的computer()函数出错折磨我好几天


  1. #include "stdafx.h"  
  2. #include <ctype.h>  
  3. #include "cv.h"       
  4. #include "highgui.h"      
  5. #include <ml.h>       
  6. #include <iostream>       
  7. #include <fstream>       
  8. #include <string.h>       
  9. #include <vector>   
  10. #include <math.h>  
  11. #include <stdio.h>  
  12. #include "cvaux.h"  
  13. using namespace cv;      
  14. using namespace std;  
  15.   
  16. class Mysvm: public CvSVM  
  17. {  
  18. public:  
  19.     int get_alpha_count()  
  20.     {  
  21.         return this->sv_total;  
  22.     }  
  23.   
  24.     int get_sv_dim()  
  25.     {  
  26.         return this->var_all;  
  27.     }  
  28.   
  29.     int get_sv_count()  
  30.     {  
  31.         return this->decision_func->sv_count;  
  32.     }  
  33.   
  34.     double* get_alpha()  
  35.     {  
  36.         return this->decision_func->alpha;  
  37.     }  
  38.   
  39.     float** get_sv()  
  40.     {  
  41.         return this->sv;  
  42.     }  
  43.   
  44.     float get_rho()  
  45.     {  
  46.         return this->decision_func->rho;  
  47.     }  
  48. };  
  49.   
  50. void Train()  
  51. {  
  52.     char classifierSavePath[256] = "E:\\My Documents1\\hogsvm\\hogsvm\\liu.xml";  
  53.   
  54. //  string positivePath = "E:\\My Documents1\\hogsvm\\hogsvm\\pos296\\";  
  55. //  string negativePath = "E:\\My Documents1\\hogsvm\\hogsvm\\neg297\\";  
  56.   
  57.     int positiveSampleCount = 18;  
  58.     int negativeSampleCount = 18;  
  59.     int totalSampleCount = positiveSampleCount + negativeSampleCount;  
  60.   
  61.     cout<<"//"<<endl;  
  62.     cout<<"totalSampleCount: "<<totalSampleCount<<endl;  
  63.     cout<<"positiveSampleCount: "<<positiveSampleCount<<endl;  
  64.     cout<<"negativeSampleCount: "<<negativeSampleCount<<endl;  
  65.   
  66.     int totalCols = /*3780*/1764;  
  67.     CvMat *sampleFeaturesMat = cvCreateMat(totalSampleCount , totalCols, CV_32FC1);  
  68.     //64*128的训练样本,该矩阵将是totalSample*3780,64*64的训练样本,该矩阵将是totalSample*1764  
  69.     cvSetZero(sampleFeaturesMat);    
  70.     CvMat *sampleLabelMat = cvCreateMat(totalSampleCount, 1, CV_32FC1);//样本标识    
  71.     cvSetZero(sampleLabelMat);    
  72.   
  73.     cout<<"************************************************************"<<endl;  
  74.     cout<<"start to training positive samples..."<<endl;  
  75.   
  76.     //char positiveImgName[256];  
  77.     //HOGDescriptor *hog=NULL;  
  78.   
  79.     string path;     
  80.     ifstream positive_data( "E:\\My Documents1\\hogsvm\\hogsvm\\pos296\\pos.txt" );   
  81.     int count = 0;  
  82.     while( positive_data )      
  83.     {      
  84.         if( getline( positive_data, path) )    
  85.         {  
  86.             cv::Mat img = cv::imread(path);  
  87.             if( img.data == NULL )  
  88.             {  
  89.                 cout<<"positive image sample load error: "<<count<<" "<<path<<endl;  
  90.                 system("pause");  
  91.                 continue;  
  92.             }  
  93.   
  94.             cv::HOGDescriptor hog(cv::Size(64,64), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9);  
  95.             //cv::HOGDescriptor *hog=new HOGDescriptor(cv::Size(64,64), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9);  
  96.             <span style="background-color: rgb(51, 204, 255);">vector<float> featureVec(1764);//此句,去掉(1764),在vs2008上正确,但是在vs2005上就出错,在vs2005上加入(1764就正确运行)   
  97. </span>         //vector<float> featureVec = new std::vector<float>();   
  98.             hog->compute(img, featureVec, cv::Size(8,8),cv::Size(0,0));   
  99.             //hog.compute(img, featureVec, cvSize(8,8));    
  100.   
  101.             int featureVecSize = featureVec.size();  
  102.   
  103.             //for (int j=0; j<featureVecSize; j++)  //todo  
  104.             for (int j = 0; j < totalCols; j++)  
  105.             {      
  106.                 CV_MAT_ELEM( *sampleFeaturesMat, float, count, j ) = featureVec[j];   
  107.             }    
  108.             sampleLabelMat->data.fl[count] = 1;  
  109.             count ++;/**/  
  110.         }  
  111.     }  
  112.     positive_data.close();  
  113.     cout<<"end of training for positive samples...["<<count<<"]"<<endl;  
  114.   
  115.     cout<<"*********************************************************"<<endl;  
  116.     cout<<"start to train negative samples..."<<endl;  
  117.   
  118.     ifstream negative_data("E:\\My Documents1\\hogsvm\\hogsvm\\neg297\\neg.txt");  
  119.     count = 0;  
  120.     while(negative_data)  
  121.     {  
  122.         if( getline( negative_data, path ))    
  123.         {  
  124.             cv::Mat img = cv::imread(path);  
  125.             if(img.data == NULL)  
  126.             {  
  127.                 cout<<"negative image sample load error: "<<path<<endl;  
  128.                 continue;  
  129.             }  
  130.   
  131.             cv::HOGDescriptor hog(cv::Size(64,64), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9);    
  132.             vector<float> featureVec(1764);   
  133.   
  134.             hog.compute(img,featureVec,cv::Size(8,8));//计算HOG特征  
  135.             int featureVecSize = featureVec.size();    
  136.   
  137.             //for ( int j=0; j<featureVecSize; j ++)  //todo  
  138.             for (int j = 0; j< totalCols; j++)  
  139.             {    
  140.                 CV_MAT_ELEM( *sampleFeaturesMat, float, count + positiveSampleCount, j ) = featureVec[ j ];  
  141.             }    
  142.   
  143.             sampleLabelMat->data.fl[ count + positiveSampleCount ] = -1;  
  144.             count ++;  
  145.         }  
  146.     }    
  147.   
  148.     negative_data.close();  
  149.   
  150.     cout<<"end of training for negative samples...["<<count<<"]"<<endl;  
  151.     cout<<"********************************************************"<<endl;  
  152.     cout<<"start to train for SVM classifier..."<<endl;  
  153.   
  154.     CvSVMParams params;    
  155.     params.svm_type = CvSVM::C_SVC;    
  156.     params.kernel_type = CvSVM::LINEAR;    
  157.     params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 1000, FLT_EPSILON);  
  158.     params.C = 0.01;  
  159.   
  160.     Mysvm svm;  
  161.     svm.train( sampleFeaturesMat, sampleLabelMat, NULL, NULL, params ); //用SVM线性分类器训练//sampleFeaturesMat保存各样本的特征值,sampleLabelMat保存图片类型   
  162.   
  163.     svm.save(classifierSavePath);  
  164.   
  165.     cvReleaseMat(&sampleFeaturesMat);  
  166.     cvReleaseMat(&sampleLabelMat);  
  167.   
  168.     int supportVectorSize = svm.get_support_vector_count();//获得支持向量的个数  
  169.     cout<<"support vector size of SVM:"<<supportVectorSize<<endl;  
  170.     cout<<"************************ end of training for SVM ******************"<<endl;  
  171.   
  172.     CvMat *sv,*alp,*re;//所有样本特征向量   
  173.     sv  = cvCreateMat(supportVectorSize , totalCols, CV_32FC1);  
  174.     alp = cvCreateMat(1 , supportVectorSize, CV_32FC1);  
  175.     re  = cvCreateMat(1 , totalCols, CV_32FC1);  
  176.     CvMat *res  = cvCreateMat(1 , 1, CV_32FC1);  
  177.   
  178.     cvSetZero(sv);  
  179.     cvSetZero(re);  
  180.   
  181.     for(int i=0; i<supportVectorSize; i++)  
  182.     {  
  183.         memcpy( (float*)(sv->data.fl+i*totalCols), svm.get_support_vector(i), totalCols*sizeof(float)); //get_support_vector获得对应的索引编号的支持向量  
  184.     }  
  185.   
  186.     double* alphaArr = svm.get_alpha();  
  187.     int alphaCount = svm.get_alpha_count();  
  188.     cout<<"alpharr"<<*alphaArr<<endl;  
  189.   
  190.     for(int i=0; i<supportVectorSize; i++)  
  191.     {  
  192.         alp->data.fl[i] = alphaArr[i];  
  193.     }  
  194.     cvMatMul(alp, sv, re);  
  195.   
  196.     int posCount = 0;  
  197.     for (int i=0; i<totalCols; i++)  
  198.     {  
  199.         re->data.fl[i] *= -1;  
  200.     }  
  201.   
  202.     FILE* fp = fopen("E:\\My Documents1\\hogsvm\\hogsvm\\num.txt","wb");  
  203.     if( NULL == fp )  
  204.     {  
  205.         return;  
  206.     }  
  207.     for(int i=0; i<totalCols; i++)  
  208.     {  
  209.         fprintf(fp,"%f \n",re->data.fl[i]);  
  210.     }  
  211.     float rho = svm.get_rho();  
  212.     fprintf(fp, "%f", rho);  
  213.     cout<<"E:\\My Documents1\\hogsvm\\hogsvm\\num.txt 保存完毕"<<endl;//保存HOG能识别的分类器  
  214.     fclose(fp);  
  215.   
  216.     return;  
  217. }  
  218.   
  219. /*void Detect()  
  220. {  
  221. CvCapture* cap = cvCreateFileCapture("D:\\people.avi");  
  222. //CvCapture* cap = cvCreateFileCapture("E:/My Documents1/detecthog+svm/detecthog+svm/00.jpg");  
  223. if (!cap)  
  224. {  
  225. cout<<"avi file load error..."<<endl;  
  226. system("pause");  
  227. exit(-1);  
  228. }  
  229.   
  230. vector<float> x;  
  231. ifstream fileIn("D:/image/WorkTest/hogSVMDetector-peopleFlow.txt", ios::in);  
  232. float val = 0.0f;  
  233. while(!fileIn.eof())  
  234. {  
  235. fileIn>>val;  
  236. x.push_back(val);  
  237. }  
  238. fileIn.close();  
  239.   
  240. vector<cv::Rect>  found;  
  241. cv::HOGDescriptor hog(cv::Size(20,20), cv::Size(10,10), cv::Size(5,5), cv::Size(5,5), 9);  
  242. hog.setSVMDetector(x);  
  243.   
  244. IplImage* img = NULL;  
  245. cvNamedWindow("img", 0);  
  246. while(img=cvQueryFrame(cap))  
  247. {  
  248. hog.detectMultiScale(img, found, 0, cv::Size(5,5), cv::Size(10,10), 1.05, 2);  
  249. if (found.size() > 0)  
  250. {  
  251.   
  252. for (int i=0; i<found.size(); i++)  
  253. {  
  254. CvRect tempRect = cvRect(found[i].x, found[i].y, found[i].width, found[i].height);  
  255.   
  256. cvRectangle(img, cvPoint(tempRect.x,tempRect.y),  
  257. cvPoint(tempRect.x+tempRect.width,tempRect.y+tempRect.height),CV_RGB(255,0,0), 2);  
  258. }  
  259. }  
  260. }  
  261. cvReleaseCapture(&cap);  
  262. }  
  263. */  
  264.   
  265.   
  266. int face()    
  267. {    
  268.     Mat img;    
  269.     FILE* f = 0;    
  270.     char _filename[1024] = "3.jpg";   
  271.     char _filePath[1024];  
  272.   
  273.     sprintf(_filePath, "E:\\My Documents1\\hogsvm\\hogsvm\\%s", _filename);  
  274.     img = imread(_filePath);    
  275.   
  276.     if( !img.data )     
  277.     {      
  278.         return -1;     
  279.     }    
  280.   
  281.     cv::HOGDescriptor hog(cv::Size(64,64), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9);//("D:\\pedestrianDetect-peopleFlow.xml");    
  282.     //hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());//得到检测器   
  283.     //vector<float> detector;// = load_lear_model("D:\\hogSVMDetector-peopleFlow.txt");  
  284.     //CvLatentSvmDetector* detector = cvLoadLatentSvmDetector("result.xml");  
  285.     vector<float> detector;  
  286.   
  287.     ifstream detector_data("E:\\My Documents1\\hogsvm\\hogsvm\\num.txt", ios::in);  
  288.     int count = 0;  
  289.     string buf;  
  290.     float tmpFlaot;  
  291.     while(!detector_data.eof())  
  292.     {  
  293.         detector_data >> tmpFlaot;  
  294.         detector.push_back(tmpFlaot);  
  295.         count ++;  
  296.     }  
  297.     hog.setSVMDetector(detector);  
  298.     namedWindow("people detector", 1);   
  299.   
  300.     for(;;)    
  301.     {    
  302.         char* filename = _filename;    
  303.         if(f)    
  304.         {    
  305.             if(!fgets(filename, (int)sizeof(_filename)-2, f))    
  306.                 break;    
  307.             //while(*filename && isspace(*filename))     
  308.             //  ++filename;     
  309.             if(filename[0] == '#')    
  310.                 continue;    
  311.             int l = strlen(filename);    
  312.             while(l > 0 && isspace(filename[l-1]))    
  313.                 --l;    
  314.             filename[l] = '\0';    
  315.             img = imread(filename);    
  316.         }    
  317.         printf("%s:\n", filename);    
  318.         if(!img.data)    
  319.             continue;    
  320.   
  321.         fflush(stdout);    
  322.         vector<Rect> found, found_filtered;    
  323.         double t = (double)getTickCount();    
  324.         // run the detector with default parameters. to get a higher hit-rate     
  325.         // (and more false alarms, respectively), decrease the hitThreshold and     
  326.         // groupThreshold (set groupThreshold to 0 to turn off the grouping completely).     
  327.         hog.detectMultiScale(img, found, 0, Size(8,8), Size(0,0), 1.05, 2);    
  328.         t = (double)getTickCount() - t;    
  329.         printf("tdetection time = %gms\n", t*1000./cv::getTickFrequency());    
  330.         size_t i, j;    
  331.         for( i = 0; i < found.size(); i++ )    
  332.         {    
  333.             Rect r = found[i];    
  334.             for( j = 0; j < found.size(); j++ )    
  335.                 if( j != i && (r & found[j]) == r)    
  336.                     break;    
  337.             if( j == found.size() )    
  338.                 found_filtered.push_back(r);    
  339.         }   
  340.         count = 0;  
  341.         for( i = 0; i < found_filtered.size(); i++ )    
  342.         {    
  343.             Rect r = found_filtered[i];    
  344.             // the HOG detector returns slightly larger rectangles than the real objects.     
  345.             // so we slightly shrink the rectangles to get a nicer output.     
  346.             r.x += cvRound(r.width*0.1);    
  347.             r.width = cvRound(r.width*0.8);    
  348.             r.y += cvRound(r.height*0.07);    
  349.             r.height = cvRound(r.height*0.8);    
  350.             rectangle(img, r.tl(), r.br(), cv::Scalar(255,0,0), 3);    
  351.             count++;  
  352.         }   
  353.         cout<<"all count round:"<<count<<endl;  
  354.         imshow("detector", img);    
  355.         int c = cvWaitKey(0) & 255;    
  356.         if( c == 'q' || c == 'Q' || !f)    
  357.             break;    
  358.     }    
  359.     if(f)    
  360.         fclose(f);    
  361.     return 0;    
  362. }    
  363.   
  364.   
  365. int main()  
  366. {     
  367.     Train();  
  368.     //Detect();  
  369.     face();  
  370.   
  371.     return 0;  
  372. }  
#include "stdafx.h"
#include <ctype.h>
#include "cv.h"     
#include "highgui.h"    
#include <ml.h>     
#include <iostream>     
#include <fstream>     
#include <string.h>     
#include <vector> 
#include <math.h>
#include <stdio.h>
#include "cvaux.h"
using namespace cv;    
using namespace std;

class Mysvm: public CvSVM
{
public:
	int get_alpha_count()
	{
		return this->sv_total;
	}

	int get_sv_dim()
	{
		return this->var_all;
	}

	int get_sv_count()
	{
		return this->decision_func->sv_count;
	}

	double* get_alpha()
	{
		return this->decision_func->alpha;
	}

	float** get_sv()
	{
		return this->sv;
	}

	float get_rho()
	{
		return this->decision_func->rho;
	}
};

void Train()
{
	char classifierSavePath[256] = "E:\\My Documents1\\hogsvm\\hogsvm\\liu.xml";

// 	string positivePath = "E:\\My Documents1\\hogsvm\\hogsvm\\pos296\\";
// 	string negativePath = "E:\\My Documents1\\hogsvm\\hogsvm\\neg297\\";

	int positiveSampleCount = 18;
	int negativeSampleCount = 18;
	int totalSampleCount = positiveSampleCount + negativeSampleCount;

	cout<<"//"<<endl;
	cout<<"totalSampleCount: "<<totalSampleCount<<endl;
	cout<<"positiveSampleCount: "<<positiveSampleCount<<endl;
	cout<<"negativeSampleCount: "<<negativeSampleCount<<endl;

	int totalCols = /*3780*/1764;
	CvMat *sampleFeaturesMat = cvCreateMat(totalSampleCount , totalCols, CV_32FC1);
	//64*128的训练样本,该矩阵将是totalSample*3780,64*64的训练样本,该矩阵将是totalSample*1764
	cvSetZero(sampleFeaturesMat);  
	CvMat *sampleLabelMat = cvCreateMat(totalSampleCount, 1, CV_32FC1);//样本标识  
	cvSetZero(sampleLabelMat);  

	cout<<"************************************************************"<<endl;
	cout<<"start to training positive samples..."<<endl;

	//char positiveImgName[256];
	//HOGDescriptor *hog=NULL;

	string path;   
	ifstream positive_data( "E:\\My Documents1\\hogsvm\\hogsvm\\pos296\\pos.txt" ); 
	int count = 0;
	while( positive_data )    
	{    
		if( getline( positive_data, path) )  
		{
			cv::Mat img = cv::imread(path);
			if( img.data == NULL )
			{
				cout<<"positive image sample load error: "<<count<<" "<<path<<endl;
				system("pause");
				continue;
			}

			cv::HOGDescriptor hog(cv::Size(64,64), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9);
			//cv::HOGDescriptor *hog=new HOGDescriptor(cv::Size(64,64), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9);
			vector<float> featureVec(1764);//此句,去掉(1764),在vs2008上正确,但是在vs2005上就出错,在vs2005上加入(1764就正确运行) 
			//vector<float> featureVec = new std::vector<float>(); 
			hog->compute(img, featureVec, cv::Size(8,8),cv::Size(0,0)); 
			//hog.compute(img, featureVec, cvSize(8,8));  

			int featureVecSize = featureVec.size();

			//for (int j=0; j<featureVecSize; j++)  //todo
			for (int j = 0; j < totalCols; j++)
			{    
				CV_MAT_ELEM( *sampleFeaturesMat, float, count, j ) = featureVec[j]; 
			}  
			sampleLabelMat->data.fl[count] = 1;
			count ++;/**/
		}
	}
	positive_data.close();
	cout<<"end of training for positive samples...["<<count<<"]"<<endl;

	cout<<"*********************************************************"<<endl;
	cout<<"start to train negative samples..."<<endl;

	ifstream negative_data("E:\\My Documents1\\hogsvm\\hogsvm\\neg297\\neg.txt");
	count = 0;
	while(negative_data)
	{
		if( getline( negative_data, path ))  
		{
			cv::Mat img = cv::imread(path);
			if(img.data == NULL)
			{
				cout<<"negative image sample load error: "<<path<<endl;
				continue;
			}

			cv::HOGDescriptor hog(cv::Size(64,64), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9);  
			vector<float> featureVec(1764); 

			hog.compute(img,featureVec,cv::Size(8,8));//计算HOG特征
			int featureVecSize = featureVec.size();  

			//for ( int j=0; j<featureVecSize; j ++)  //todo
			for (int j = 0; j< totalCols; j++)
			{  
				CV_MAT_ELEM( *sampleFeaturesMat, float, count + positiveSampleCount, j ) = featureVec[ j ];
			}  

			sampleLabelMat->data.fl[ count + positiveSampleCount ] = -1;
			count ++;
		}
	}  

	negative_data.close();

	cout<<"end of training for negative samples...["<<count<<"]"<<endl;
	cout<<"********************************************************"<<endl;
	cout<<"start to train for SVM classifier..."<<endl;

	CvSVMParams params;  
	params.svm_type = CvSVM::C_SVC;  
	params.kernel_type = CvSVM::LINEAR;  
	params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 1000, FLT_EPSILON);
	params.C = 0.01;

	Mysvm svm;
	svm.train( sampleFeaturesMat, sampleLabelMat, NULL, NULL, params ); //用SVM线性分类器训练//sampleFeaturesMat保存各样本的特征值,sampleLabelMat保存图片类型 

	svm.save(classifierSavePath);

	cvReleaseMat(&sampleFeaturesMat);
	cvReleaseMat(&sampleLabelMat);

	int supportVectorSize = svm.get_support_vector_count();//获得支持向量的个数
	cout<<"support vector size of SVM:"<<supportVectorSize<<endl;
	cout<<"************************ end of training for SVM ******************"<<endl;

	CvMat *sv,*alp,*re;//所有样本特征向量 
	sv  = cvCreateMat(supportVectorSize , totalCols, CV_32FC1);
	alp = cvCreateMat(1 , supportVectorSize, CV_32FC1);
	re  = cvCreateMat(1 , totalCols, CV_32FC1);
	CvMat *res  = cvCreateMat(1 , 1, CV_32FC1);

	cvSetZero(sv);
	cvSetZero(re);

	for(int i=0; i<supportVectorSize; i++)
	{
		memcpy( (float*)(sv->data.fl+i*totalCols), svm.get_support_vector(i), totalCols*sizeof(float)); //get_support_vector获得对应的索引编号的支持向量
	}

	double* alphaArr = svm.get_alpha();
	int alphaCount = svm.get_alpha_count();
	cout<<"alpharr"<<*alphaArr<<endl;

	for(int i=0; i<supportVectorSize; i++)
	{
		alp->data.fl[i] = alphaArr[i];
	}
	cvMatMul(alp, sv, re);

	int posCount = 0;
	for (int i=0; i<totalCols; i++)
	{
		re->data.fl[i] *= -1;
	}

	FILE* fp = fopen("E:\\My Documents1\\hogsvm\\hogsvm\\num.txt","wb");
	if( NULL == fp )
	{
		return;
	}
	for(int i=0; i<totalCols; i++)
	{
		fprintf(fp,"%f \n",re->data.fl[i]);
	}
	float rho = svm.get_rho();
	fprintf(fp, "%f", rho);
	cout<<"E:\\My Documents1\\hogsvm\\hogsvm\\num.txt 保存完毕"<<endl;//保存HOG能识别的分类器
	fclose(fp);

	return;
}

/*void Detect()
{
CvCapture* cap = cvCreateFileCapture("D:\\people.avi");
//CvCapture* cap = cvCreateFileCapture("E:/My Documents1/detecthog+svm/detecthog+svm/00.jpg");
if (!cap)
{
cout<<"avi file load error..."<<endl;
system("pause");
exit(-1);
}

vector<float> x;
ifstream fileIn("D:/image/WorkTest/hogSVMDetector-peopleFlow.txt", ios::in);
float val = 0.0f;
while(!fileIn.eof())
{
fileIn>>val;
x.push_back(val);
}
fileIn.close();

vector<cv::Rect>  found;
cv::HOGDescriptor hog(cv::Size(20,20), cv::Size(10,10), cv::Size(5,5), cv::Size(5,5), 9);
hog.setSVMDetector(x);

IplImage* img = NULL;
cvNamedWindow("img", 0);
while(img=cvQueryFrame(cap))
{
hog.detectMultiScale(img, found, 0, cv::Size(5,5), cv::Size(10,10), 1.05, 2);
if (found.size() > 0)
{

for (int i=0; i<found.size(); i++)
{
CvRect tempRect = cvRect(found[i].x, found[i].y, found[i].width, found[i].height);

cvRectangle(img, cvPoint(tempRect.x,tempRect.y),
cvPoint(tempRect.x+tempRect.width,tempRect.y+tempRect.height),CV_RGB(255,0,0), 2);
}
}
}
cvReleaseCapture(&cap);
}
*/


int face()  
{  
	Mat img;  
	FILE* f = 0;  
	char _filename[1024] = "3.jpg"; 
	char _filePath[1024];

	sprintf(_filePath, "E:\\My Documents1\\hogsvm\\hogsvm\\%s", _filename);
	img = imread(_filePath);  

	if( !img.data )   
	{    
		return -1;   
	}  

	cv::HOGDescriptor hog(cv::Size(64,64), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9);//("D:\\pedestrianDetect-peopleFlow.xml");  
	//hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());//得到检测器 
	//vector<float> detector;// = load_lear_model("D:\\hogSVMDetector-peopleFlow.txt");
	//CvLatentSvmDetector* detector = cvLoadLatentSvmDetector("result.xml");
	vector<float> detector;

	ifstream detector_data("E:\\My Documents1\\hogsvm\\hogsvm\\num.txt", ios::in);
	int count = 0;
	string buf;
	float tmpFlaot;
	while(!detector_data.eof())
	{
		detector_data >> tmpFlaot;
		detector.push_back(tmpFlaot);
		count ++;
	}
	hog.setSVMDetector(detector);
	namedWindow("people detector", 1); 

	for(;;)  
	{  
		char* filename = _filename;  
		if(f)  
		{  
			if(!fgets(filename, (int)sizeof(_filename)-2, f))  
				break;  
			//while(*filename && isspace(*filename))   
			//  ++filename;   
			if(filename[0] == '#')  
				continue;  
			int l = strlen(filename);  
			while(l > 0 && isspace(filename[l-1]))  
				--l;  
			filename[l] = '\0';  
			img = imread(filename);  
		}  
		printf("%s:\n", filename);  
		if(!img.data)  
			continue;  

		fflush(stdout);  
		vector<Rect> found, found_filtered;  
		double t = (double)getTickCount();  
		// run the detector with default parameters. to get a higher hit-rate   
		// (and more false alarms, respectively), decrease the hitThreshold and   
		// groupThreshold (set groupThreshold to 0 to turn off the grouping completely).   
		hog.detectMultiScale(img, found, 0, Size(8,8), Size(0,0), 1.05, 2);  
		t = (double)getTickCount() - t;  
		printf("tdetection time = %gms\n", t*1000./cv::getTickFrequency());  
		size_t i, j;  
		for( i = 0; i < found.size(); i++ )  
		{  
			Rect r = found[i];  
			for( j = 0; j < found.size(); j++ )  
				if( j != i && (r & found[j]) == r)  
					break;  
			if( j == found.size() )  
				found_filtered.push_back(r);  
		} 
		count = 0;
		for( i = 0; i < found_filtered.size(); i++ )  
		{  
			Rect r = found_filtered[i];  
			// the HOG detector returns slightly larger rectangles than the real objects.   
			// so we slightly shrink the rectangles to get a nicer output.   
			r.x += cvRound(r.width*0.1);  
			r.width = cvRound(r.width*0.8);  
			r.y += cvRound(r.height*0.07);  
			r.height = cvRound(r.height*0.8);  
			rectangle(img, r.tl(), r.br(), cv::Scalar(255,0,0), 3);  
			count++;
		} 
		cout<<"all count round:"<<count<<endl;
		imshow("detector", img);  
		int c = cvWaitKey(0) & 255;  
		if( c == 'q' || c == 'Q' || !f)  
			break;  
	}  
	if(f)  
		fclose(f);  
	return 0;  
}  


int main()
{	
	Train();
	//Detect();
	face();

	return 0;
}


说明:

1、在vs2005工程目录下面,要加入一个pmmintrin.h文件,在vs2008中就不需要,vs2008中本身就有这个文件,而05中没有

2、此程序在vs2008上运行正确,在vs2005上出错,原因,把vector<float> featureVec; 修改为 vector<float> featureVec(1764);就可以运行

只要是>1764就可以。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值