Computer >> Máy Tính >  >> Lập trình >> Python

Vẽ hình elip trên hình ảnh bằng OpenCV

Trong chương trình này, chúng ta sẽ vẽ một hình elip trên một hình ảnh bằng cách sử dụng thư viện OpenCV. Chúng tôi sẽ sử dụng hàm OpenCV ellipse () cho tương tự.

Hình ảnh gốc

Vẽ hình elip trên hình ảnh bằng OpenCV

Thuật toán

Step 1: Import cv2.
Step 2: Read the image using imread().
Step 3: Set the center coordinates.
Step 4: Set the axes length.
Step 5: Set the angle.
Step 6: Set start and end angle.
Step 6: Set the color.
Step 7: Set the thickness.
Step 8: Draw the ellipse by passing the above parameters in the cv2.ellipse function along with the original image.
Step 9: Display the final output.

Mã mẫu

import cv2
image = cv2.imread('testimage.jpg')
center_coordinates = (120, 100)

axesLength = (100, 50)
angle = 0
startAngle = 0
endAngle = 360
color = (0, 0, 255)
thickness = 5
image = cv2.ellipse(image, center_coordinates, axesLength, angle, startAngle, endAngle, color, thickness)

cv2.imshow('Ellipse', image)

Đầu ra

Vẽ hình elip trên hình ảnh bằng OpenCV