【问题标题】:Printing one color using imshow [closed]使用 imshow 打印一种颜色 [关闭]
【发布时间】:2017-11-13 19:21:43
【问题描述】:

我想使用 RGB 值在屏幕上打印一种颜色,并且输出应该只是一种颜色。例如,如果我给出红色的 RGB 值,我希望输出显示红色。但是当我尝试这段代码时,它不起作用。我错过了什么?

import matplotlib.pyplot as plt
plt.imshow([(255, 0, 0)])
plt.show()

输出为:

【问题讨论】:

  • 您好, imshow 命令将通过标准化数组中的值在屏幕上显示图像。所以你的数组 [255,0,0] 被标准化为 (1,0,0)。然后根据默认颜色图将这些值显示为 3 个像素:x 轴上的 0 为黄色,1 和 2 为紫色。我建议您查看 matplotlib 中的 gallery 以找到您正在尝试执行的操作的示例,并根据需要修改代码。

标签: python matplotlib colors rgb imshow


【解决方案1】:

问题是您正在尝试显示具有1 行和3 列的二维颜色数组。从左到右的像素值为255、0和0。正如@Ben K. 在 cmets 中正确指出的那样,通过这样做,强度值被缩放到范围 0..1 并使用当前颜色图显示。这就是您的代码显示一个黄色像素和两个紫色像素的原因。

如果您希望指定 RGB 值您应该创建一个 3D 数组,其中包含 m 行、n 列和 3 颜色通道(每个 RGB 组件一个颜色通道)。

演示

下面的 sn-p 生成调色板索引的随机数组并显示结果:

In [14]: import numpy as np

In [15]: import matplotlib.pyplot as plt

In [16]: from skimage import io

In [17]: palette = np.array([[255,   0,   0], # index 0: red
    ...:                     [  0, 255,   0], # index 1: green
    ...:                     [  0,   0, 255], # index 2: blue
    ...:                     [255, 255, 255], # index 3: white
    ...:                     [  0,   0,   0], # index 4: black
    ...:                     [255, 255,   0], # index 5: yellow
    ...:                     ], dtype=np.uint8)
    ...: 

In [18]: m, n = 4, 6

In [19]: indices = np.random.randint(0, len(palette), size=(4, 6))

In [20]: indices
Out[20]: 
array([[2, 4, 0, 1, 4, 2],
       [1, 1, 5, 5, 2, 0],
       [4, 4, 3, 3, 0, 4],
       [2, 5, 0, 5, 2, 3]])

In [21]: io.imshow(palette[indices])
Out[21]: <matplotlib.image.AxesImage at 0xdbb8ac8>

您也可以生成随机颜色模式,而不是使用调色板:

In [24]: random_colors = np.uint8(np.random.randint(0, 255, size=(m, n, 3)))

In [24]: random_colors
Out[27]: 
array([[[137,  40,  84],
        [ 42, 142,  25],
        [ 48, 240,  90],
        [ 22,  27, 205],
        [253, 130,  22],
        [137,  33, 252]],

       [[144,  67, 156],
        [155, 208, 130],
        [187, 243, 200],
        [ 88, 171, 116],
        [ 51,  15, 157],
        [ 39,  64, 235]],

       [[ 76,  56, 135],
        [ 20,  38,  46],
        [216,   4, 102],
        [142,  60, 118],
        [ 93, 222, 117],
        [ 53, 138,  39]],

       [[246,  88,  20],
        [219, 114, 172],
        [208,  76, 247],
        [  1, 163,  65],
        [ 76,  83,   8],
        [191,  46,  53]]], dtype=uint8)

In [26]: io.imshow(random_colors)
Out[26]: <matplotlib.image.AxesImage at 0xe6c6a90>

【讨论】:

    【解决方案2】:

    这是由

    产生的输出
    import matplotlib.pyplot as plt
    plt.imshow([(3,0,0),(0,2,0),(0,0,1)])
    plt.colorbar()
    plt.show()
    

    您看到我提供给imshow 的三个元组被解释为矩阵的行:

     3  0  0 
     0  2  0
     0  0  1
    

    数值映射到绘图的颜色。 colorbar 函数显示颜色和数值之间的映射。

    要绘制矩形,请参考this SO question,但将facecolor 参数的值替换为以下一种可能性:

    • A color name,作为字符串。
    • A Hex color code,作为带有前导 # 符号的字符串给出。例如,facecolor='#FF0000' 是鲜红色。
    • 具有 0 到 1 之间的三个值的三元组,指定颜色的(红、绿、蓝)部分。 (不像您在问题中假设的那样是 0 到 255!)

    同样使用edgecolor参数确定矩形边框的颜色,或者使用'None'不画边框。

    【讨论】:

      猜你喜欢
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      • 2013-01-30
      相关资源
      最近更新 更多