Để phân tích ảnh ta dùng OpenCV (Open Source Computer Vision Library) Python. Tên thư viện import sau cài opencv là cv2.
Ví dụ dưới tìm contour trong file ảnh. Contour giúp nhận diện hình dạng. Contour là đường nối các điểm trên biên có cùng cường độ. Hàm findContours tìm contour, drawContours vẽ contour.
Cú pháp
cv2.findContours(image, mode, method) cv2.drawContours(img, contours, contourIdx, color, thickness)
Ví dụ
Dùng ảnh input sau. Chạy chương trình lấy contour.

Thấy 3 hình dạng. Vẽ contour quanh chúng.
Ví dụ
import cv2
# Load ảnh
image = cv2.imread("path to image file")
# Thay đổi không gian màu
LUV = cv2.cvtColor(image, cv2.COLOR_BGR2LUV)
# Tìm cạnh
edges = cv2.Canny(LUV, 10, 100)
# Tìm Contours
contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# Số contour
print("Số Contour:", len(contours))
# Vẽ viền vàng quanh 2 contour
cv2.drawContours(image, contours, 0, (0, 230, 255), 6)
cv2.drawContours(image, contours, 2, (0, 230, 255), 6)
# Hiển thị
cv2.imshow('Contours', image)
cv2.waitKey(0)
Kết quả
Số Contour tìm thấy = 3
Hình ảnh output:
