Python 使用 RGB 数据将输入数据剪切到 imshow 的有效范围([0..1] 表示浮点数或 [0..255] 表示整数)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49643907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:10:46  来源:igfitidea点击:

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

pythonmatplotlibnormalization

提问by Ankita Shinde

I tried to run the graph cut algorithm for a slice of an MRI after converting it into PNG format. I keep encountering the following problem:

在将 MRI 切片转换为 PNG 格式后,我尝试对其运行图形切割算法。我一直遇到以下问题:

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

This is even after setting vminand vmaxas follows:

这甚至设置后vminvmax如下:

plt.imshow(out, vmin=0, vmax=255)

回答by Dat Nguyen

Cast the image to np.uint8after scaling [0, 255]range will dismiss this warning. It seems like a feature in matplotlib, as discussed in this issue.

将图像投射到np.uint8缩放[0, 255]范围之后将消除此警告。这似乎是 中的一个功能matplotlib,正如本期讨论的那样。

plt.imshow((out * 255).astype(np.uint8))

回答by Anh-Thi DINH

Instead of plt.imshow(out), use plt.imshow(out.astype('uint8')). That's it!

而不是plt.imshow(out),使用plt.imshow(out.astype('uint8'))。就是这样!

回答by L.YS

If you want to show it, you can use img/255.

如果你想显示它,你可以使用img/255.

Or

或者

np.array(img,np.int32)

The reason is that if the color intensity is a float, then matplotlib expectsit to range from 0 to 1. If an int, then it expects 0 to 255. So you can either force all the numbers to int or scale them all by 1/255.

原因是如果颜色强度是一个浮点数,那么matplotlib 期望它的范围从 0 到 1。如果是 int,那么它期望是 0 到 255。所以你可以强制所有数字为 int 或将它们全部缩放 1 /255。