OpenCV HOGDescripter Python

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

I was wondering if anyone knew why there is no documentation for HOGDescriptors in the Python bindings of OpenCV.

Maybe I've just missed them, but the only code I've found of them is this thread: Get HOG image features from OpenCV + Python?

If you scroll down in that thread, this code is found in there:

import cv2 hog = cv2.HOGDescriptor() im = cv2.imread(sample) h = hog.compute(im) 

I've tested this and it works -- so the Python Bindings do exist, just the documentation doesn't. I was wondering if anyone knew why documentation for the Python bindings for HOG is so difficult to find / non-existent. Does anyone know if there is a tutorial I can read anywhere about HOG (especially via the Python Bindings)? I'm new to HOG and would like to see a few examples of how OpenCV does stuff before I start writing my own stuff.

回答1:

1. Get Inbuilt Documentation: Following command on your python console will help you know the structure of class HOGDescriptor:

import cv2 help(cv2.HOGDescriptor())

2. Example code: Here is a snippet of code to initialize an cv2.HOGDescriptor with different parameters (The terms I used here are standard terms which are well defined in OpenCV documentation here):

import cv2 image = cv2.imread("test.jpg",0) winSize = (64,64) blockSize = (16,16) blockStride = (8,8) cellSize = (8,8) nbins = 9 derivAperture = 1 winSigma = 4. histogramNormType = 0 L2HysThreshold = 2.0000000000000001e-01 gammaCorrection = 0 nlevels = 64 hog = cv2.HOGDescriptor(winSize,blockSize,blockStride,cellSize,nbins,derivAperture,winSigma,                         histogramNormType,L2HysThreshold,gammaCorrection,nlevels) #compute(img[, winStride[, padding[, locations]]]) -> descriptors winStride = (8,8) padding = (8,8) locations = ((10,20),) hist = hog.compute(image,winStride,padding,locations) 

3. Reasoning: The resultant hog descriptor will have dimension as: 9 orientations X (4 corner blocks that get 1 normalization + 6x4 blocks on the edges that get 2 normalizations + 6x6 blocks that get 4 normalizations) = 1764. as I have given only one location for hog.compute().

4. Different way to initialize HOGDescriptor:
One more way to initialize is from xml file which contains all parameter values:

hog = cv2.HOGDescriptor("hog.xml") 

To get an xml file one can do following:

hog = cv2.HOGDescriptor() hog.save("hog.xml") 

and edit the respective parameter values in xml file.



回答2:

I was wondering the same. Almost none documentation can be found for OpenCV HOGDescriptor, other than the source cpp code.

Scikit-image has a good example page on extracting and illustrating HOG feature. It provides an alternative to explore HOG. It is documented here.

However, there is one thing to point out about scikit-image's hog implementation. Its Python code for hog function does not implement weighted vote for histogram orientation binning, but only does simple binning based on magnitude value falling into which bin. See its hog_histogram function. This is not following exactly Dalal and Triggs's paper.

Actually, I found that object detection based on OpenCV's implementation of HOG is more accurate than with the api from scikit-image. It makes sense to me, because weighted vote is important. By casting weighted votes to bins, variation in histogram is greatly reduced when gradient magnitude falls on or around the boundary. Chris McCormick wrote a very insightful blog on hog, in which orientation binning is clearly described as

For each gradient vector, it’s contribution to the histogram is given by the magnitude of the vector (so stronger gradients have a bigger impact on the histogram). We split the contribution between the two closest bins. So, for example, if a gradient vector has an angle of 85 degrees, then we add 1/4th of its magnitude to the bin centered at 70 degrees, and 3/4ths of its magnitude to the bin centered at 90.

I believe the intent of splitting the contribution is to minimize the problem of gradients which are right on the boundary between two bins. Otherwise, if a strong gradient was right on the edge of a bin, a slight change in the gradient angle (which nudges the gradient into the next bin) could have a strong impact on the histogram.

So, use OpenCV to compute hog if possible (haven't digged into its code and don't feel like doing so, but I suppose OpenCV's way of hog implementation is more appropriate). Not only I found an improvement in detection accuracy, but it also runs faster. Compared to scikit-image's hog code with wonderful comments, its documentation is almost none. Yet it is still feasible that one could get OpenCV's version working in practice - it's a matter of passing the right parameter for window size, cell size, block size, block stride, number of orientations, etc. Other parameters I just went with default.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!