方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子。HOG特征通过计算和统计图像局部区域的梯度方向直方图来构成特征。
参考链接:https://livezingy.com/hogdescriptor-in-opencv3-1/
//https://livezingy.com/hogdescriptor-in-opencv3-1/
//训练集合图像大小128*128
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
using namespace cv::ml;
int main() {
//winsize(64,128),blocksize(16,16),blockstep(8,8),cellsize(8,8),bins9
//检测窗口(64,128),块尺寸(16,16),块步长(8,8),cell尺寸(8,8),直方图bin个数9
HOGDescriptor hog(Size(128, 128), Size(16, 16), Size(8, 8), Size(8, 8), 9);
//HOG检测器,用来计算HOG描述子的
int DescriptorDim;//HOG描述子的维数,由图片大小、检测窗口大小、块大小、细胞单元中直方图bin个数决定
Mat sampleFeatureMat;//所有训练样本的特征向量组成的矩阵,行数等于所有样本的个数,列数等于HOG描述子维数
Mat sampleLabelMat;//训练样本的类别向量,行数等于所有样本的个数,列数等于1;1表示有人,-1表示无人
Ptr svm = SVM::create();//SVM分类器
string ImgName;//图片名(绝对路径)
ifstream fin("pos_list.txt");//正样本图片的文件名列表
if (!fin)
{
cout << "Pos/Neg imglist reading failed..." << endl;
return -1;
}
for (int num = 0; num < 2000 && getline(fin, ImgName); num++)
{
std::cout << "Now processing original image: " << ImgName << endl;
/*ImgName = "样本//" + ImgName;*///加上正样本的路径名
Mat src = imread(ImgName);//读取图片
if (src.empty())
cout << "no pic" << endl;
resize(src, src, Size(128, 128), 1);
//if (CENTRAL_CROP)
// src = src(Rect(16, 16, 128, 128));//将96*160的INRIA正样本图片剪裁为64*128,即剪去上下左右各16个像素
vector descriptors;//HOG描述子向量
hog.compute(src, descriptors, Size(8, 8));//计算HOG描述子,检测窗口移动步长(8,8)
//处理第一个样本时初始化特征向量矩阵和类别矩阵,因为只有知道了特征向量的维数才能初始化特征向量矩阵
if (0 == num)
{
DescriptorDim = descriptors.size();
//初始化所有训练样本的特征向量组成的矩阵,行数等于所有样本的个数,列数等于HOG描述子维数sampleFeatureMat
sampleFeatureMat = Mat::zeros(2000, DescriptorDim, CV_32FC1);
//初始化训练样本的类别向量,行数等于所有样本的个数,列数等于1;1表示有人,0表示无人
sampleLabelMat = Mat::zeros(2000, 1, CV_32SC1);//sampleLabelMat的数据类型必须为有符号整数型
}
//将计算好的HOG描述子复制到样本特征矩阵sampleFeatureMat
for (int i = 0; i < DescriptorDim; i++)
{
sampleFeatureMat.at(num, i) = descriptors[i];//第num个样本的特征向量中的第i个元素
}
sampleLabelMat.at(num, 0) = num / 1000;//正样本类别为1,有人
}
fin.close();
//输出样本的HOG特征向量矩阵到文件
svm->setType(SVM::C_SVC);
svm->setC(0.01);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 3000, 1e-6));
std::cout << "Starting training..." << endl;
svm->train(sampleFeatureMat, ROW_SAMPLE, sampleLabelMat);//训练分类器
std::cout << "Finishing training..." << endl;
//将训练好的SVM模型保存为xml文件
svm->SVM::save("SVM_HOG.xml");
//imshow("src", src);
waitKey();
return 0;
}
基于xml文件的预测:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
using namespace cv::ml;
int main(){
//reat SVM classfier
Ptr svm = SVM::create();
//load train file
svm = SVM::load("SVM_HOG.xml");
if (!svm){
cout << "Load file failed..." << endl;
}
Mat test;
test = imread("123.png");
//winsize(64,128),blocksize(16,16),blockstep(8,8),cellsize(8,8),bins9
//检测窗口(64,128),块尺寸(16,16),块步长(8,8),cell尺寸(8,8),直方图bin个数9
HOGDescriptor hog(Size(128, 128), Size(16, 16), Size(8, 8), Size(8, 8), 9);
vector descriptors;//HOG描述子向量
hog.compute(test, descriptors, Size(8, 8));//计算HOG描述子,检测窗口移动步长(8,8)
int r = svm->predict(descriptors); //对所有行进行预测
cout << "The number is " << r << endl;
//waitKey();//note:The function only works if there is at least one HighGUI window created and the window is active.If there are several HighGUI windows, any of them can be active.
//getchar();
system("pause");
return 1;
}
训练好的xml文件与数据集合:https://download.csdn.net/my
C=参考博客:https://blog.csdn.net/wwwsssZheRen/article/details/79542693?utm_source=blogxgwz4