0

I tried to find the hog features using Get HOG image features from OpenCV + Python?

but whenever I run the below stub, it causes my user session to log out and when I login back to os, then all the windows are closed.

import cv2
img=cv2.imread('Figure_1.png')
print(img.shape)
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
hog=cv2.HOGDescriptor()
m=hog.compute(img)
print(img.shape)
cv2.imshow('hog',m)
cv2.waitKey()
cv2.DestroyAllWindows()

Can somebody please tell why I am witnessing this behaviour and any suggestions, if the code is buggy.

dodo
  • 89
  • 7

1 Answers1

0

The code has a few bugs.

  1. The cv2.HOGDescriptor() object must be initialized with a few parameters. This is probably one of the reasons the code crashes.
  2. It's cv2.destroyAllWindows() and not cv2.DestroyAllWindows()
  3. I'm guessing you want to destroy the image window on a key press. To achieve that, the cv2.destroyAllWindows() should be conditioned on the cv2.waitKey()
  4. Also, you are trying to view the Hog in an image window. This is not the correct way to do it. The scikit-learn package has functions to let you view the HOG. An example of this is here.

The corrected code is:

import cv2
img=cv2.imread('Figure_1.png')
print(img.shape)
print(img.shape)
winSize = (64,64)
blockSize = (16,16)
blockStride = (8,8)
cellSize = (8,8)
nbins = 9

hog=cv2.HOGDescriptor(winSize,blockSize,blockStride,cellSize,nbins)
m=hog.compute(img)
print(img.shape)
cv2.imshow('hog',img)
if cv2.waitKey() == 27: #27 refers to the Esc key
    cv2.destroyAllWindows()
Shawn Mathew
  • 2,198
  • 1
  • 14
  • 31