【问题标题】:Solving clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers)使用 RGB 数据将输入数据裁剪到 imshow 的有效范围(浮点数为 [0..1] 或整数为 [0..255])
【发布时间】:2020-09-29 10:20:49
【问题描述】:

我正在使用 matplotlib,但在处理日志消息时遇到了问题。 我的代码如下

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline


# obtain one batch of training images
dataiter = iter(train_loader)
images, labels = dataiter.next()
images = images.numpy() # convert images to numpy for display
classes = ['melanoma', 'nevus', 'seborrheic_keratosis']

# plot the images in the batch, along with the corresponding labels
fig = plt.figure(figsize=(25, 4))
for idx in np.arange(20):
    ax = fig.add_subplot(2, 20/2, idx+1, xticks=[], yticks=[])
    plt.imshow(np.transpose(images[idx], (1, 2, 0)))
    ax.set_title(classes[labels[idx]])

我的图像已被“归一化”,即除以一个数字并缩放,这样三个通道 R、G、B 的平均值分别为 0.485、0.456 和 0.406,而三个通道的标准差分别为 0.229、0.224 和 0.225。

所以我得到的是这条日志消息: Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers)

现在,是的,我查看了this 并尝试了那里给出的解决方案,将我的代码的最后第二行更改为plt.imshow(np.transpose(images[idx].astype('uint8'), (1, 2, 0)))

但是,这样做会极大地改变我的图像色调。

我该如何解决? 哦,是的,在遵循here 之后,我当然可以摆脱日志消息。但是,我不确定这是否是一个好的解决方法。我的图像与日志消息一起显示得很好,所以我认为抑制消息不是一个坏主意。但我正在寻找确认和/或修复,以便根本不生成任何消息。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    你说你试过plt.imshow(np.transpose(images[idx].astype('uint8'), (1, 2, 0)))

    从那行我可以理解你正在使用octal base
    Numpy 需要float between 1 to 0 int between 255 to 0

    所以如果你像这样除以 8:

    plt.imshow(np.transpose(images[idx].astype('uint8'), (1/8, 2/8, 0/8)))

    它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2018-09-13
      • 2021-02-28
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2020-06-01
      相关资源
      最近更新 更多