正样本来源是INRIA数据集中的96*160大小的人体图片,使用时上下左右都去掉16个像素,截取中间的64*128大小的人体。
负样本是从不包含人体的图片中随机裁取的,大小同样是64*128(从完全不包含人体的图片中随机剪裁出64*128大小的用于人体检测的负样本)。
SVM使用的是OpenCV自带的CvSVM类。
首先计算正负样本图像的HOG描述子,组成一个特征向量矩阵,对应的要有一个指定每个特征向量的类别的类标向量,输入SVM中进行训练。
训练好的SVM分类器保存为XML文件,然后根据其中的支持向量和参数生成OpenCV中的HOG描述子可用的检测子参数,再调用OpenCV中的多尺度检测函数进行行人检测。
难例(Hard Example)是指利用第一次训练的分类器在负样本原图(肯定没有人体)上进行行人检测时所有检测到的矩形框,这些矩形框区域很明显都是误报,把这些误报的矩形框保存为图片,加入到初始的负样本集合中,重新进行SVM的训练,可显著减少误报。
用训练好的分类器在负样本原图上检测Hard Example见:用初次训练的SVM+HOG分类器在负样本原图上检测HardExample
Navneet Dalal在CVPR2005上的HOG原论文翻译见:http://blog.csdn.net/masibuaa/article/details/14056807
- #include <iostream>
- #include <fstream>
- #include <opencv2/core/core.hpp>
- #include <opencv2/highgui/highgui.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- #include <opencv2/objdetect/objdetect.hpp>
- #include <opencv2/ml/ml.hpp>
-
- using namespace std;
- using namespace cv;
-
- #define PosSamNO 2400 //正样本个数
- #define NegSamNO 12000 //负样本个数
-
- #define TRAIN false //是否进行训练,true表示重新训练,false表示读取xml文件中的SVM模型
- #define CENTRAL_CROP true //true:训练时,对96*160的INRIA正样本图片剪裁出中间的64*128大小人体
-
-
-
- #define HardExampleNO 4435
-
-
-
-
- class MySVM : public CvSVM
- {
- public:
-
- double * get_alpha_vector()
- {
- return this->decision_func->alpha;
- }
-
-
- float get_rho()
- {
- return this->decision_func->rho;
- }
- };
-
-
-
- int main()
- {
-
- HOGDescriptor hog(Size(64,128),Size(16,16),Size(8,8),Size(8,8),9);
- int DescriptorDim;
- MySVM svm;
-
-
- if(TRAIN)
- {
- string ImgName;
- ifstream finPos("INRIAPerson96X160PosList.txt");
-
- ifstream finNeg("NoPersonFromINRIAList.txt");
-
- Mat sampleFeatureMat;
- Mat sampleLabelMat;
-
-
-
- for(int num=0; num<PosSamNO && getline(finPos,ImgName); num++)
- {
- cout<<"处理:"<<ImgName<<endl;
-
- ImgName = "D:\\DataSet\\INRIAPerson\\INRIAPerson\\96X160H96\\Train\\pos\\" + ImgName;
- Mat src = imread(ImgName);
- if(CENTRAL_CROP)
- src = src(Rect(16,16,64,128));
-
-
- vector<float> descriptors;
- hog.compute(src,descriptors,Size(8,8));
-
-
-
- if( 0 == num )
- {
- DescriptorDim = descriptors.size();
-
- sampleFeatureMat = Mat::zeros(PosSamNO+NegSamNO+HardExampleNO, DescriptorDim, CV_32FC1);
-
- sampleLabelMat = Mat::zeros(PosSamNO+NegSamNO+HardExampleNO, 1, CV_32FC1);
- }
-
-
- for(int i=0; i<DescriptorDim; i++)
- sampleFeatureMat.at<float>(num,i) = descriptors[i];
- sampleLabelMat.at<float>(num,0) = 1;
- }
-
-
- for(int num=0; num<NegSamNO && getline(finNeg,ImgName); num++)
- {
- cout<<"处理:"<<ImgName<<endl;
- ImgName = "D:\\DataSet\\NoPersonFromINRIA\\" + ImgName;
- Mat src = imread(ImgName);
-
-
- vector<float> descriptors;
- hog.compute(src,descriptors,Size(8,8));
-
-
-
- for(int i=0; i<DescriptorDim; i++)
- sampleFeatureMat.at<float>(num+PosSamNO,i) = descriptors[i];
- sampleLabelMat.at<float>(num+PosSamNO,0) = -1;
- }
-
-
- if(HardExampleNO > 0)
- {
- ifstream finHardExample("HardExample_2400PosINRIA_12000NegList.txt");
-
- for(int num=0; num<HardExampleNO && getline(finHardExample,ImgName); num++)
- {
- cout<<"处理:"<<ImgName<<endl;
- ImgName = "D:\\DataSet\\HardExample_2400PosINRIA_12000Neg\\" + ImgName;
- Mat src = imread(ImgName);
-
-
- vector<float> descriptors;
- hog.compute(src,descriptors,Size(8,8));
-
-
-
- for(int i=0; i<DescriptorDim; i++)
- sampleFeatureMat.at<float>(num+PosSamNO+NegSamNO,i) = descriptors[i];
- sampleLabelMat.at<float>(num+PosSamNO+NegSamNO,0) = -1;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- CvTermCriteria criteria = cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 1000, FLT_EPSILON);
-
- CvSVMParams param(CvSVM::C_SVC, CvSVM::LINEAR, 0, 1, 0, 0.01, 0, 0, 0, criteria);
- cout<<"开始训练SVM分类器"<<endl;
- svm.train(sampleFeatureMat, sampleLabelMat, Mat(), Mat(), param);
- cout<<"训练完成"<<endl;
- svm.save("SVM_HOG.xml");
-
- }
- else
- {
- svm.load("SVM_HOG_2400PosINRIA_12000Neg_HardExample(误报少了漏检多了).xml");
- }
-
-
-
-
-
-
-
-
- DescriptorDim = svm.get_var_count();
- int supportVectorNum = svm.get_support_vector_count();
- cout<<"支持向量个数:"<<supportVectorNum<<endl;
-
- Mat alphaMat = Mat::zeros(1, supportVectorNum, CV_32FC1);
- Mat supportVectorMat = Mat::zeros(supportVectorNum, DescriptorDim, CV_32FC1);
- Mat resultMat = Mat::zeros(1, DescriptorDim, CV_32FC1);
-
-
- for(int i=0; i<supportVectorNum; i++)
- {
- const float * pSVData = svm.get_support_vector(i);
- for(int j=0; j<DescriptorDim; j++)
- {
-
- supportVectorMat.at<float>(i,j) = pSVData[j];
- }
- }
-
-
- double * pAlphaData = svm.get_alpha_vector();
- for(int i=0; i<supportVectorNum; i++)
- {
- alphaMat.at<float>(0,i) = pAlphaData[i];
- }
-
-
-
- resultMat = -1 * alphaMat * supportVectorMat;
-
-
- vector<float> myDetector;
-
- for(int i=0; i<DescriptorDim; i++)
- {
- myDetector.push_back(resultMat.at<float>(0,i));
- }
-
- myDetector.push_back(svm.get_rho());
- cout<<"检测子维数:"<<myDetector.size()<<endl;
-
- HOGDescriptor myHOG;
- myHOG.setSVMDetector(myDetector);
-
-
-
- ofstream fout("HOGDetectorForOpenCV.txt");
- for(int i=0; i<myDetector.size(); i++)
- {
- fout<<myDetector[i]<<endl;
- }
-
-
-
-
-
- Mat src = imread("1.png");
- vector<Rect> found, found_filtered;
- cout<<"进行多尺度HOG人体检测"<<endl;
- myHOG.detectMultiScale(src, found, 0, Size(8,8), Size(32,32), 1.05, 2);
- cout<<"找到的矩形框个数:"<<found.size()<<endl;
-
-
- for(int i=0; i < found.size(); i++)
- {
- Rect r = found[i];
- int j=0;
- for(; j < found.size(); j++)
- if(j != i && (r & found[j]) == r)
- break;
- if( j == found.size())
- found_filtered.push_back(r);
- }
-
-
- for(int i=0; i<found_filtered.size(); i++)
- {
- Rect r = found_filtered[i];
- 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(src, r.tl(), r.br(), Scalar(0,255,0), 3);
- }
-
- imwrite("ImgProcessed.jpg",src);
- namedWindow("src",0);
- imshow("src",src);
- waitKey();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- system("pause");
- }
结果:
(1) 1500个INRIA正样本,2000个负样本,结果误报太多:
(2) 2400个INRIA正样本,12000个负样本,结果表明负样本增多后误报明显减少,但依然有不少误报:
(3)2400个INRIA正样本,12000个负样本 + 4435个用(2)中的分类器在负样本原图上检测出来的Hard Example,
结果误报明显减少,几乎没有误报了,但同时漏检率增加:
上图中的两个小女孩都没有被检测出来
(4)下面是OpenCV中HOG检测器的默认SVM参数的结果,OpenCV自带的SVM参数也是用INRIA数据集训练得到的:
上图中的两个小女孩用OpenCV默认SVM参数也检测不出来。
所以感觉要想效果好的话,还应该加大正样本的个数。
参考: http://blog.csdn.net/carson2005/article/details/7841443
源码下载,环境为VS2010 + OpenCV2.4.4
http://download.csdn.net/detail/masikkk/6547973
1500个INRIA正样本,2000个负样本训练好的SVM下载(XML文件):http://pan.baidu.com/s/18CCos
2400个INRIA正样本,12000个负样本训练好的SVM下载(XML文件):http://pan.baidu.com/s/1gmudL
2400个INRIA正样本,12000个负样本 + 4435个用(2)中的分类器在负样本原图上检测出来的Hard Example 训练好的SVM下载(XML文件):http://pan.baidu.com/s/126Yoc