Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get HOG image features from OpenCV + Python?

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?

like image 459
lubar Avatar asked May 22 '11 19:05

lubar


People also ask

How do you extract HOG features?

The HOG features are extracted from local regions with 16 × 16 pixels. Histograms of edge gradients with 8 orientations are calculated from each of 4 × 4 local cells. The edge gradients and orientations are obtained by applying Sobel filters. Thus the total number of HOG features becomes 128 = 8 × (4 × 4).

What is the output of HOG?

In the case of the HOG feature descriptor, the input image is of size 64 x 128 x 3 and the output feature vector is of length 3780.

What is HOG feature in image processing?

Histogram of Oriented Gradients, also known as HOG, is a feature descriptor like the Canny Edge Detector, SIFT (Scale Invariant and Feature Transform) . It is used in computer vision and image processing for the purpose of object detection.


2 Answers

In python opencv you can compute hog like this:

 import cv2  hog = cv2.HOGDescriptor()  im = cv2.imread(sample)  h = hog.compute(im) 
like image 152
ton4eg Avatar answered Oct 08 '22 19:10

ton4eg


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.

like image 34
mdilip Avatar answered Oct 08 '22 18:10

mdilip