如何在 Matplotlib 中以具有不同通道的不同顏色顯示影像?
若要使用 misc.imread 將影像切片為紅色、綠色和藍色通道,我們可以採取以下步驟 −
- 設定圖形大小並調整子圖之間以及周圍的邊距。
- 從檔案中讀取影像到一個數組中。
- 製作顏色圖和標題列表。
- 建立一個圖形和一組子圖。
- 將座標軸、影像、標題和顏色圖壓縮在一起。
- 迭代壓縮的物件,並設定每個通道影像的標題。
- 若要顯示圖形,請使用show() 方法。
示例
import matplotlib.pyplot as 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, axes = 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()
輸入影像
輸出影像
廣告