anto jose anto jose - 1 year ago 266
Python Question

Error getting while extracting hog features from set of images using python

I am trying to extract HOG features from set of images But i am getting Memory Error as

hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]


I have copied HOG function from opencv example my sample code is

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)
bins = np.int32(bin_n*ang/(2*np.pi)) # quantizing binvalues in (0...16)
bin_cells = bins[:10,:10], bins[10:,:10], bins[:10,10:], bins[10:,10:]
mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists) # hist is a 64 bit vector
return hist
listdir = os.listdir(path_url)
for file in listdir:
img = cv2.imread(path_url + file)

Answer Source

The problem is with your image set.Since you have downloaded images randomly from google.It may have different sizes and that is causing the Error.Before calling hog function you must resize the image.In opencv you can resize using


In PIL library you can resize using

resizes=img.resize(resolution , Image.ANTIALIAS)

But i must recommend to add a separate preproceesing step.In the preprocessing step you can resize and save all images in your 'preimages' folder to another folder and you can give this as input to your hog extraction program.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download