Python3 & OpenCV3 [ Part 2 : Function Image] 😝

เรียนรู้พื้นฐานคำสั่ง OpenCV3 : Image

จากบทความที่แล้ว เราได้สอนการติดตั้งและได้ลอง run OpenCV กันไปบ้างแล้ว
ครั้งนี้เราจะมาดูคำสั่งพื้นฐานในการจัดการ Imageกัน 💻

open terminal in VS Code (Ctrl + `)

workon opencv

Basic Function OpenCV

import import function/class

  • import cv2 : เรียกใช้ function cv2

Ex. import function/class

cv2.imread(‘file_image’, type_color)

  • cv2.imread() : อ่านภาพจากไฟล์
  • cv2.IMREAD_COLOR : โหลดรูปภาพสี มีความโปร่งใสของรูปภาพ เป็นค่าเริ่มต้น
  • cv2.IMREAD_GRAYSCALE : โหลดรูปภาพในโหมดระดับสีเทา (grayscale mode)
  • cv2.IMREAD_UNCHANGED : โหลดรูปภาพโดยเรียกใช้ alpha channel

cv2.imshow(‘windows_name’, file_image_from_imread())
cv2.waitKey(millisecond)
cv2.destroyAllWindows()
cv2.destroyWindow()

  • cv2.imshow() : แสดงรูปภาพที่ได้มาจาก cv2.imread()
  • cv2.waitKey() : รอคีย์บอร์ดตามที่กำหนดเวลาไว้ในหน่วยมิลลิวินาที ถ้าใช้ 0 จะไม่มีการรอ
  • cv2.destroyAllWindows() : ใช้ทำลายหน้าต่างทั้งหมดที่ถูกสร้างขึ้น หากต้องการทำลายบางหน้าต่างให้ใช้คำสั่ง cv2.destroyWindow()

Ex. cv2.imread() , cv2.imshow() , cv2.waitKey() , cv2.destroyAllWindows()

cv2.imwrite(‘image_name’, file_image_from_imread())

  • cv2.imwrite() : สร้างรูปภาพจากไฟล์ภาพที่กำหนด

Ex. cv2.imwrite()

Install matplotlib

pip install matplotlib
  • numpy ใช้ในการคำนวณเชิงตัวเลขและวิเคราะห์ข้อมูล
  • matplotlib ใช้แสดงผลข้อมูลออกมาเป็นกราฟหรือแผนภาพต่างๆ

Ex. matplotlib

cv2.namedWindow(‘windows_name’, ‘type_size’)

  • cv2.nameWindow : กำหนดชื่อ(ชื่อเดียวกับชื่อไฟล์รูปภาพที่ใช้ใน imread(),imshow())และขนาดของหน้าต่างที่แสดง
  • cv2.WINDOW_AUTOSIZE : ค่าหน้าต่างแบบ auto
  • cv2.WINDOW_NORMAL : ค่าหน้าต่างแบบกำหนดเอง
  • cv2. WINDOW_OPENGL : ค่าหน้าต่างจาก OpenGL

Ex. cv2.namedWindow()

……………………………………………………………………………………….

Source Code :

แล้วพบกันใหม่ Part 3เราจะมารู้จักกับ function Videoกัน 😵

……………………………………………………………………………………….

Nonpavit Detbun 🔋🍺

Written by

I’m Electrical Engineering. 📟 I have an interest in Web App, Mobile App, IoT and Microcontroller 🧐

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade