【问题标题】:Valid data for imshowimshow 的有效数据
【发布时间】:2019-07-09 02:04:21
【问题描述】:

我想用 numpy 创建一个新图像,我想创建的图像是这样的:

但是当运行我的脚本以显示图像时,会显示如下内容:

不显示半圆并发送消息说 “使用 RGB 数据将输入数据剪切到 imshow 的有效范围(浮点数为 [0..1] 或整数为 [0..255]) 。”

我的代码是这样的:

import math
import numpy as np
import matplotlib.pyplot as plt

new_image = np.zeros((300, 250, 3))

for x in range(300):
    for y in range(250):
        if (180 <= x <= 240) and (100 <= y <=200):
            new_image[x, y , 2] = 255
        elif (90 <= x <= 150) and ((-2*x/3)+110 <= y <= (2*x/3)-10):
            new_image[x,y,0] = 255
        elif (60 <= x <=120) and (150 <= y <= 200+math.sqrt(900-(x-90)^2)):
            new_image[x,y,0] = 255
            new_image[x,y,1] = 255
        else:
            new_image[x,y,0] = 255
            new_image[x,y,1] = 255
            new_image[x,y,2] = 255


# First Plot
plt.figure()
plt.imshow(new_image)
plt.axis('off')
plt.show()

【问题讨论】:

  • plt.imshow((new_image).astype(np.uint8)) 应该删除警告。你能解释一下elif (60 &lt;= x &lt;=120) and (150 &lt;= y &lt;= 200+math.sqrt(900-(x-90)^2)):背后的原因吗?
  • 请注意,python 中的^ 表示XOR - 如果您想将某个x 提高到2 的幂,请使用x ** 2
  • @Jeppe 这是因为这是图形的方程,我们在 Matlab 中也这样做,但我们需要将代码传递给 python,但在 Python 中不显示半圆。
  • @Jeppe 谢谢我忘了改变它。现在有效。
  • 问题与scikit-learnscipy 无关 - 请不要向无关标签发送垃圾邮件(已删除并替换为matplotlib)。

标签: python-3.x numpy matplotlib


【解决方案1】:

在您的情况下,new_image 是一个浮点数组,但是当您分配颜色值时,您需要给出 0 到 1 之间的值,但您给出的是 255,它是一个 int。

您可以为颜色值提供 int (0-255),但您的图像数组应该由 int 组成。

所以你可以通过使用来摆脱错误

new_image = np.zeros((300, 250, 3), dtype = int)

【讨论】:

    猜你喜欢
    • 2022-01-25
    • 2021-02-28
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 2019-02-24
    • 2018-09-13
    相关资源
    最近更新 更多