【行人检测】利用HOG+SVM训练模型步骤:
1.准备样本
2.提取hog特征
3. 训练SVM分类器
4.利用SVM训练的分类器,进行检测
1.原始数据集: INRIAPerson训练集下载:原始数据集
因为原始数据集比较乱,所以需要对原始数据集进行裁剪,下面是对原始数据集里面负例数据集裁剪图片的例程:
#include<fstream>//fstream
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/objdetect/objdetect.hpp> // hog
#include<opencv2/ml/ml.hpp> //SVM
#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
using namespace cv;
//using namespace ml;
#define PosNum 614
#define NegNum 1218
#define cropNegNum 1218
//#define totalDim 3780
/********************************* 随机剪裁负样本 *******************************************/
void crop_negsample_random()
{
string imgName;
char saveName[200];
char pic_name[200];
int neg_num;
//string newNegFile = "F:\\opencv_project\\data\\INRIAPerson\\Train\\new_neg\\" ;
ifstream fileNeg("F:\\360MoveData\\Users\\SIACAS\\Desktop\\OPENCVproj\\Train\\neg.lst");
ofstream file_newneg("F:\\360MoveData\\Users\\SIACAS\\Desktop\\OPENCVproj\\Train\\new_neg.txt");
if (fileNeg.fail())
{
cout << "加载文件失败!" << endl;
}
int num = 0;
//读取负样本
for (int i = 0; i < cropNegNum && getline(fileNeg, imgName); i++)
{
imgName = "F:\\360MoveData\\Users\\SIACAS\\Desktop\\OPENCVproj\\" + imgName; //加路径
Mat img = imread(imgName, 1);
//Mat img;
if (img.empty())
{
cout << "can not load the image:" << imgName << endl;
continue;
}
if (img.cols >= 64 && img.rows >= 128)
{
//num = 0;
//从每张图片中随机剪裁2张64*128的负样本
srand(time(NULL));//设置随机数种子
int x = rand() % (img.cols - 64); //左上角x
int y = rand() % (img.rows - 128); //左上角y
Mat src = img(Rect(x, y, 64, 128));
neg_num = num++;
sprintf_s(saveName, "F:\\360MoveData\\Users\\SIACAS\\Desktop\\OPENCVproj\\Train\\new_neg\\negCropped%d.jpg", neg_num);
sprintf_s(pic_name, "Train\\new_neg\\negCropped%d.jpg", neg_num);
imwrite(saveName, src);
file_newneg << pic_name << endl;
}
}
cout << "crop ok!" << endl;
}
上述为裁剪图像的例程,裁剪完后就可以进行加载图像进行提取HOG特征了,后面的程序可以参考后面我提到的博客,这里我就不重复写了!以上程序特别要注意imgName = "F:\\360MoveData\\Users\\SIACAS\\Desktop\\OPENCVproj\\" + imgName; 这个路径一定要填好 ,要不然会找不到图片!!!
然后晒一下结果,,,,很一般!可能训练的不好,,,后续再更新最好的结果!
程序源码这里下载:源代码
参考文献:
1、https://blog.csdn.net/u012679707/article/details/80669745#commentsedit