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

标签 python numpy plot imshow

我有一个二维 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')

我任意选择 xy 等于 5(原始图像为 16x16)

最后一行显示 im 没有被修改,而前一行给出了你想要的结果。

输出:

enter image description here

enter image description here

关于python - 如何在灰度图像上绘制彩色像素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32908424/

相关文章:

r - 无法启动 png() 设备来使用 "paste"函数创建绘图名称

r - 在 r 中使用 ggplot2 创建月份大小的 bin

matlab - 图例与 MATLAB 绘图上的绘图线不匹配

python - 在没有输入的情况下使用 GNU 并行生成 n 个工作线程

python - 使用带约束的 linalg 求解系统

python - 查询保存为 npz 的 NumPy 数组的 NumPy 数组很慢

python - 根据 pandas 中的第四列将数据从一列移动到另外两列之一

numpy - 用numpy实现各向同性平方指数核

python - Python 中的运算符重载 : handling different types and order of parameters

python - Python线程启动开始时间