Recently, Image Feature Extraction may require hog features. Therefore, we have studied the hog descriptor of opencv. The hog Feature Extraction function in opencv uses the hogdescriptor class for encapsulation. There are also ready-made interfaces for pedestrian detection. However, there are no instructions for using this class either in the official opencv documentation or on Chinese and English websites. So I will share some of my experiences here.
First, go to the header file where hogdescriptor is located to see which parameters are required by its constructor.
CV_WRAP HOGDescriptor() : winSize(64,128), blockSize(16,16), blockStride(8,8), cellSize(8,8), nbins(9), derivAperture(1), winSigma(-1), histogramNormType(HOGDescriptor::L2Hys), L2HysThreshold(0.2), gammaCorrection(true), nlevels(HOGDescriptor::DEFAULT_NLEVELS) {}
CV_WRAP HOGDescriptor(Size _winSize, Size _blockSize, Size _blockStride, Size _cellSize, int _nbins, int _derivAperture=1, double _winSigma=-1, int _histogramNormType=HOGDescriptor::L2Hys, double _L2HysThreshold=0.2, bool _gammaCorrection=false, int _nlevels=HOGDescriptor::DEFAULT_NLEVELS) : winSize(_winSize), blockSize(_blockSize), blockStride(_blockStride), cellSize(_cellSize), nbins(_nbins), derivAperture(_derivAperture), winSigma(_winSigma), histogramNormType(_histogramNormType), L2HysThreshold(_L2HysThreshold), gammaCorrection(_gammaCorrection), nlevels(_nlevels) {}
CV_WRAP HOGDescriptor(const String& filename) { load(filename); }
HOGDescriptor(const HOGDescriptor& d) { d.copyTo(*this); }
We can see that hogdescriptor has a total of four constructors. The first three have the cv_wrap prefix, indicating that they are the functions exported from the DLL, that is, the functions that can be called in the program; the last one does not have the above prefix, so we cannot use it for the moment. It is actually a copy constructor.
Next let's focus on the parameters of the previous constructor. Here we have several important parameters to study: winsize (64,128), blocksize (), blockstride ), cellsize (8, 8), nbins (9 ). These are all member variables of hogdescriptor. The values in the brackets are their default values. They reflect the parameters of the hog sub-statement. Here are a few examples to illustrate their meanings.
Window Size winsize
Block Size blocksize
Cellsize
Gradient Direction Number nbins
Nbins indicates counting the number of gradient directions in a cell. For example, if nbins is set to 9, the gradient histogram in 9 directions is collected within a cell, each direction is 180/9 = 20 degrees.
Hog description sub-Dimension
After the preceding parameters are determined, we can calculate the dimension of a hog description sub-dimension. The hog source code in opencv calculates the sub-dimension according to the following formula.
size_t HOGDescriptor::getDescriptorSize() const{ CV_Assert(blockSize.width % cellSize.width == 0 && blockSize.height % cellSize.height == 0); CV_Assert((winSize.width - blockSize.width) % blockStride.width == 0 && (winSize.height - blockSize.height) % blockStride.height == 0 ); return (size_t)nbins* (blockSize.width/cellSize.width)* (blockSize.height/cellSize.height)* ((winSize.width - blockSize.width)/blockStride.width + 1)* ((winSize.height - blockSize.height)/blockStride.height + 1);}
References
The hog Algorithm in opencv comes fromHistograms of Oriented gradients for human detection, cvpr 2005. For detailed algorithms, refer to this article.