Phát hiện cạnh là thuật ngữ xác định ranh giới của đối tượng trong hình ảnh. Chúng ta sẽ tìm hiểu về cách phát hiện cạnh bằng cách sử dụng kỹ thuật phát hiện cạnh canny. Cú pháp là hàm phát hiện cạnh canny được đưa ra như sau:
Các bài viết liên quan:
Thông số:
- / path / to / img: đường dẫn tệp của hình ảnh (bắt buộc)
- minVal: Gradient cường độ tối thiểu (bắt buộc)
- maxVal: Độ dốc cường độ tối đa (bắt buộc)
- aperture (khẩu độ): Đây là đối số tùy chọn.
- L2gradient: Giá trị mặc định của nó là false, nếu giá trị là true, Canny () sử dụng một phương trình tính toán đắt tiền hơn để phát hiện các cạnh, cung cấp độ chính xác hơn với chi phí tài nguyên.
Ví dụ 1
Đầu ra
Ví dụ về Real time Edge detection
# import libraries of python OpenCV import cv2 # import Numpy by alias name np import numpy as np # capture frames from a camera cap = cv2.VideoCapture(0) # loop runs if capturing has been initialized while (1): # reads frames from a camera ret, frame = cap.read() # converting BGR to HSV hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # define range of red color in HSV lower_red = np.array([30, 150, 50]) upper_red = np.array([255, 255, 180]) # create a red HSV colour boundary and # threshold HSV image mask = cv2.inRange(hsv, lower_red, upper_red) # Bitwise-AND mask and original image res = cv2.bitwise_and(frame, frame, mask=mask) # Display an original image cv2.imshow('Original', frame) # discovers edges in the input image image and # marks them in the output map edges edges = cv2.Canny(frame, 100, 200) # Display edges in a frame cv2.imshow('Edges', edges) # Wait for Esc key to stop k = cv2.waitKey(5) & 0xFF if k == 27: break # Close the window cap.release() # De-allocate any associated memory usage cv2.destroyAllWindows()
Đầu ra: