Arnaud
Arnaud

Reputation: 5112

OpenCV HOGDescriptor.compute error

I try to compute HOG features on an image. This code:

hog = cv2.HOGDescriptor()
return hog.compute(image)

throws the following error at the second line:

error: ..\..\..\..\opencv\modules\objdetect\src\hog.cpp:630: error: (-215) (unsigned)pt.x <= (unsigned)(grad.cols - blockSize.width) && (unsigned)pt.y <= (unsigned)(grad.rows - blockSize.height) in function cv::HOGCache::getBlock

I checked that image is a valid image. Do you have an idea regarding the source of the problem please?

Upvotes: 1

Views: 4166

Answers (2)

mdilip
mdilip

Reputation: 1136

1. Get Inbuilt Documentation: You can also change HOGDescriptor properties according to your requirements, 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.

Solution to your problem: You can change 'winSize' value to what you want it to be. so that your image size won't be out HoG window area.

Upvotes: 3

Micka
Micka

Reputation: 20130

The error message looks like an images pixel is out of your HoG window area.

As far as I know, HoG Descriptors have some kind of "winSize" property (e.g. 64x128 pixel for the people descriptor afair).

Make sure that your image fits the descriptor window size by resizing the image or selecting the relevant sub-area!

Upvotes: 3

Related Questions