如何在 Matplotlib 中以不同颜色和不同通道显示图像?
matplotlibpythondata visualization更新于 2025/9/12 13:00:17
要使用 misc.imread 将图像切分为红色、绿色和蓝色通道,我们可以采取以下步骤 −
- 设置图形大小并调整子图之间和周围的填充。
- 将文件中的图像读入数组。
- 制作颜色图和标题列表。
- 创建一个图形和一组子图。
- 压缩轴、图像、标题和颜色图。
- 迭代压缩的对象并设置每个通道图像的标题。
- 要显示图形,请使用 show() 方法。
示例
import matplotlib.pyplot 作为 plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
image = plt.imread('bird.png')
titles = ['With red channel', 'With green channel', 'With blue channel']
cmaps = [plt.cm.Reds_r, plt.cm.Greens_r, plt.cm.Blues_r]
fig, axis = plt.subplots(1, 3)
objs = zip(axes, (image, *image.transpose(2, 0, 1)), titles, cmaps)
for ax, channel, title, cmap in objs:
ax.imshow(channel, cmap=cmap)
ax.set_title(title)
ax.set_xticks(())
ax.set_yticks(())
plt.show()
输入图像
.jpg)
.jpg)
输出图像
.png)
.png)
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

