Python Histogram of Oriented Gradients

Viewed 7615

I am trying to implement this version of Histogram of Oriented Gradients(HOG). My code is below. The only difference in my code is that I've used opencv to read the image and convert it to grayscale.

import cv2
import matplotlib.pyplot as plt
from skimage.feature import hog
from skimage import data, color, exposure

filename = 'match1/hockey15.jpg'
im = cv2.imread(filename)
gr = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) 

print im.shape
image = gr

fd, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16),
                    cells_per_block=(1, 1), visualise=True)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True)

ax1.axis('off')
ax1.imshow(image, cmap=plt.cm.gray)
ax1.set_title('Input image')
ax1.set_adjustable('box-forced')

# Rescale histogram for better display
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 0.02))

ax2.axis('off')
ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray)
ax2.set_title('Histogram of Oriented Gradients')
ax1.set_adjustable('box-forced')
plt.show()

A sample input and output are:

Input:

enter image description here

Output:

enter image description here

Why is the output so cluttered? I have even tried the image with the astronaut in the skimage link above. For this too, I get a lot of clutter and the output is not at all like what is shown in the link. How can I fix this?

3 Answers

Just change the value 0.02 to something like 0.5. That will make it darker.

hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 0.02))

Change to

hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 0.5))
Related