opencv HOG特征提取+svm行人检测

本章内容:


        1. HOG特征描述符计算
        2. opencv系统默认 hog + svm行人检测

 

opencv HOG特征提取+svm行人检测

输出结果

opencv HOG特征提取+svm行人检测

代码

#include <ostream>
#include <opencv.hpp>
#include<opencv2/opencv.hpp>
#include "opencv2/xfeatures2d.hpp"

int main(int argc, char *argv[])
{
    /*
     本章内容:
        1. HOG特征描述符计算
        2. opencv系统默认 hog + svm行人检测
    */
    cv::String fileName = "/home/wang/dev/Image/pepole.jpeg";
    cv::Mat src = cv::imread(fileName);
    cv::Mat src1 = src.clone();
    if(src.data == NULL){
        printf("图像读入失败\n");
        return -1;
    }
    cv::imshow("src",src);
    /*1. HOG特征描述符计算流程
     * 1. 转换成灰度图像
     * 2.定义HOG特征描述符
     * 3.计算特征描述符
    */
    cv::Mat gray;
    cv::cvtColor(src,gray,cv::COLOR_BGR2GRAY);
    cv::HOGDescriptor hogDes(cv::Size(64,128),cv::Size(16,16),cv::Size(8,8),cv::Size(8,8),9);
    std::vector<float> dess;
    std::vector<cv::Point> loca;
    hogDes.compute(gray,dess,cv::Size(0,0),cv::Size(0,0),loca);
    std::cout << "number of HOG descriptors : " << dess.size() << std::endl;

    // 行人检测
    cv::HOGDescriptor hog = cv::HOGDescriptor();
    std::vector<float>  desPeople= hog.getDefaultPeopleDetector();
    std::cout << "desPeople.size() = " << desPeople.size() << std::endl;
    /*
    brief Sets coefficients for the linear SVM classifier.
    @param svmdetector coefficients for the linear SVM classifier.
    */
    hog.setSVMDetector(desPeople);
    std::vector<cv::Rect> Flocations;
    hog.detectMultiScale(src,Flocations,0,cv::Size(8,8),cv::Size(32,32),1.05,2);
    std::cout << "pepole num = " << Flocations.size() << std::endl;
    for(int i=0; i < Flocations.size();i++){
        cv::rectangle(src,Flocations[i],cv::Scalar(0,255,0),4);
    }
    cv::imshow("pepole detect",src);

    cv::waitKey(0);
    return 1;
}