Sib*_*ibi 5 python image-processing scikit-image histogram-of-oriented-gradients
我正在尝试实现这个版本的定向梯度直方图(HOG)。我的代码如下。我的代码中唯一的区别是我曾经opencv
读取图像并将其转换为灰度。
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()
Run Code Online (Sandbox Code Playgroud)
示例输入和输出为:
输入:
输出:
为什么输出如此混乱?我什至在上面的 skimage 链接中尝试了宇航员的图像。为此,我也遇到了很多混乱,并且输出根本不像链接中显示的那样。我怎样才能解决这个问题?
我可以用您的代码重现您的错误,并得到相同的混乱图像。我稍微修改了你的代码,特别是改变了轴,我得到了以下输出。
这是修改后的代码。
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()
Run Code Online (Sandbox Code Playgroud)