【问题标题】:Numpy matshow produces blank image despite dtype is float尽管 dtype 是浮动的,但 Numpy matshow 会产生空白图像
【发布时间】:2021-11-22 08:07:12
【问题描述】:

我的脚本需要 3 个相同大小的 numpy 数组,使用 dstack 创建 ndarray 并假设将它们显示为 rgb 图像。但是,RGB 图像的最后一部分似乎正在工作,但我无法显示图像,我得到的只是是空白图像。
这就是我从 3 个数组创建图像的方式:

rgb_result=np.dstack((result[3,:,:],result[2,:,:],result[1,:,:]))
    

plt.figure(figsize=(10,6))
plt.matshow(rgb_result*5,fignum=1,aspect='auto')
plt.title(d)
plt.show()

结果是空白图像:

每当我绘制构成“rgb_result”的波段之一时,它都会起作用(这三个图像与此非常相似):

我还检查了每个波段和 ndarray 的 dype,它是 float64。
我的问题是如何使 matshow 工作并以 RGB 显示图像。

【问题讨论】:

  • 您的rgb_resultmin()max() 是什么?当我尝试使用一些随机数据时,您的代码会生成正确的 rgb 图像,所以我认为我们需要查看导致问题的实际数据样本(我没有投反对票)
  • @tdy max() 为 0.0135,min () 为 0.0

标签: python numpy matplotlib imshow


【解决方案1】:

matshow 在后台使用imshow,它期望数据在浮点数介于 0-1 之间或对于整数介于 0-255 之间:

(M, N, 3):具有 RGB 值(0-1 float 或 0-255 int)的图像。

由于rgb_result 包含浮点数,因此应将其归一化为0-1(减去最小值并除以最大值):

plt.matshow((rgb_result-rgb_result.min())/rgb_result.max(), fignum=1, aspect='auto')

或者如果rgb_result保证是正数,就除以最大值:

plt.matshow(rgb_result/rgb_result.max(), fignum=1, aspect='auto')

使用一些假数据,这是标准化前后的输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 2019-02-28
    相关资源
    最近更新 更多