【问题标题】:Bad display with pyplot, image too darkpyplot 显示不好,图像太暗
【发布时间】:2019-04-27 18:18:39
【问题描述】:

我正在处理图像处理问题。

我创建了一个将椒盐噪声应用于图像的函数。 这是函数:

def sp_noise(image,prob):

    res = np.zeros(image.shape,np.uint8)
    for i in range(image.shape[0]):
        for j in range(image.shape[1]):
            rdn = random.random()
            if rdn < prob:
                rdn2 = random.random()
                if rdn2 < 0.5:
                    res[i][j] = 0
                else:
                    res[i][j] = 255
            else:
                res[i][j] = image[i][j]
    return res

当我想显示结果时出现问题。

wood = loadPNGFile('wood.jpeg',rgb=False)
woodSP = sp_noise(bois,0.01)

plt.subplot(1,2,1)
plt.imshow(bois,'gray')
plt.title("Wood")
plt.subplot(1,2,2)
plt.imshow(woodSP,'gray')
plt.title("Wood SP")

我无法直接发布图片,但这是链接:

图片较暗。但是当我显示像素值时

但是当我显示两个图像之间的像素值时,值是相同的:

[[ 99  97  96 ... 118  90  70]
 [110 110 103 ... 116 115 101]
 [ 79  73  65 ...  96 121 121]
 ...
 [ 79  62  46 ... 105 124 113]
 [ 86  98 100 ... 114 119  99]
 [ 96  95  95 ... 116 111  90]]
[[255  97  96 ... 118  90  70]
 [110 110 103 ... 116 115 101]
 [ 79  73  65 ...  96 121 121]
 ...
 [ 79  62  46 ... 105 124 113]
 [ 86  98 100 ... 114 119  99]
 [ 96  95  95 ... 116 111  90]]

我还检查了平均值:

117.79877369007804
117.81332616658703

显然问题来自于显示plt.imshow,但我找不到解决方案

【问题讨论】:

  • 什么是wood.min()wood.max() 在图像中添加噪点之前?
  • 将参数vmin=0vmax=255 添加到imshow(...) 以确保两个调用使用从整数值到灰度级别的相同映射。
  • 最大值为 160,最小值为 9。它适用于 vmin=0 和 vmax=255,谢谢! imshow 会自动映射 0 到 255 吗?

标签: python matplotlib image-processing jupyter-notebook


【解决方案1】:

查看imshowdocumentation,有2个可选参数,vminvmax其中:

当使用标量数据且没有明确的范数时,vmin 和 vmax 定义 颜色图覆盖的数据范围。默认情况下,颜色图覆盖 所提供数据的完整值范围。 vmin, vmax 被忽略 如果使用 norm 参数。

因此,如果没有为这些参数指定值,则亮度范围以实际数据值为准,最小值设置为黑色,最大值设置为白色。正如您所发现的,这在可视化中很有用,但在比较中没有用。因此,只需将vminvmax 设置为适当的值(可能是0 和255)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多