为什么使用 scikit-image 加载的图像乘以 0.5 在 matplotlib 中会显示得更亮?

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

我只是想让我在 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 scikit-image brightness
2个回答
2
投票

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

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进行图像处理比较好。


2
投票

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

当您有

uint8
类型的数据时,值范围预计为0到255。图像不需要是uint8。它也可以有其他数据类型。

当您有 float/float64 类型的数据时,值范围是“可能”,预计为 0.0 .. 1.0。 Matplotlib 期望如此。 如果您看到这样的警告,则表明存在问题:

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

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

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

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

uint8

来解决此问题。

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

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