Python3 & OpenCV3 [ Part 2 : Function Image] 😝

Nonpavit Detbun 🔋🍺
2 min readSep 5, 2018

เรียนรู้พื้นฐานคำสั่ง 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กัน 😵

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

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Nonpavit Detbun 🔋🍺
Nonpavit Detbun 🔋🍺

Written by Nonpavit Detbun 🔋🍺

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

No responses yet

Write a response