我有一个二维 ndarray。每个元素都是一个整数。我以这种方式显示矩阵:
plt.figure()
plt.imshow(img, cmap='Greys')
其中 img 是二维数组。 现在我要突出显示图像上的一些点:例如,我希望 (x,y) 中的像素是红色的(我不想更改真实像素值,但只显示带有红色像素的图像)。
有什么建议吗?
最佳答案
这里是一个代码片段来说明这个过程(想法是玩 RGB channel ):
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')
我任意选择 x
和 y
等于 5(原始图像为 16x16)
最后一行显示 im
没有被修改,而前一行给出了你想要的结果。
输出:
关于python - 如何在灰度图像上绘制彩色像素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32908424/