I've read this post about how to use OpenCV's HOG-based pedestrian detector: How can I detect and track people using OpenCV?
I want to use HOG for detecting other types of objects in images (not just pedestrians). However, the Python binding of HOGDetectMultiScale doesn't seem to give access to the actual HOG features.
Is there any way to use Python + OpenCV to extract the HOG features directly from any image?
If you want fast Python code for HOG features, I've ported the code to Cython: https://github.com/cvondrick/pyvision/blob/master/vision/features.pyx
In python opencv you can compute hog like this:
import cv2 hog = cv2.HOGDescriptor() im = cv2.imread(sample) h = hog.compute(im)
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. 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.
Despite the fact that exist a method as said in previous answers:
hog = cv2.HOGDescriptor()
I would like to post a python implementation you can find on opencv's examples directory, hoping it can be useful to understand HOG funcionallity:
def hog(img): gx = cv2.Sobel(img, cv2.CV_32F, 1, 0) gy = cv2.Sobel(img, cv2.CV_32F, 0, 1) mag, ang = cv2.cartToPolar(gx, gy) bin_n = 16 # Number of bins bin = np.int32(bin_n*ang/(2*np.pi)) bin_cells = [] mag_cells = [] cellx = celly = 8 for i in range(0,img.shape[0]/celly): for j in range(0,img.shape[1]/cellx): bin_cells.append(bin[i*celly : i*celly+celly, j*cellx : j*cellx+cellx]) mag_cells.append(mag[i*celly : i*celly+celly, j*cellx : j*cellx+cellx]) hists = [np