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

โปรแกรม Python ตรวจจับขอบภาพด้วย OpenCV

ในปัญหานี้เราจะดูวิธีที่ Python ตรวจจับขอบของภาพหรือไฟล์วิดีโอ เพื่อทำได้นี้ เราต้องการไลบรารี OpenCV ไลบรารี OpenCV ออกแบบมาโดยหลักสำหรับ computer vision เป็น open source ต้นทางออกแบบโดย Intel ฟรีสำหรับใช้ภายใต้ open-source BSD license

เพื่อใช้ฟังก์ชัน OpenCV เราต้องดาวน์โหลดด้วย pip OpenCV จะดาวน์โหลดโมดูล Numpy มาด้วย ซึ่งก็จะต้องใช้เช่นกัน

sudo pip3 install opencv-python

เป็น input ในกรณีนี้ เราใช้ไฟล์วิดีโอหนึ่งไฟล์ เราสามารถใช้ webcam เพื่อดู real-time effect ของขั้นตอนตรวจจับขอบนี้ได้

ที่นี่เราใช้ไฟล์วิดีโอ ภาพจริง (frame ของวิดีโอ) เป็นแบบนี้:

โปรแกรม Python ตรวจจับขอบภาพด้วย OpenCV

Edge Detection ทำงานอย่างไร?

เพื่อตรวจจับขอบ มีการสังเกตทางคณิตศาสตร์บางอย่างเพื่อตรวจสอบว่า brightness ของ pixels เปลี่ยนแปลงชัดเจนหรือไม่

เราต้องหา gradient สำหรับ grayscale version ของภาพเรา ใน computer vision เมื่อ pixels เปลี่ยนจากดำเป็นขาว ถูกบันทึกเป็น positive slope สำหรับการเปลี่ยนจากขาวเป็นดำ เป็น negative slope

Edge Detection โดยใช้ Derivative ของภาพ

ภาพถูกเก็บเป็น matrix ซึ่งแต่ละ element ของ matrix เก็บข้อมูลเกี่ยวกับ pixels ทั้งหมด เพื่อหา derivation เราต้องการ Laplacian operator เพื่อได้ Laplacian ก่อนอื่นเราต้องการ Sobel derivatives Sobel derivatives ใช้ได้ gradient variation ของภาพ

Horizontal Sobel Derivative (Sobel X)

Sobel derivative นี้ได้จาก convolution ของภาพจริงและ matrix อีกตัว (เรียกว่า kernel) kernel เป็น matrix 3x3 สำหรับกรณีง่าย

มีฟังก์ชันชื่อ sobel() ใช้ฟังก์ชันนี้หา sobel derivatives ได้ เมื่อพยายามหา Sobel x ส่วน y ควรเป็น 0

ตัวอย่างโค้ด

import cv2
import numpy as np

capture = cv2.VideoCapture('sample_video.mp4')
while(capture.isOpened()):
    ret, frame = capture.read()
    hsv_col = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    # The Sobelx Method
    sobelx = cv2.Sobel(frame, cv2.CV_64F, 1, 0, ksize=5)
    cv2.imshow('SobelX', sobelx)
    k = cv2.waitKey(5) & 0xFF
    if k == 27:  # 27 is ASCII for ESC key
        break

capture.release()
cv2.destroyAllWindows()

ผลลัพธ์

โปรแกรม Python ตรวจจับขอบภาพด้วย OpenCV

Vertical Sobel Derivative (Sobel Y)

เช่นเดียวกัน เราสามารถหา Vertical Sobel derivative ด้วยฟังก์ชัน sobel() ในกรณีนี้ ส่วน x จะเป็น 0

ตัวอย่างโค้ด

import cv2
import numpy as np

capture = cv2.VideoCapture('sample_video.mp4')
while(capture.isOpened()):
    ret, frame = capture.read()
    hsv_col = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    # The Sobely Method
    sobely = cv2.Sobel(frame, cv2.CV_64F, 0, 1, ksize=5)
    cv2.imshow('SobelY', sobely)
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

capture.release()
cv2.destroyAllWindows()

ผลลัพธ์

โปรแกรม Python ตรวจจับขอบภาพด้วย OpenCV

Laplacian Derivative

สุดท้ายเราจะดู Laplacian Derivative ของภาพ มีฟังก์ชันชื่อ Laplacian() ใช้ได้ derivative

ตัวอย่างโค้ด

import cv2
import numpy as np

capture = cv2.VideoCapture('sample_video.mp4')
while(capture.isOpened()):
    ret, frame = capture.read()
    hsv_col = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    # The Laplacian Method
    laplacian = cv2.Laplacian(frame, cv2.CV_64F)
    cv2.imshow('Laplacian', laplacian)
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

capture.release()
cv2.destroyAllWindows()

ผลลัพธ์

โปรแกรม Python ตรวจจับขอบภาพด้วย OpenCV