OpenCV เป็นไลบรารี Open Source Computer Vision ใน Python ซึ่งใช้กันอย่างแพร่หลายเพื่อการวิจัยด้านปัญญาประดิษฐ์และการเรียนรู้ของเครื่อง Computer Vision Library เช่น OpenCV เกี่ยวข้องกับการประมวลผลภาพ เราสามารถใช้ OpenCV เพื่ออ่านรูปภาพและนำไปใช้เพื่อการพัฒนาต่อไปได้
สมมติว่าเราต้องการสร้างแอปพลิเคชันที่อ่านรูปภาพและแสดงในหน้าต่างโดยใช้ OpenCV
ติดตั้ง OpenCV โดยใช้คำสั่งต่อไปนี้ -
pip install opencv-python
ต่อไป ทำตามขั้นตอนด้านล่าง -
-
ติดตั้ง OpenCV ในสภาพแวดล้อมและนำเข้าไลบรารีโดยใช้ นำเข้า cv2 .
-
นำเข้า NumPy และ PIL (Pillow Package) สำหรับคำนวณภาพ
-
โหลดรูปภาพโดยใช้ imread(image_location) ฟังก์ชัน
-
แยกสี RGB ของรูปภาพโดยใช้ split(image) ฟังก์ชัน
-
รวมสีของรูปภาพโดยใช้ merge(rgb) ฟังก์ชัน
-
แปลงเมทริกซ์หลายมิติเป็นรูปภาพ
-
แปลงรูปภาพที่กำหนดโดยใช้ PhotoImage(image=file) ฟังก์ชัน
-
เริ่มต้นป้ายกำกับและแสดงรูปภาพ
ตัวอย่าง
#Import the tkinter library
from tkinter import *
import numpy as np
import cv2
from PIL import Image, ImageTk
#Create an instance of tkinter frame
win = Tk()
win.geometry("700x550")
#Load the image
img = cv2.imread('tutorialspoint.png')
#Rearrange colors
blue,green,red = cv2.split(img)
img = cv2.merge((red,green,blue))
im = Image.fromarray(img)
imgtk = ImageTk.PhotoImage(image=im)
#Create a Label to display the image
Label(win, image= imgtk).pack()
win.mainloop() ผลลัพธ์
การเรียกใช้โค้ดด้านบนจะเป็นการโหลดและแสดงรูปภาพในหน้าต่าง

ตรวจสอบให้แน่ใจว่ารูปภาพ 'tutorialspoint.png ' อยู่ในโฟลเดอร์เดียวกับโปรแกรม