之前介绍过Hog特征(http://blog.csdn.net/carson2005/article/details/7782726),也介绍过SVM分类器(http://blog.csdn.net/carson2005/article/details/6453502 );而本文的目的在于介绍利用Hog特征和SVM分类器来进行行人检测。
在2005年CVPR上,来自法国的研究人员Navneet Dalal 和Bill Triggs提出利用Hog进行特征提取,利用线性SVM作为分类器,从而实现行人检测。而这两位也通过大量的测试发现,Hog+SVM是速度和效果综合平衡性能较好的一种行人检测方法。后来,虽然很多研究人员也提出了很多改进的行人检测算法,但基本都以该算法为基础框架。因此,Hog+SVM也成为一个里程表式的算法被写入到OpenCV中。在OpenCV2.0之后的版本,都有Hog特征描述算子的API,而至于SVM,早在OpenCV1.0版本就已经集成进去了;OpenCV虽然提供了Hog和SVM的API,也提供了行人检测的sample,遗憾的是,OpenCV并没有提供样本训练的sample。这也就意味着,很多人只能用OpenCV自带的已经训练好的分类器来进行行人检测。然而,OpenCV自带的分类器是利用Navneet Dalal和Bill Triggs提供的样本进行训练的,不见得能适用于你的应用场合。因此,针对你的特定应用场景,很有必要进行重新训练得到适合你的分类器。本文的目的,正在于此。
重新训练行人检测的流程:
(1)准备训练样本集合;包括正样本集和负样本集;根据机器学习的基础知识我们知道,要利用机器学习算法进行样本训练,从而得到一个性能优良的分类器,训练样本应该是无限多的,而且训练样本应该覆盖实际应用过程中可能发生的各种情况。(很多朋友,用10来个正样本,10来个负样本进行训练,之后,就进行测试,发现效果没有想象中的那么好,就开始发牢骚,抱怨。。。对于这些人,我只能抱歉的说,对于机器学习、模式识别的认识,你还处于没有入门的阶段);实际应用过程中,训练样本不可能无限多,但无论如何,三五千个正样本,三五千个负样本,应该不是什么难事吧?(如果连这个都做不到,建议你别搞机器学习,模式识别了;训练素材都没有,怎么让机器学习到足够的信息呢?)
(2)收集到足够的训练样本之后,你需要手动裁剪样本。例如,你想用Hog+SVM来对商业步行街的监控画面中进行行人检测,那么,你就应该用收集到的训练样本集合,手动裁剪画面中的行人(可以写个简单程序,只需要鼠标框选一下,就将框选区域保存下来)。
(3)裁剪得到训练样本之后,将所有正样本放在一个文件夹中;将所有负样本放在另一个文件夹中;并将所有训练样本缩放到同样的尺寸大小。OpenCV自带的例子在训练时,就是将样本缩放为64*128进行训练的;
(4)提取所有正样本的Hog特征;
(5)提取所有负样本的Hog特征;
(6)对所有正负样本赋予样本标签;例如,所有正样本标记为1,所有负样本标记为0;
(7)将正负样本的Hog特征,正负样本的标签,都输入到SVM中进行训练;Dalal在论文中考虑到速度问题,建议采用线性SVM进行训练。这里,不妨也采用线性SVM;
(8)SVM训练之后,将结果保存为文本文件。
(9)线性SVM进行训练之后得到的文本文件里面,有一个数组,叫做support vector,还有一个数组,叫做alpha,有一个浮点数,叫做rho;将alpha矩阵同support vector相乘,注意,alpha*supportVector,将得到一个列向量。之后,再该列向量的最后添加一个元素rho。如此,变得到了一个分类器,利用该分类器,直接替换opencv中行人检测默认的那个分类器(cv::HOGDescriptor::setSVMDetector()),就可以利用你的训练样本训练出来的分类器进行行人检测了。
下面给出样本训练的参考代码:
[cpp] view plaincopyprint?
01.class Mysvm: public CvSVM
02.{
03.public:
04. int get_alpha_count()
05. {
06. return this->sv_total;
07. }
08.
09. 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.}
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] = "c:/pedestrianDetect-peopleFlow.txt";
string positivePath = "E:\\pictures\\train1\\pos\\";
string negativePath = "E:\\pictures\\train1\\neg\\";
int positiveSampleCount = 4900;
int negativeSampleCount = 6192;
int totalSampleCount = positiveSampleCount + negativeSampleCount;
cout<<"//////////////////////////////////////////////////////////////////"<<endl;
cout<<"totalSampleCount: "<<totalSampleCount<<endl;
cout<<"positiveSampleCount: "<<positiveSampleCount<<endl;
cout<<"negativeSampleCount: "<<negativeSampleCount<<endl;
CvMat *sampleFeaturesMat = cvCreateMat(totalSampleCount , 1764, 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];
string path;
for(int i=0; i<positiveSampleCount; i++)
{
memset(positiveImgName, '\0', 256*sizeof(char));
sprintf(positiveImgName, "%d.jpg", i);
int len = strlen(positiveImgName);
string tempStr = positiveImgName;
path = positivePath + tempStr;
cv::Mat img = cv::imread(path);
if( img.data == NULL )
{
cout<<"positive image sample load error: "<<i<<" "<<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);
vector<float> featureVec;
hog.compute(img, featureVec, cv::Size(8,8));
int featureVecSize = featureVec.size();
for (int j=0; j<featureVecSize; j++)
{
CV_MAT_ELEM( *sampleFeaturesMat, float, i, j ) = featureVec[j];
}
sampleLabelMat->data.fl[i] = 1;
}
cout<<"end of training for positive samples..."<<endl;
cout<<"*********************************************************"<<endl;
cout<<"start to train negative samples..."<<endl;
char negativeImgName[256];
for (int i=0; i<negativeSampleCount; i++)
{
memset(negativeImgName, '\0', 256*sizeof(char));
sprintf(negativeImgName, "%d.jpg", i);
path = negativePath + negativeImgName;
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;
hog.compute(img,featureVec,cv::Size(8,8));//计算HOG特征
int featureVecSize = featureVec.size();
for ( int j=0; j<featureVecSize; j ++)
{
CV_MAT_ELEM( *sampleFeaturesMat, float, i + positiveSampleCount, j ) = featureVec[ j ];
}
sampleLabelMat->data.fl[ i + positiveSampleCount ] = -1;
}
cout<<"end of training for negative samples..."<<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线性分类器训练
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 , 1764, CV_32FC1);
alp = cvCreateMat(1 , supportVectorSize, CV_32FC1);
re = cvCreateMat(1 , 1764, 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*1764), svm.get_support_vector(i), 1764*sizeof(float));
}
double* alphaArr = svm.get_alpha();
int alphaCount = svm.get_alpha_count();
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<1764; i++)
{
re->data.fl[i] *= -1;
}
FILE* fp = fopen("c:/hogSVMDetector-peopleFlow.txt","wb");
if( NULL == fp )
{
return 1;
}
for(int i=0; i<1764; i++)
{
fprintf(fp,"%f \n",re->data.fl[i]);
}
float rho = svm.get_rho();
fprintf(fp, "%f", rho);
cout<<"c:/hogSVMDetector.txt 保存完毕"<<endl;//保存HOG能识别的分类器
fclose(fp);
return 1;
}接着,再给出利用训练好的分类器进行行人检测的参考代码:
[cpp] view plaincopyprint?
01.void Detect()
02.{
03. CvCapture* cap = cvCreateFileCapture("E:\\02.avi");
04. if (!cap)
05. {
06. cout<<"avi file load error..."<<endl;
07. system("pause");
08. exit(-1);
09. }
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.}
void Detect()
{
CvCapture* cap = cvCreateFileCapture("E:\\02.avi");
if (!cap)
{
cout<<"avi file load error..."<<endl;
system("pause");
exit(-1);
}
vector<float> x;
ifstream fileIn("c:/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(64,64), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9);
hog.setSVMDetector(x);
IplImage* img = NULL;
cvNamedWindow("img", 0);
while(img=cvQueryFrame(cap))
{
hog.detectMultiScale(img, found, 0, cv::Size(8,8), cv::Size(32,32), 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);
}
这里总结网上自己找到的资料,搞一个简单的框架供大家参考一下。
OpenCV官方的SVM代码在http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html
在http://blog.csdn.net/sangni007/article/details/7471222看到一段还不错的代码,结构清楚,虽然注释比较少,但很有参考价值,于是我添加了一些注释,看着更舒服。废话少说,直接上代码:
[cpp] view plaincopyprint #include "cv.h" #include "highgui.h" #include "stdafx.h" #include <ml.h> #include <iostream> #include <fstream> #include <string> #include <vector> using namespace cv; using namespace std; int main(int argc, char** argv) { vector<string> img_path;//输入文件名变量 vector<int> img_catg; int nLine = 0; string buf; ifstream svm_data( "E:/SVM_DATA.txt" );//首先,这里搞一个文件列表,把训练样本图片的路径都写在这个txt文件中,使用bat批处理文件可以得到这个txt文件 unsigned long n; while( svm_data )//将训练样本文件依次读取进来 { if( getline( svm_data, buf ) ) { nLine ++; if( nLine % 2 == 0 )//这里的分类比较有意思,看得出来上面的SVM_DATA.txt文本中应该是一行是文件路径,接着下一行就是该图片的类别,可以设置为0或者1,当然多个也无所谓 { img_catg.push_back( atoi( buf.c_str() ) );//atoi将字符串转换成整型,标志(0,1),注意这里至少要有两个类别,否则会出错 } else { img_path.push_back( buf );//图像路径 } } } svm_data.close();//关闭文件 CvMat *data_mat, *res_mat; int nImgNum = nLine / 2; //读入样本数量 ,因为是每隔一行才是图片路径,所以要除以2 ////样本矩阵,nImgNum:横坐标是样本数量, WIDTH * HEIGHT:样本特征向量,即图像大小 data_mat = cvCreateMat( nImgNum, 1764, CV_32FC1 ); //这里第二个参数,即矩阵的列是由下面的descriptors的大小决定的,可以由descriptors.size()得到,且对于不同大小的输入训练图片,这个值是不同的 cvSetZero( data_mat ); //类型矩阵,存储每个样本的类型标志 res_mat = cvCreateMat( nImgNum, 1, CV_32FC1 ); cvSetZero( res_mat ); IplImage* src; IplImage* trainImg=cvCreateImage(cvSize(64,64),8,3);//需要分析的图片,这里默认设定图片是64*64大小,所以上面定义了1764,如果要更改图片大小,可以先用debug查看一下descriptors是多少,然后设定好再运行 //开始搞HOG特征 for( string::size_type i = 0; i != img_path.size(); i++ ) { src=cvLoadImage(img_path[i].c_str(),1); if( src == NULL ) { cout<<" can not load the image: "<<img_path[i].c_str()<<endl; continue; } cout<<" processing "<<img_path[i].c_str()<<endl; cvResize(src,trainImg); //读取图片 HOGDescriptor *hog=new HOGDescriptor(cvSize(64,64),cvSize(16,16),cvSize(8,8),cvSize(8,8),9); //具体意思见参考文章1,2 vector<float>descriptors;//结果数组 hog->compute(trainImg, descriptors,Size(1,1), Size(0,0)); //调用计算函数开始计算 cout<<"HOG dims: "<<descriptors.size()<<endl; //CvMat* SVMtrainMat=cvCreateMat(descriptors.size(),1,CV_32FC1); n=0; for(vector<float>::iterator iter=descriptors.begin();iter!=descriptors.end();iter++) { cvmSet(data_mat,i,n,*iter);//把HOG存储下来 n++; } //cout<<SVMtrainMat->rows<<endl; cvmSet( res_mat, i, 0, img_catg[i] ); cout<<" end processing "<<img_path[i].c_str()<<" "<<img_catg[i]<<endl; } CvSVM svm = CvSVM();//新建一个SVM CvSVMParams param;//这里是参数 CvTermCriteria criteria; criteria = cvTermCriteria( CV_TERMCRIT_EPS, 1000, FLT_EPSILON ); param = CvSVMParams( CvSVM::C_SVC, CvSVM::RBF, 10.0, 0.09, 1.0, 10.0, 0.5, 1.0, NULL, criteria ); /* SVM种类:CvSVM::C_SVC Kernel的种类:CvSVM::RBF degree:10.0(此次不使用) gamma:8.0 coef0:1.0(此次不使用) C:10.0 nu:0.5(此次不使用) p:0.1(此次不使用) 然后对训练数据正规化处理,并放在CvMat型的数组里。 */ //☆☆☆☆☆☆☆☆☆(5)SVM学习☆☆☆☆☆☆☆☆☆☆☆☆ svm.train( data_mat, res_mat, NULL, NULL, param );//训练啦 //☆☆利用训练数据和确定的学习参数,进行SVM学习☆☆☆☆ svm.save( "SVM_DATA.xml" ); //检测样本 IplImage *test; vector<string> img_tst_path; ifstream img_tst( "E:/SVM_TEST.txt" );//同输入训练样本,这里也是一样的,只不过不需要标注图片属于哪一类了 while( img_tst ) { if( getline( img_tst, buf ) ) { img_tst_path.push_back( buf ); } } img_tst.close(); CvMat *test_hog = cvCreateMat( 1, 1764, CV_32FC1 );//注意这里的1764,同上面一样 char line[512]; ofstream predict_txt( "SVM_PREDICT.txt" );//把预测结果存储在这个文本中 for( string::size_type j = 0; j != img_tst_path.size(); j++ )//依次遍历所有的待检测图片 { test = cvLoadImage( img_tst_path[j].c_str(), 1); if( test == NULL ) { cout<<" can not load the image: "<<img_tst_path[j].c_str()<<endl; continue; } cvZero(trainImg); cvResize(test,trainImg); //读取图片 HOGDescriptor *hog=new HOGDescriptor(cvSize(64,64),cvSize(16,16),cvSize(8,8),cvSize(8,8),9); //具体意思见参考文章1,2 vector<float>descriptors;//结果数组 hog->compute(trainImg, descriptors,Size(1,1), Size(0,0)); //调用计算函数开始计算 cout<<"HOG dims: "<<descriptors.size()<<endl; CvMat* SVMtrainMat=cvCreateMat(1,descriptors.size(),CV_32FC1); n=0; for(vector<float>::iterator iter=descriptors.begin();iter!=descriptors.end();iter++) { cvmSet(SVMtrainMat,0,n,*iter); n++; } int ret = svm.predict(SVMtrainMat);//获取最终检测结果,这个predict的用法见 OpenCV的文档 std::sprintf( line, "%s %d\r\n", img_tst_path[j].c_str(), ret ); predict_txt<<line; } predict_txt.close(); //cvReleaseImage( &src); //cvReleaseImage( &sampleImg ); //cvReleaseImage( &tst ); //cvReleaseImage( &tst_tmp ); cvReleaseMat( &data_mat ); cvReleaseMat( &res_mat ); return 0; }
另外,自己需要把这个程序嵌入到另外一个工程中去,因为那里数据类型是Mat,不是cvMat,所以我又修改了上面的程序,并且图片大小也不是固定的64*64,需要自己设置一下图片大小,因为太懒,直接把改好的程序放过来:
#include "stdafx.h" #include "cv.h" #include "highgui.h" #include "stdafx.h" #include <ml.h> #include <iostream> #include <fstream> #include <string> #include <vector> using namespace cv; using namespace std; int main(int argc, char** argv) { int ImgWidht = 120; int ImgHeight = 120; vector<string> img_path; vector<int> img_catg; int nLine = 0; string buf; ifstream svm_data( "E:/apple/SVM_DATA.txt" ); unsigned long n; while( svm_data ) { if( getline( svm_data, buf ) ) { nLine ++; if( nLine < 5 ) { img_catg.push_back(1); img_path.push_back( buf );//图像路径 } else { img_catg.push_back(0); img_path.push_back( buf );//图像路径 } } } svm_data.close();//关闭文件 Mat data_mat, res_mat; int nImgNum = nLine; //读入样本数量 ////样本矩阵,nImgNum:横坐标是样本数量, WIDTH * HEIGHT:样本特征向量,即图像大小 //data_mat = Mat::zeros( nImgNum, 12996, CV_32FC1 ); //类型矩阵,存储每个样本的类型标志 res_mat = Mat::zeros( nImgNum, 1, CV_32FC1 ); Mat src; Mat trainImg = Mat::zeros(ImgHeight, ImgWidht, CV_8UC3);//需要分析的图片 for( string::size_type i = 0; i != img_path.size(); i++ ) { src = imread(img_path[i].c_str(), 1); cout<<" processing "<<img_path[i].c_str()<<endl; resize(src, trainImg, cv::Size(ImgWidht,ImgHeight), 0, 0, INTER_CUBIC); HOGDescriptor *hog=new HOGDescriptor(cvSize(ImgWidht,ImgHeight),cvSize(16,16),cvSize(8,8),cvSize(8,8), 9); //具体意思见参考文章1,2 vector<float>descriptors;//结果数组 hog->compute(trainImg, descriptors, Size(1,1), Size(0,0)); //调用计算函数开始计算 if (i==0) { data_mat = Mat::zeros( nImgNum, descriptors.size(), CV_32FC1 ); //根据输入图片大小进行分配空间 } cout<<"HOG dims: "<<descriptors.size()<<endl; n=0; for(vector<float>::iterator iter=descriptors.begin();iter!=descriptors.end();iter++) { data_mat.at<float>(i,n) = *iter; n++; } //cout<<SVMtrainMat->rows<<endl; res_mat.at<float>(i, 0) = img_catg[i]; cout<<" end processing "<<img_path[i].c_str()<<" "<<img_catg[i]<<endl; } CvSVM svm = CvSVM(); CvSVMParams param; CvTermCriteria criteria; criteria = cvTermCriteria( CV_TERMCRIT_EPS, 1000, FLT_EPSILON ); param = CvSVMParams( CvSVM::C_SVC, CvSVM::RBF, 10.0, 0.09, 1.0, 10.0, 0.5, 1.0, NULL, criteria ); /* SVM种类:CvSVM::C_SVC Kernel的种类:CvSVM::RBF degree:10.0(此次不使用) gamma:8.0 coef0:1.0(此次不使用) C:10.0 nu:0.5(此次不使用) p:0.1(此次不使用) 然后对训练数据正规化处理,并放在CvMat型的数组里。 */ //☆☆☆☆☆☆☆☆☆(5)SVM学习☆☆☆☆☆☆☆☆☆☆☆☆ svm.train( data_mat, res_mat, Mat(), Mat(), param ); //☆☆利用训练数据和确定的学习参数,进行SVM学习☆☆☆☆ svm.save( "E:/apple/SVM_DATA.xml" ); //检测样本 vector<string> img_tst_path; ifstream img_tst( "E:/apple/SVM_TEST.txt" ); while( img_tst ) { if( getline( img_tst, buf ) ) { img_tst_path.push_back( buf ); } } img_tst.close(); Mat test; char line[512]; ofstream predict_txt( "E:/apple/SVM_PREDICT.txt" ); for( string::size_type j = 0; j != img_tst_path.size(); j++ ) { test = imread( img_tst_path[j].c_str(), 1);//读入图像 resize(test, trainImg, cv::Size(ImgWidht,ImgHeight), 0, 0, INTER_CUBIC);//要搞成同样的大小才可以检测到 HOGDescriptor *hog=new HOGDescriptor(cvSize(ImgWidht,ImgHeight),cvSize(16,16),cvSize(8,8),cvSize(8,8),9); //具体意思见参考文章1,2 vector<float>descriptors;//结果数组 hog->compute(trainImg, descriptors,Size(1,1), Size(0,0)); //调用计算函数开始计算 cout<<"The Detection Result:"<<endl; cout<<"HOG dims: "<<descriptors.size()<<endl; Mat SVMtrainMat = Mat::zeros(1,descriptors.size(),CV_32FC1); n=0; for(vector<float>::iterator iter=descriptors.begin();iter!=descriptors.end();iter++) { SVMtrainMat.at<float>(0,n) = *iter; n++; } int ret = svm.predict(SVMtrainMat); std::sprintf( line, "%s %d\r\n", img_tst_path[j].c_str(), ret ); printf("%s %d\r\n", img_tst_path[j].c_str(), ret); getchar(); predict_txt<<line; } predict_txt.close(); return 0; }
就到这里吧,再整理一下思路。
如果运行的时候出现Link错误,有可能是没有附加依赖项,要添加opencv_objdetect230d.lib,我的OpenCV是2.3版本,所以这里是230.
挑一个小毛病:
for( string::size_type i = 0; i != img_path.size(); i++ )
string的size_type是用来确定是字符串的第几个字符的。
应该改为:
for( vector<string>::size_type i = 0; i != img_path.size(); i++ )
vector<string>的才是用来判断是vector的第几个元素的。