opencv HOG SVM 二

Posted qianbo_insist

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv HOG SVM 二相关的知识,希望对你有一定的参考价值。

这一部分是hog svm 识别部分
训练部分

show me the code

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


#ifndef _DEBUG
#pragma comment(lib,"../opencv/opencv_world341.lib")
#else
#pragma comment(lib,"opencv_world341d.lib")
#endif

int main(int argc ,char * argv[])
{
	//creat SVM classfier
	if (argc < 2) {
		cout << "please input the file" << endl;
		return -1;
	}
	char * file = argv[1];
	cout << "program will read the file:" << file << endl;
	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(file);

	if (test.empty())
	{
		cout << "not exist" << endl;
		return -1;
	}
	cout << "Load test image done..." << endl;


	//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;
    getchar();
	return 1;
}

以上是关于opencv HOG SVM 二的主要内容,如果未能解决你的问题,请参考以下文章

opencv学习笔记SVM+HOG

OpenCV HOG+SVM:断言失败 checkDetectorSize()

HOG + SVM(行人检测, opencv实现)

OpenCV - 使用 SVM 和 HOG 进行人员检测

HOG+SVM(OpenCV案例源码train_HOG.cpp解读)

opencv HOG SVM 二