文章目录
 
 - HOGDescriptor 构造函数
- setSVMDetector 设置支持向量机(SVM)检测器,用于目标检测。
- compute 用于计算图像区域的HOG描述符。
- detectMultiScale 多尺度检测目标。
- 示例
  
 
HOGDescriptor 构造函数
 
HOGDescriptor();
HOGDescriptor(const Size& _winSize, const Size& _blockSize, 
              const Size& _blockStride, const Size& _cellSize, 
              int _nbins, double _alpha, double _L2HysThreshold,
              bool _gammaCorrection, NHistType _histogramNormType);
              
    _winSize: 指定窗口大小,默认是 64x128 像素。
    _blockSize: 指定块的大小,默认是 16x16 像素。一个块由多个单元格(cell)组成。
    _blockStride: 指定在计算下一个块时块之间滑动的步长,默认是 8x8 像素。
    _cellSize: 指定每个单元格的大小,默认是 8x8 像素。每个单元格内的像素点将被用来计算梯度直方图。
    _nbins: 指定每个单元格中梯度方向的数量,默认是 9 个bin。
    _alpha: 权重缩放因子,通常设置为 1.0。
    _L2HysThreshold: L2 范数阈值,用于防止光照变化影响。默认值通常是 0.2。
    _gammaCorrection: 是否应用伽马校正预处理。默认是 false。
    _histogramNormType: 直方图归一化类型。
 
setSVMDetector 设置支持向量机(SVM)检测器,用于目标检测。
 
void setSVMDetector(vector<float> detector)
detector: SVM权重向量,可以通过训练获得或使用OpenCV提供的默认检测器。
 
compute 用于计算图像区域的HOG描述符。
 
void compute(InputArray img, vector<float>& descriptors, 
             const Size winStride=Size(), const vector<Point>& locations=vector<Point>()); 
    img: 输入图像,应该为灰度图像。
    descriptors: 输出的HOG描述符。
    winStride: 窗口滑动步长。
    locations: 指定需要计算的特定位置。
 
detectMultiScale 多尺度检测目标。
 
void detectMultiScale(Mat image, vector<Rect>& foundLocations, 
                      double hitThreshold=0, Size winStride=Size(),
                      Size padding=Size(), double scale=1.05, int finalThreshold=2,
                      bool useMeanshiftGrouping=false)
                      
    image: 输入图像。
    foundLocations: 输出矩形框列表,表示检测到的目标位置。
    hitThreshold: 决定检测是否成功的阈值。
    winStride: 滑动窗口的步长。
    padding: 检测窗口的填充大小。
    scale: 图像金字塔的比例因子。
    finalThreshold: 需要的邻居数量。
    useMeanshiftGrouping: 是否使用均值漂移分组来合并候选矩形框。
 
示例
 
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
    
    Mat src = imread("D:/vcprojects/images/HOGV.png");
    
    
    if (src.empty()) {
        printf("could not load image...\n");
        return -1;
    }
    
    
    namedWindow("input image", CV_WINDOW_AUTOSIZE);
    
    
    imshow("input image", src);
    
    
    HOGDescriptor hog = HOGDescriptor();
    hog.setSVMDetector(hog.getDefaultPeopleDetector());
    
    vector<Rect> foundLocations;
    
    
    
    hog.detectMultiScale(src, foundLocations, 0, Size(8, 8), Size(32, 32), 1.05, 2);
    
    
    Mat result = src.clone();
    
    
    for (size_t t = 0; t < foundLocations.size(); t++) {
        rectangle(result, foundLocations[t], Scalar(0, 0, 255), 2, 8, 0);
    }
    
    
    namedWindow("HOG SVM Detector Demo", CV_WINDOW_AUTOSIZE);
    
    
    imshow("HOG SVM Detector Demo", result);
    
    waitKey(0);
    
    
    return 0;
}
 
