5

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?

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
Sibi
  • 2,221
  • 6
  • 24
  • 37

3 Answers3

1

I could reproduce your error with your code and got the same cluttered image. I modified your code a bit, specifically changing the axis and i got the following output.

enter image description here

Here's the modified code.

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

filename = r"pathtoimage\hockey.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, ax = plt.subplots(1, 2, figsize=(20, 10), sharex=True, sharey=True)

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

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

ax[1].axis('off')
ax[1].imshow(hog_image, cmap=plt.cm.gray)
ax[1].set_title('Histogram of Oriented Gradients')
ax[1].set_adjustable('box-forced')

plt.show()
Shawn Mathew
  • 2,198
  • 1
  • 14
  • 31
0

I'm not sure why but the mistake is due to reading the image and converting it to grayscale with opencv. Doing both with matplotlib like the tutorial suggests, gives me the correct result.

Sibi
  • 2,221
  • 6
  • 24
  • 37
0

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))
Nik
  • 1,780
  • 1
  • 14
  • 23
Dorian
  • 1
  • 2