Để cắt hình ảnh thành các kênh Red, Green và Blue với misc.imread, chúng ta có thể thực hiện các bước sau:
- Thiết lập kích thước figure và điều chỉnh padding giữa và xung quanh subplots.
- Đọc hình ảnh từ file vào mảng.
- Tạo danh sách colormaps và titles.
- Tạo một figure và một tập subplots.
- Zip các axes, images, titles và colormaps.
- Lặp qua các đối tượng zip và đặt title cho mỗi hình ảnh kênh.
- Để hiển thị figure, dùng phương thức show().
Ví dụ
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()
Hình ảnh đầu vào


Hình ảnh đầu ra

