行人检测(二)代码解析

   最近要做图像特征提取,可能要用下HOG特征,所以研究了下OpenCV的HOG描述子。OpenCV中的HOG特征提取功能使用了HOGDescriptor这个类来进行封装,其中也有现成的行人检测的接口。然而,无论是OpenCV官方说明文档还是各个中英文网站目前都没有这个类的使用说明,所以在这里把研究的部分心得分享一下。

 

 

 

 

 

  1.  CV_WRAP HOGDescriptor() : winSize(64,128), blockSize(16,16), blockStride(8,8),  
  2.         cellSize(8,8), nbins(9), derivAperture(1), winSigma(-1),  
  3.         histogramNormType(HOGDescriptor::L2Hys), L2HysThreshold(0.2), gammaCorrection(true),  free_coef(-1.f)
  4.         nlevels(HOGDescriptor::DEFAULT_NLEVELS),signedGradient(false)
  5.     {} 

  

winSize : 窗口的大小

blockSize :块的大小

cellSize: 胞元的大小

nbins: 方向bin的个数 nBins表示在一个胞元(cell)中统计梯度的方向数目,例如nBins=9时,在一个胞元内统计9个方向的梯度直方图,每个方向为360/9=40度。

  1.  CV_WRAP HOGDescriptor(Size _winSize, Size _blockSize, Size _blockStride,  
  2.                   Size _cellSize, int _nbins, int _derivAperture=1, double _winSigma=-1,  
  3.                   int _histogramNormType=HOGDescriptor::L2Hys,  
  4.                   double _L2HysThreshold=0.2, bool _gammaCorrection=false,  
  5.                   int _nlevels=HOGDescriptor::DEFAULT_NLEVELS)  bool _signedGradient=false)
  6.     : winSize(_winSize), blockSize(_blockSize), blockStride(_blockStride), cellSize(_cellSize),  
  7.     nbins(_nbins), derivAperture(_derivAperture), winSigma(_winSigma),  
  8.     histogramNormType(_histogramNormType), L2HysThreshold(_L2HysThreshold),  
  9.     gammaCorrection(_gammaCorrection), free_coef(-1.f),nlevels(_nlevels),signedGradient(_signedGradient) 
  10.     {}  
  11.  
    1.  CV_WRAP HOGDescriptor(const String& filename)  
    2.     {  
    3.         load(filename);  
    4.     }  
    5.   
    6.     HOGDescriptor(const HOGDescriptor& d)  
    7.     {  
    8.         d.copyTo(*this);  
    9.     }  
      virtual ~HOGDescriptor() {}  
  12.  我们看到HOGDescriptor一共有4个构造函数,前三个有CV_WRAP前缀,表示它们是从DLL里导出的函数,即我们在程序当中可以调用的函数;最后一个没有上述的前缀,所以我们暂时用不到,它其实就是一个拷贝构造函数。
    1.  CV_WRAP size_t getDescriptorSize() const;  
    2.     CV_WRAP bool checkDetectorSize() const;  
    3.     CV_WRAP double getWinSigma() const;  
    4.   
    5.     CV_WRAP virtual void setSVMDetector(InputArray _svmdetector);  
    6.   
    7.     virtual bool read(FileNode& fn);  
    8.     virtual void write(FileStorage& fs, const String& objname) const;  
    9.   
    10.     CV_WRAP virtual bool load(const String& filename, const String& objname=String());  
    11.     CV_WRAP virtual void save(const String& filename, const String& objname=String()) const;  
    12.     virtual void copyTo(HOGDescriptor& c) const;  
    1. CV_WRAP virtual void compute(const Mat& img,  
    2.                          CV_OUT vector<float>& descriptors,  
    3.                          Size winStride=Size(), Size padding=Size(),  
    4.                          const vector<Point>& locations=vector<Point>()) const;  
    5.     //with found weights output  
    6.     CV_WRAP virtual void detect(const Mat& img, CV_OUT vector<Point>& foundLocations,  
    7.                         CV_OUT vector<double>& weights,  
    8.                         double hitThreshold=0, Size winStride=Size(),  
    9.                         Size padding=Size(),  
    10.                         const vector<Point>& searchLocations=vector<Point>()) const;  
    11.     //without found weights output  
    12.     virtual void detect(const Mat& img, CV_OUT vector<Point>& foundLocations,  
    13.                         double hitThreshold=0, Size winStride=Size(),  
    14.                         Size padding=Size(),  
    15.                         const vector<Point>& searchLocations=vector<Point>()) const;  
    16.     //with result weights output  
    17.     CV_WRAP virtual void detectMultiScale(const Mat& img, CV_OUT vector<Rect>& foundLocations,  
    18.                                   CV_OUT vector<double>& foundWeights, double hitThreshold=0,  
    19.                                   Size winStride=Size(), Size padding=Size(), double scale=1.05,  
    20.                                   double finalThreshold=2.0,bool useMeanshiftGrouping = falseconst;  
    21.     //without found weights output  
    22.     virtual void detectMultiScale(const Mat& img, CV_OUT vector<Rect>& foundLocations,  
    23.                                   double hitThreshold=0, Size winStride=Size(),  
    24.                                   Size padding=Size(), double scale=1.05,  
    25.                                   double finalThreshold=2.0, bool useMeanshiftGrouping = falseconst;  
    26.   
    27.     CV_WRAP virtual void computeGradient(const Mat& img, CV_OUT Mat& grad, CV_OUT Mat& angleOfs,  
    28.                                  Size paddingTL=Size(), Size paddingBR=Size()) const;  
    29.   
    1. CV_WRAP static vector<float> getDefaultPeopleDetector();  
    2.     CV_WRAP static vector<float> getDaimlerPeopleDetector();  
    3.   
    4.     CV_PROP Size winSize;  
    5.     CV_PROP Size blockSize;  
    6.     CV_PROP Size blockStride;  
    7.     CV_PROP Size cellSize;  
    8.     CV_PROP int nbins;  
    9.     CV_PROP int derivAperture;  
    10.     CV_PROP double winSigma;  
    11.     CV_PROP int histogramNormType;  
    12.     CV_PROP double L2HysThreshold;  
    13.     CV_PROP bool gammaCorrection;  
    14.     CV_PROP vector<float> svmDetector;  
    15.     CV_PROP int nlevels;
    16.     CV_PROP bool signedGradient;
    17.     UMat colSvmDector;
    18.     float free_coef ; 
      1.  // evaluate specified ROI and return confidence value for each location  
      2.    void detectROI(const cv::Mat& img, const vector<cv::Point> &locations,  
      3.                                    CV_OUT std::vector<cv::Point>& foundLocations, CV_OUT std::vector<double>& confidences,  
      4.                                    double hitThreshold = 0, cv::Size winStride = Size(),  
      5.                                    cv::Size padding = Size()) const;  
      6.   
      7.    // evaluate specified ROI and return confidence value for each location in multiple scales  
      8.    void detectMultiScaleROI(const cv::Mat& img,  
      9.                                                        CV_OUT std::vector<cv::Rect>& foundLocations,  
      10.                                                        std::vector<DetectionROI>& locations,  
      11.                                                        double hitThreshold = 0,  
      12.                                                        int groupThreshold = 0) const;  
      13.   
      14.    // read/parse Dalal's alt model file  
      15.    void readALTModel(std::string modelfile);  
      16. }; 
      17.  
      18. void groupRectangles(std::vector<cv::Rect>& rectList,std::vactor<double> & weight, int groupThreshold,double eps) const;
      19.  HOG描述子维度

              在确定了上述的参数后,我们就可以计算出一个HOG描述子的维度了。OpenCV中的HOG源代码是按照下面的式子计算出描述子的维度的。 

        1. size_t HOGDescriptor::getDescriptorSize() const  
        2. {  
        3.     CV_Assert(blockSize.width % cellSize.width == 0 &&  
        4.         blockSize.height % cellSize.height == 0);  
        5.     CV_Assert((winSize.width - blockSize.width) % blockStride.width == 0 &&  
        6.         (winSize.height - blockSize.height) % blockStride.height == 0 );  
        7.     return (size_t)nbins*  
        8.         (blockSize.width/cellSize.width)*  
        9.         (blockSize.height/cellSize.height)*  
        10.         ((winSize.width - blockSize.width)/blockStride.width + 1)*  
        11.         ((winSize.height - blockSize.height)/blockStride.height + 1);  
        12. }
              

=++++++++++++++++++++++提取 HOG 特征+++++++++++++++++++++++++=

 

参考:

 

 

OpenCV HOGDescriptor 参数图解

 

 

opencv中的 HOGDescriptor 类

Opencv得到HOG特征(HOGDescriptor 的使用)

 

自己训练SVM分类器,进行HOG行人检测。

opencv中的 HOGDescriptor 类

 

目标检测学习_1(用opencv自带hog实现行人检测)

 

 

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值