OpenCV——HOG特征檢測


 

 

API:

HOGDescriptor(Size _winSize, ---:窗口大小,即檢測的范圍大小,前面的64*128
        Size _blockSize,--- 前面的2*2的cell,即cell的數量,這里要填像素值Size(16,16)
        Size _blockStride,---每次block移動的步長,以像素計,為一個cell像素塊大小
             Size _cellSize, ---cell的大小,前面的8*8
        int _nbins, ----直方圖的組數
        int _derivAperture=1, --梯度計算的參數
        double _winSigma=-1, --梯度計算的參數
             int _histogramNormType=HOGDescriptor::L2Hys,---歸一化的方法
              double _L2HysThreshold=0.2,
        bool _gammaCorrection=false, ---是否要伽馬校正
               int _nlevels=HOGDescriptor::DEFAULT_NLEVELS,
         bool _signedGradient=false)

 

 1 #include <opencv2/opencv.hpp>
 2 //#include <opencv2/xfeatures2d.hpp>
 3 #include <iostream>
 4 
 5 using namespace cv;
 6 //using namespace cv::xfeatures2d;
 7 using namespace std;
 8 
 9 int main(int argc, char** argv) {
10     Mat src = imread("test.jpg");
11     if (src.empty()) {
12         printf("could not load image...\n");
13         return -1;
14     }
15     namedWindow("input image", CV_WINDOW_AUTOSIZE);
16     imshow("input image", src);
17 
18     Mat dst, dst_gray;
19     resize(src,dst,Size(64,128));// 改變大小
20 
21     cvtColor(dst,dst_gray,COLOR_BGR2GRAY);
22 
23     HOGDescriptor detector(Size(64, 128), Size(16, 16), Size(8, 8), Size(8, 8),9);
24     vector<float> descriptors;//直方圖向量
25     vector<Point>locations;
26     detector.compute(dst_gray, descriptors,Size(0,0),Size(0,0),locations);
27     printf("number of HOG descriptors :%d", descriptors.size());
28 
29     waitKey(0);
30     return 0;
31 }

使用OpenCV已經訓練好的模型實現行人檢測

 1 #include <opencv2/opencv.hpp>
 2 #include <iostream>
 3 
 4 using namespace cv;
 5 using namespace std;
 6 
 7 int main(int argc, char** argv) {
 8     Mat src = imread("行人.jpg");
 9     if (src.empty()) {
10         printf("could not load image...\n");
11         return -1;
12     }
13     namedWindow("input image", CV_WINDOW_AUTOSIZE);
14     imshow("input image", src);    
15 
16     //使用opencv已經訓練好的模型,實現行人檢測
17     HOGDescriptor hog= HOGDescriptor();
18     hog.setSVMDetector(hog.getDefaultPeopleDetector());
19 
20     vector<Rect> foundLocations;
21     hog.detectMultiScale(src, foundLocations,0,Size(8,8),Size(32,32),1.05,2);//在多尺度上尋找
22     for (size_t t = 0; t < foundLocations.size(); t++) {
23         rectangle(src, foundLocations[t],Scalar(0,0,255),2,8,0);
24     }
25 
26     namedWindow("HOG行人檢測",CV_WINDOW_AUTOSIZE);
27     imshow("HOG行人檢測",src);
28 
29     waitKey(0);
30     return 0;
31 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM