1. 程式人生 > >HOG+SVM實現目標檢測

HOG+SVM實現目標檢測

一、環境: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) 

二、函式引數分析

(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;
}