OpenCV3特征提取与目标检测之HOG(二)——使用官方的行人分类器实现行人检测
生活随笔
收集整理的這篇文章主要介紹了
OpenCV3特征提取与目标检测之HOG(二)——使用官方的行人分类器实现行人检测
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、概述
前面看到如何使用HOG去提取一張圖像的中的局部目標的特征,那么得到了這些特征之后,用來做什么,OpencCV的官方給了一個使用HOG進行行人檢測的樣例。所加載的分類器是官方已經訓練好的,而OpenCV官方自帶的分類器是利用Navneet Dalal和Bill Triggs提供的樣本進行訓練的,并利用線性SVM作為分類器,從而實現行人檢測。
二.代碼實現
#pragma once#include <iostream> #include <string> #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> #include <opencv2\opencv.hpp> #include<ctime>using namespace std; using namespace cv; using namespace cv::ml;class HogDetection { public:HogDetection();HogDetection(Mat _src);~HogDetection();Mat hogSvm(); private:Mat src; };實現文件:
#include "HogDetection.h" #include <opencv2\xfeatures2d\nonfree.hpp>HogDetection::HogDetection() {}HogDetection::HogDetection(Mat _src) {src = _src; }HogDetection::~HogDetection() { }Mat HogDetection::hogSvm() {//外接矩形數組vector<Rect> found, found_filtered; clock_t start, end;start = clock();//HOG特征檢測器HOGDescriptor hog;//設置SVM分類器為默認參數 hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());//對圖像進行多尺度檢測,檢測窗口移動步長為(8,8)hog.detectMultiScale(src, found, 0, Size(2, 2), Size(0, 0), 1.05, 2);end = (double)(1000 * (clock() - start) / CLOCKS_PER_SEC);cout << "HOG Detection time: "<<end << "ms!" << endl;//找出所有沒有嵌套的矩形框r,并放入found_filtered中,如果有嵌套的話,則取外面最大的那個矩形框放入found_filtered中for (int i = 0; i < found.size(); i++){Rect r = found[i];int j = 0;for (; j < found.size(); j++){if (j != i && (r & found[j]) == r){break;} } if (j == found.size()){found_filtered.push_back(r);} }//畫矩形框,因為hog檢測出的矩形框比實際人體框要稍微大些,所以這里需要做一些調整for (int i = 0; i<found_filtered.size(); i++){Rect r = found_filtered[i];r.x += cvRound(r.width*0.1);r.width = cvRound(r.width*0.8);r.y += cvRound(r.height*0.07);r.height = cvRound(r.height*0.8);rectangle(src, r.tl(), r.br(), Scalar(0, 0, 255), 3);}return src; }主函數:
#include "HogDetection.h"using namespace std; using namespace cv; using namespace cv::ml;int main(void) {Mat src = imread("C:/Users/matt/Desktop/demo/05.jpg");namedWindow("src", WINDOW_NORMAL);imshow("src", src);HogDetection hogDet(src);Mat svm_dst = hogDet.hogSvm();namedWindow("HOGSVM", WINDOW_NORMAL);imshow("HOGSVM", svm_dst);waitKey(0);return 0; }運行結果:
3.運行得到的結果并不見得都能適用個人項目的應用場合。因此,針對個人的特定應用場景,有時候很有必要進行重新訓練得到適合自己使用的分類器。
后記
1.關于整個工程的源碼,運行程序時的bug,或者有如何優代碼的想法都可以加這個群(487350510)互相討論學習。總結
以上是生活随笔為你收集整理的OpenCV3特征提取与目标检测之HOG(二)——使用官方的行人分类器实现行人检测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 论文笔记:GVCNN: Group-Vi
- 下一篇: Windows下Caffe的学习与应用(