HOG+SVM实现目标检测

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lindamtd/article/details/80693720

一、环境:VS2013+OpenCV3.0

看论文《Detection and Recognition of Traffic Planar Objects Using Colorized Laser Scan and Perspective Distortion Rectification》,Traffic Planar Objects Detection is implemented by the HoG+SVM。

HoG是在计算机视觉和图像处理中用于实现物体检测的特征描述子,出自论文:

Dalal N, Triggs B. Histograms of oriented gradients for human detection[C]//Computer Vision and Pattern Recognition, 2005. CVPR 2005. IEEE Computer Society Conference on. IEEE, 2005, 1: 886-893.(2016:Google Citation: 14046) 

下载链接:https://hal.inria.fr/file/index/docid/548512/filename/hog_cvpr2005.pdf

HoG特征详细总结:https://www.cnblogs.com/wyuzl/p/6792216.html

二、函数参数分析

(1)detectMultiScal()

virtual void detectMultiScale(InputArray img, CV_OUT std::vector<Rect>& foundLocations,
                                  double hitThreshold = 0, Size winStride = Size(),
                                  Size padding = Size(), double scale = 1.05,
                                  double finalThreshold = 2.0, bool useMeanshiftGrouping = false) const;

共有8个参数:

img: 输入图像,可以是彩色图像也可以是灰度图像;

foundLocations:存取检测到的目标的位置;

hitThreshold(optional): The threshold for the distance from features to the SVM classifying plane;

winStride(optional): HoG检测窗口移动时的步长(水平和垂直)

padding(optional):在原图外围添加像素,常见的pad 尺寸包括(8,8),(16,16),(24,24),(32,32)

scale:图像的多尺度表示,每层图像都被缩小然后被高斯平滑,通常在[1.01-1.5];

finalThreshold:优化bounding box.

useMeanshiftGrouping:bool类型,表示是否用meanShift来消除重叠,默认为false.

下面的三幅图hitThreshold分别为0, 0.5,1的检测结果:

hitThreshold=0


hitThreshold=0.5


hitThreshold=1


由此可见,参数的设置不同,对检测效果影响很大,每个参数都需要合理设置,才可以达到最佳的检测效果。

(2) rectangle(): 通过传入矩形参数来画矩形

CV_EXPORTS void rectangle(CV_IN_OUT Mat& img, Rect rec,
                          const Scalar& color, int thickness = 1,
                          int lineType = LINE_8, int shift = 0);

(3)rectangle():通过传入对角线两点来画矩形

CV_EXPORTS_W void rectangle(InputOutputArray img, Point pt1, Point pt2,
                          const Scalar& color, int thickness = 1,
                          int lineType = LINE_8, int shift = 0);

三、代码实现HoG行人检测:

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main()
{
	Mat src = imread("person_293.bmp");
	if (!src.data)
	{
		cout << "read image failed" << endl;
		return false;
	}
	//Define HOG Object
	HOGDescriptor hog; // 采用默认参数
	//Set SVM Classifier
	hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
	//Detect the Pedestrians region on the test image 
	vector<Rect> regions;
	double hitThreshold=0.5;
	cout << " hitThreshold=" << hitThreshold << endl;
	hog.detectMultiScale(src, regions, hitThreshold, Size(8, 8), Size(32, 32), 1.05, 1);
	// Display
	for (size_t i = 0; i < regions.size(); i++)
	{
		rectangle(src, regions[i], Scalar(0, 0, 255), 2);  //对判定是行人的区域画矩形标记
	}
    imshow("hog", src);
    waitKey(0);
	return 0;
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页