【问题标题】:Gaussian filters create holes in histogram高斯滤波器在直方图中创建孔
【发布时间】:2019-09-29 12:04:48
【问题描述】:

我正在使用一个简单的高斯滤镜来模糊下面 MWE 中的图像

import imageio
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage.filters import gaussian_filter

image = imageio.imread('imageio:camera.png')
image_blurred = np.copy(image).astype(np.float)
image_blurred /= 255
image_blurred = gaussian_filter(image_blurred, sigma=3)
image_blurred *= 255
image_blurred = np.round(image_blurred).astype(np.uint8)

plt.close('all')
fig, ax = plt.subplots(2,2, figsize=(6,8))

ax[0][0].imshow(image, cmap='gray')
ax[0][0].set_title('Original')
ax[1][0].hist(image.ravel(), 256, density=True)

ax[0][1].imshow(image_blurred, cmap='gray')
ax[0][1].set_title('Blur 3')
ax[1][1].hist(image_blurred.ravel(), 256, density=True)
plt.show()

问题是,经过高斯滤波器后的图像在直方图中显示了一些孔,我们可以在下面右侧的直方图中看到

这是预期的吗?如果是这样,我可以对图像应用哪种操作来平滑直方图?

【问题讨论】:

  • 当您使用与原版完全相同的垃圾箱时,您是否也会出现孔洞? IE。如果h, bins = ax[1][0].hist(image.ravel(), 256, density=True),那么ax[1][1].hist(image_blurred.ravel(), bins=bins, density=True)?
  • 哇。不。我注意到axes.hist 中的 256 个垃圾箱给了我 257 个垃圾箱而不是 256 个。因此我将 256 更改为 255,它给了我预期的 [0, 1, 2, ..., 255] 垃圾箱。
  • matplotlib hist 文档中所说:如果给定整数,则计算并返回 bin + 1 bin 边

标签: python numpy image-processing computer-vision gaussianblur


【解决方案1】:

注意直方图的 x 轴:第二个的范围较小。如果您在 0-240 范围内创建 256 个 bin,则应该得到 15 个不包含整数值的 bin。因为您将像素值四舍五入,所以这些 bin 将保持为空。

这是因为模糊往往会从图像中移除极值。

在计算整数值图像的直方图时,请确保您的 bin 具有整数宽度。

【讨论】:

    猜你喜欢
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 2014-10-02
    • 2011-02-15
    • 1970-01-01
    • 2018-10-03
    • 2018-01-26
    相关资源
    最近更新 更多