【机器学习--opencv3.4.1版本基于Hog特征描述子Svm对经典手写数字识别】

 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子。HOG特征通过计算和统计图像局部区域的梯度方向直方图来构成特征。


#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;

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 = SVM::create();//SVM分类器



	string ImgName;//图片名(绝对路径)



	ifstream finPos("img.txt");//正样本图片的文件名列表



	if (!finPos)

	{

		cout << "Pos/Neg imglist reading failed..." << endl;

		return 1;

	}



	for (int num = 0; num < 200 && getline(finPos, ImgName); num++)

	{

		std::cout << "Now processing original positive image: " << ImgName << endl;

		ImgName = "Image\\" + ImgName;//加上正样本的路径名



		Mat src = imread(ImgName);//读取图片



								  //if (CENTRAL_CROP)

								  //	src = src(Rect(16, 16, 128, 128));//将96*160的INRIA正样本图片剪裁为64*128,即剪去上下左右各16个像素



		vector<float> descriptors;//HOG描述子向量

		hog.compute(src, descriptors, Size(8, 8));//计算HOG描述子,检测窗口移动步长(8,8)



												  //处理第一个样本时初始化特征向量矩阵和类别矩阵,因为只有知道了特征向量的维数才能初始化特征向量矩阵

		if (0 == num)

		{

			DescriptorDim = descriptors.size();

			//初始化所有训练样本的特征向量组成的矩阵,行数等于所有样本的个数,列数等于HOG描述子维数sampleFeatureMat

			sampleFeatureMat = Mat::zeros(200, DescriptorDim, CV_32FC1);

			//初始化训练样本的类别向量,行数等于所有样本的个数,列数等于1;1表示有人,0表示无人

			sampleLabelMat = Mat::zeros(200, 1, CV_32SC1);//sampleLabelMat的数据类型必须为有符号整数型

		}



		//将计算好的HOG描述子复制到样本特征矩阵sampleFeatureMat

		for (int i = 0; i<DescriptorDim; i++)

			sampleFeatureMat.at<float>(num, i) = descriptors[i];//第num个样本的特征向量中的第i个元素



		sampleLabelMat.at<int>(num, 0) = num / 20;//正样本类别为1,有人

	}



	finPos.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 <iostream>

#include <fstream>
#include <opencv2/core/bufferpool.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;
using namespace cv::ml;



int main(){

	//reat SVM classfier

	Ptr<SVM> 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<float> 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

猜你喜欢

转载自blog.csdn.net/qq_35054151/article/details/83447593