Python OpenCV 畫橢圓 ellipse

本篇將介紹如何使用 OpenCV 與 Python 來畫橢圓 ellipse,在寫 Python 影像處理程式時常會用到 OpenCV cv2.ellipse 畫橢圓的功能,接下來介紹怎麼使用 Python 搭配 OpenCV 模組來畫橢圓 ellipse。

使用範例

詳細程式碼如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np

# Create a blank 640x480 black image
image = np.zeros((480, 640, 3), np.uint8)
# Fill image with gray color(set each pixel to gray)
image[:] = (128, 128, 128)

center = (300, 300)
axes = (100, 50)
angle = 45
startAngle = 0
endAngle = 270
thickness = -1
color = (0, 0, 255) # BGR red
cv2.ellipse(image, center, axes, angle, startAngle, endAngle, color, thickness)
#cv2.ellipse(image, (300, 300), (100, 50), 45, 0, 270, (0, 0, 255), -1)

cv2.imshow('Result', image)
cv2.waitKey(0)

結果如下圖所示:

cv2.ellipse 參數的詳細細節請參考這裡

其它相關文章推薦
Python OpenCV 畫線條 line
Python OpenCV 畫圓 circle
Python OpenCV 畫矩形 rectangle
Python OpenCV 畫多邊形 polylines
Python OpenCV 繪製文字 putText