Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีแสดงภาพใน Matplotlib ด้วยสีต่างกันในแต่ละช่องสี (Channel)

ในการตัดภาพเป็นช่องสี Red, Green และ Blue ด้วย misc.imread เราสามารถทำตามขั้นตอนต่อไปนี้:

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots
  • อ่านภาพจากไฟล์เข้าเป็นอาร์เรย์
  • สร้างลิสต์ของ colormaps และ titles
  • สร้าง figure และชุด subplots
  • Zip axes, images, titles และ colormaps
  • วนลูป zipped objs และตั้งค่า title ของแต่ละภาพช่องสี
  • เพื่อแสดงรูป ใช้เมธอด 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()

รูปภาพ Input

วิธีแสดงภาพใน Matplotlib ด้วยสีต่างกันในแต่ละช่องสี (Channel)วิธีแสดงภาพใน Matplotlib ด้วยสีต่างกันในแต่ละช่องสี (Channel)

รูปภาพ Output

วิธีแสดงภาพใน Matplotlib ด้วยสีต่างกันในแต่ละช่องสี (Channel)วิธีแสดงภาพใน Matplotlib ด้วยสีต่างกันในแต่ละช่องสี (Channel)