【问题标题】:Python Histogram of Oriented Gradients定向梯度的 Python 直方图
【发布时间】:2017-12-10 20:22:36
【问题描述】:

我正在尝试实现this 版本的定向梯度直方图(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()

一个示例输入和输出是:

输入:

输出:

为什么输出如此混乱?我什至在上面的 skimage 链接中尝试了宇航员的图像。为此,我也遇到了很多混乱,并且输出与链接中显示的完全不同。我怎样才能解决这个问题?

【问题讨论】:

    标签: python image-processing scikit-image histogram-of-oriented-gradients


    【解决方案1】:

    只需将值0.02 更改为0.5 之类的值。这会使它变暗。

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

    改为

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

    【讨论】:

      【解决方案2】:

      我不知道为什么,但错误是由于读取图像并使用 opencv 将其转换为灰度。像教程建议的那样用 matplotlib 做这两个,给了我正确的结果。

      【讨论】:

        【解决方案3】:

        我可以用您的代码重现您的错误并得到相同的混乱图像。我稍微修改了你的代码,特别是改变了轴,我得到了以下输出。

        这是修改后的代码。

        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()
        

        【讨论】:

        • 它对我来说效果很好,in_range=(0, 10) 显示了一个很好的图像。
        猜你喜欢
        • 2011-11-30
        • 2011-08-01
        • 2013-06-22
        • 2015-01-21
        • 2013-11-08
        • 2012-07-15
        • 2018-11-03
        • 1970-01-01
        相关资源
        最近更新 更多