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

วิธีเพิ่มสีเฉด (tint) ให้ภาพขาวดำด้วย scikit-learn ใน Python

ค่าของ 'R', 'G', 'B' จะถูกเปลี่ยนแปลงและนำไปคูณกับภาพต้นฉบับเพื่อให้ได้เฉดสีที่ต้องการ

ด้านล่างคือโปรแกรม Python ที่ใช้ scikit-learn (sklearn) ไลบรารีสำหรับ Machine Learning เพื่อทำงานนี้:

ตัวอย่าง

import matplotlib.pyplot as plt
from skimage import data
from skimage import color
path = "path to puppy_1.jpg"
orig_img = io.imread(path)
grayscale_img = rgb2gray(orig_img)
image = color.gray2rgb(grayscale_img)
red_multiplier = [0.7, 0, 0]
yellow_multiplier = [1, 0.9, 0]
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4), sharex=True, sharey=True)
ax1.imshow(red_multiplier * image)
ax1.set_title('Original image')
ax2.imshow(yellow_multiplier * image)
ax2.set_title('Tinted image')

ผลลัพธ์

วิธีเพิ่มสีเฉด (tint) ให้ภาพขาวดำด้วย scikit-learn ใน Python

คำอธิบาย

  • นำเข้าแพ็กเกจที่จำเป็น
  • กำหนดพาธที่เก็บภาพ
  • ใช้ฟังก์ชัน 'imread' อ่านภาพ
  • ใช้ 'imshow' แสดงภาพ
  • ฟังก์ชัน 'rgb2gray' แปลงจากพื้นที่สี RGB เป็น grayscale
  • ฟังก์ชัน 'gray2rgb' แปลงจาก grayscale กลับเป็น RGB (3 ช่อง)
  • ใช้ matplotlib พล็อตข้อมูล
  • กำหนดค่า R, G, B multipliers และนำไปคูณกับภาพ
  • แสดงผลบนคอนโซล

หมายเหตุ: โค้ดต้นฉบับมีการนำเข้า `io` ขาดหายไป ควรเพิ่ม `from skimage import io`