歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> OpenCV_基於HOG特征的行人檢測

OpenCV_基於HOG特征的行人檢測

日期:2017/3/1 10:02:55   编辑:Linux編程

OpenCV中提供了HOG的行人檢測(pedestrain detection)類。

cv::HOGDescriptor類的構造函數的各參數的定義:

CV_WRAP HOGDescriptor() :
winSize(64,128), // detect window
blockSize(16,16), // block 大小
blockStride(8,8), // overlap block的滑動步長
cellSize(8,8), // cell 大小
nbins(9), // 直方圖的bin個數
derivAperture(1), // 微分算子核
winSigma(-1), // 在window上進行高斯加權
histogramNormType(HOGDescriptor::L2Hys), // 直方圖歸一化類型
L2HysThreshold(0.2), // L2-norm followed by clipping (limiting the maximum values of v to 0.2) and renormalising
gammaCorrection(true), // Gamma校正,去除光照影響
nlevels(HOGDescriptor::DEFAULT_NLEVELS) // 分層數

下面的兩段代碼采用OpenCV中的HOG行人檢測類來完成對靜態圖片中的行人檢測。

1)采用64*128 (像素為單位)的detect window

// 基於HOG特征的行人檢測
// Author: http://blog.csdn.net/icvpr

#include <iostream>
#include <opencv2/opencv.hpp>


int main(int argc, char** argv)
{
cv::Mat image = cv::imread("test.bmp");
if (image.empty())
{
std::cout<<"read image failed"<<std::endl;
}


// 1. 定義HOG對象
cv::HOGDescriptor hog; // 采用默認參數

// 2. 設置SVM分類器
hog.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector()); // 采用已經訓練好的行人檢測分類器

// 3. 在測試圖像上檢測行人區域
std::vector<cv::Rect> regions;
hog.detectMultiScale(image, regions, 0, cv::Size(8,8), cv::Size(32,32), 1.05, 1);

// 顯示
for (size_t i = 0; i < regions.size(); i++)
{
cv::rectangle(image, regions[i], cv::Scalar(0,0,255), 2);
}

cv::imshow("hog", image);
cv::waitKey(0);

return 0;
}

行人檢測實驗結果:

Copyright © Linux教程網 All Rights Reserved