pyplot显示不正确,图像太暗 [英] Bad display with pyplot, image too dark

查看:99
本文介绍了pyplot显示不正确,图像太暗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on an image processing problem.

我创建了一个将盐和胡椒粉噪点应用到图像的函数. 功能如下:

I create a function that applies a salt and pepper noise to an image. Here is the function:

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

我想显示结果时出现问题.

Problems happen when I want to display the result.

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

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

I can not post the image directly but here is the link:

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

The picture is darker. But when I display the value of the pixels

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

But when I display the value of the pixels between the 2 images the values ​​are the same:

[[ 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,但我找不到解决方法

Apparently the problem comes from the display plt.imshow, but I can not find a solution

推荐答案

查看

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

When using scalar data and no explicit norm, vmin and vmax define the data range that the colormap covers. By default, the colormap covers the complete value range of the supplied data. vmin, vmax are ignored if the norm parameter is used.

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

Therefore, if no values are specified for these parameters, the range of luminosity is based on the actual data values, with the minimum value being set to black and the maximum value being set to white. This is useful in visualization, but not in comparisons, as you found out. Therefore, just set vmin and vmax to appropriate values (probably 0 and 255).

这篇关于pyplot显示不正确,图像太暗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆