参考了博客http://blog.csdn.net/carson2005/article/details/7841443 后,自己动手后发现了一些问题,博客里提到的一些问题没有解决
,是关于为什么图像的HOG特征向量debug后是15876的问题。答案是因为原作者的窗口是64*64的,所以维数为9*4*7*7=1764(图像的大小也是64*64,所以图像的特征维数与一个窗口的维数是相同的,compute()里的窗口步进(8,8)也是无效的)。而我的图像时64*128大小的,我把窗口也换成
64*128,所以维数就是3780了,与setSVMDetector默认的getDefaultPeopleDetector大小一样(大概小于getDefaultPeopleDetector()大小为(3781*1))的vector传到setSVMDetector()里都没关系吧,不会出现断言错误。
上代码:
/*
* =====================================================================================
*
* Filename: people_detector.cpp
* Environment:
* Description: 行人检测程序,程序里窗口大小和图片大小一样大,都是64*128
*
*
*
* Version: 1.0
* Created: 2013/10/20 10:45:02
* Author: yuliyang
I*
* Mail: wzyuliyang911@gmail.com
* Blog: http://www.cnblogs.com/yuliyang
*
* =====================================================================================
*/ #include "opencv2/opencv.hpp"
#include "windows.h"
#include "fstream"
#include <iostream>
using namespace std;
using namespace cv;
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;
}
}; int my_train()
{ /*-----------------------------------------------------------------------------
* e:/pedestrianDetect-peopleFlow.txt是用来保存所有样本的特征,大小为样本数*3780(每个样本的特征数)
*
*
*
*
*-----------------------------------------------------------------------------*/
char classifierSavePath[] = "e:/pedestrianDetect-peopleFlow.txt";
string buf;
vector<string> pos_img_path;
vector<string> neg_img_path;
ifstream svm_pos_data("pos.txt"); /* 批处理程序生成 */
ifstream svm_neg_data("neg.txt"); /* 批处理生成 */
while( svm_pos_data )//将训练样本文件依次读取进来
{
if( getline( svm_pos_data, buf ) )
pos_img_path.push_back( buf ); }
while( svm_neg_data )//将训练样本文件依次读取进来
{
if( getline( svm_neg_data, buf ) )
neg_img_path.push_back( buf ); }
cout<<pos_img_path.size()<<"个正样本"<<endl;
cout<<neg_img_path.size()<<"个负样本"<<endl;
int totalSampleCount=pos_img_path.size()+neg_img_path.size();
CvMat *sampleFeaturesMat = cvCreateMat(totalSampleCount , , CV_32FC1);
//64*128窗口大小的训练样本,该矩阵将是totalSample*3780
//64*64的窗口大小的训练样本,该矩阵将是totalSample*1764
cvSetZero(sampleFeaturesMat);
CvMat *sampleLabelMat = cvCreateMat(totalSampleCount, , CV_32FC1);//样本标识
cvSetZero(sampleLabelMat); cout<<"************************************************************"<<endl;
cout<<"start to training positive samples..."<<endl; for(int i=; i<pos_img_path.size(); i++)
{
cv::Mat img = cv::imread(pos_img_path.at(i)); if( img.data == NULL )
{
cout<<"positive image sample load error: "<<i<<endl;
system("pause");
continue;
} cv::HOGDescriptor hog(cv::Size(,), cv::Size(,), cv::Size(,), cv::Size(,), );
vector<float> featureVec; hog.compute(img, featureVec, cv::Size(,));
unsigned int featureVecSize = featureVec.size(); for (int j=; j<featureVecSize; j++)
{
CV_MAT_ELEM( *sampleFeaturesMat, float, i, j ) = featureVec[j];
}
sampleLabelMat->data.fl[i] = ;
}
cout<<"end of training for positive samples..."<<endl; cout<<"*********************************************************"<<endl;
cout<<"start to train negative samples..."<<endl; for (int i=; i<neg_img_path.size(); i++)
{ cv::Mat img = cv::imread(neg_img_path.at(i));
if(img.data == NULL)
{
cout<<"negative image sample load error: "<<endl;
continue;
} cv::HOGDescriptor hog(cv::Size(,), cv::Size(,), cv::Size(,), cv::Size(,), );
vector<float> featureVec; hog.compute(img,featureVec,cv::Size(,));//计算HOG特征
int featureVecSize = featureVec.size(); for ( int j=; j<featureVecSize; j ++)
{
CV_MAT_ELEM( *sampleFeaturesMat, float, i + pos_img_path.size(), j ) = featureVec[ j ];
} sampleLabelMat->data.fl[ i + pos_img_path.size() ] = -;
} 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, , 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 , , CV_32FC1);
alp = cvCreateMat( , supportVectorSize, CV_32FC1);
re = cvCreateMat( , , CV_32FC1);
CvMat *res = cvCreateMat( , , CV_32FC1); cvSetZero(sv);
cvSetZero(re); for(int i=; i<supportVectorSize; i++)
{
memcpy( (float*)(sv->data.fl+i*), svm.get_support_vector(i), *sizeof(float));
} double* alphaArr = svm.get_alpha();
int alphaCount = svm.get_alpha_count(); for(int i=; i<supportVectorSize; i++)
{
alp->data.fl[i] = (float)alphaArr[i];
}
cvMatMul(alp, sv, re); int posCount = ;
for (int i=; i<; i++)
{
re->data.fl[i] *= -;
} /*-----------------------------------------------------------------------------
* e:/hogSVMDetector-peopleFlow.txt文件中保存的是支持向量,共有3781个值,是一个3781*1的列向量
*
*
*-----------------------------------------------------------------------------*/
FILE* fp = fopen("e:/hogSVMDetector-peopleFlow.txt","wb");
if( NULL == fp )
{
return ;
}
for(int i=; i<; i++)
{
fprintf(fp,"%f \n",re->data.fl[i]);
}
float rho = svm.get_rho();
fprintf(fp, "%f", rho);
cout<<"e:/hogSVMDetector.txt 保存完毕"<<endl;//保存HOG能识别的分类器
fclose(fp); return ;
}
void my_detect()
{
CvCapture* cap = cvCreateFileCapture("E:\\test.avi");
if (!cap)
{
cout<<"avi file load error..."<<endl;
system("pause");
exit(-);
} vector<float> x;
ifstream fileIn("e:/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(,), cv::Size(,), cv::Size(,), cv::Size(,), ); /*-----------------------------------------------------------------------------
*
*
* 如果setSVMDetector出现问题的话,可能是这个原因:因为默认hog.getDefaultPeopleDetector()
* 获取的检测器的大小是3781*1的列向量,所以如果生成的e:/hogSVMDetector-peopleFlow.txt里的大小不等的话
* ,读入
* 就会出现错误,可能这个函数考虑了运行的速度问题,所以限制了大小为3781*1
*
* 特别注意:有些童鞋可能生成的特征向量是15876(所以setSVMDetector里的列向量就是15877了与默认的大小不一,assetion就出错了)
* ,只要调整下图像的大小和检测窗口的大小,使生成的特征向量为3780就行了,怎么计算,可以参考
* 网上其他博客
*
*-----------------------------------------------------------------------------*/
hog.setSVMDetector(x); IplImage* img = NULL;
cvNamedWindow("img", );
while(img=cvQueryFrame(cap))
{
hog.detectMultiScale(img, found, , cv::Size(,), cv::Size(,), 1.05, );
if (found.size() > )
{
for (int i=; 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(,,), );
}
}
}
cvReleaseCapture(&cap);
} int main(int argc, char** argv){ //my_train();
//my_detect();
vector<float> x;
ifstream fileIn("e:/hogSVMDetector-peopleFlow.txt", ios::in); /* 读入支持向量,没必要读入样本的向量 */
float val = 0.0f;
while(!fileIn.eof())
{
fileIn>>val;
x.push_back(val);
}
fileIn.close(); vector<Rect> found, found_filtered;
cv::HOGDescriptor hog(cv::Size(,), cv::Size(,), cv::Size(,), cv::Size(,), );
hog.setSVMDetector(x); Mat img;
img=imread("1.jpg",);
hog.detectMultiScale(img, found, , cv::Size(,), cv::Size(,), 1.05, );
size_t i, j;
for( i = ; i < found.size(); i++ )
{
Rect r = found[i];
for( j = ; j < found.size(); j++ )
if( j != i && (r & found[j]) == r)
break;
if( j == found.size() )
found_filtered.push_back(r);
}
for( i = ; 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(,,), );
}
imshow("people detector", img);
waitKey(); /*cvNamedWindow("img", 0);
string testimage="E:\database\picture_resize_pos\resize000r.bmp";
Mat img=cv::imread(testimage);
hog.detectMultiScale(img, found, 0, cv::Size(8,8), cv::Size(32,32), 1.05, 2);
if (found.size() > 0)
{
printf("found!");
}*/ return ; }
运行效果:
正样本1500多个,负样本400多个,所以效果不咋地,只能呵呵了。
再上一张效果图:(ps:运行的太慢了)