简体   繁体   English

如何在灰度图像上绘制彩色像素?

[英]How to draw a colored pixel on a grayscale image?

I have a bidimensional ndarray.我有一个二维 ndarray。 Each element is an integer.每个元素都是一个整数。 I display the matrix in this way:我以这种方式显示矩阵:

plt.figure()
plt.imshow(img, cmap='Greys')

where img is the bidimensional array.其中img是二维数组。 Now I would to highlight some points on the image: for example I would that the pixel in (x,y) is red (I don't want change the real pixel value, but only display the image with the red pixel).现在我要突出显示图像上的一些点:例如,我希望 (x,y) 中的像素为红色(我不想更改实际像素值,但只显示带有红色像素的图像)。

Any suggestion?有什么建议吗?

Here is a code snippet to illustrate the process (the idea is to play with RGB channels):下面是一个代码片段来说明这个过程(这个想法是使用 RGB 通道):

import numpy as np
import matplotlib.pyplot as plt
im = np.random.randint(0, 255, (16, 16))
I = np.dstack([im, im, im])
x = 5
y = 5
I[x, y, :] = [1, 0, 0]
plt.imshow(I, interpolation='nearest' )
plt.imshow(im, interpolation='nearest', cmap='Greys')

where I have arbitrarily chosen x and y equal to 5 (original image being 16x16)我任意选择xy等于 5(原始图像为 16x16)

the last line shows im has been untouched, and the one before that gives the result you want.最后一行显示im未受影响,前一行给出了您想要的结果。

Output:输出:

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM