当我将图像乘以 0.5 时,matplotlib 显示它变得更亮?

问题描述 投票:0回答:3

我只是想让我在 python 中使用 scikit-image 加载的给定图像变暗。

如果我这样做:

test_image = io.imread("TestProvs2.bmp")

test_image = test_image * 0.5

show_map(test_image)

RGB 值数组确实按比例缩小,因此每个 RGB 值都是 127.5。

然而生成的图像具有最大值(左=原始,右=新):

python matplotlib image-processing multiplication brightness
3个回答
1
投票

当您读取图像时,图像的数据类型是

uint8
。当你除以
0.5
时,python 将其转换为
float64
但图像必须是
uint8
,所以它给出了这个错误

Lossy conversion from float64 to uint8. Range [0.0, 127.5]. Convert image to uint8 prior to saving to suppress this warning.

你要做的就是手动将其投射到

uint8

test_image = (test_image * 0.5).astype(np.uint8)

不要忘记导入

numpy

import numpy as np

一般情况下,使用

OpenCV
进行图像处理比较好。


0
投票

使用

skimage.exposure.adjust_gamma
,您可以将图像的直方图移向更暗或更亮的输出:

from skimage import io, exposure

img = io.imread('myimage.png')
gamma_corrected = exposure.adjust_gamma(img, 5)

io.imshow(gamma_corrected)
io.show() 

原始和修正后的测试图像:


0
投票

问题确实是数据类型,以及 matplotlib 如何处理它们。

当您有

uint8
类型的数据时,值范围预计为0 .. 255。

当您有 float/float64 类型的数据时,值范围是可能预计为 0.0 ... 1.0。

由于您只是将值乘以 0.5,因此您的值范围是 0.0 .. 127.5

考虑到这一点,虽然期望范围高达 1.0,但会耗尽所有像素。

您可以通过适当缩放或显式转换为

uint8
来解决此问题。

matplotlib 非常适合用于显示目的。您只需检查文档,了解任何 API 将如何处理您的数据。

© www.soinside.com 2019 - 2024. All rights reserved.