vs2005hog+svm,vs2008 hog+svm 中的computer()函数出错折磨我好几天
2013-02-02 21:20
274 查看
#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就可以。
相关文章推荐
- vs2005hog+svm,vs2008 hog+svm 中的computer()函数出错折磨我好几天
- IE8正式版引发VS2005和VS2008添加变量向导出错的解决方案
- VS2005 MFC MessageBox 消息函数出错
- 基于opencv中HOG+SVM的车轮识别及predict()函数的改进
- IE8正式版引发VS2005和VS2008添加变量向导出错的解决方案
- IE8正式版引发VS2005和VS2008添加变量向导出错的解决方案
- vs2005连接时出错拉 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用
- vs2005 vs2008 mainfest出错的一个很常见的情况
- VS2005、VS2008中SetUnhandledExceptionFilter函数不起作用
- VS2005与VS2008下编译的MPI程序运行出错的解决办法
- vs2005连接时出错拉 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用
- IE8正式版引发VS2005和VS2008添加变量向导出错的解决方案
- IE8正式版引发VS2005和VS2008添加变量向导出错的解决方案
- 无返回值的函数如何捕获出错情况(检查errno常量)
- 找不到php_mbstring.dll或php_exif.dll的解决方法与mb_系列函数的用法 libmcrypt.dll时出错的处理
- Opencv2.0 lib和dll的编译获取过程 以及 vs2005,vs2008配置过程
- Windows Server2008上安装VS2008出错及解决办法
- VS2008打开VS2005项目
- VS2005可以打开VS2008的工程么?
- vs2005 vs2008中使用wince5.0 sdk 仿真器