Python OpenCV 畫圓 circle

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

使用範例

以下範例為 cv2.circle 的用法,一開始先建立 640*480 大小的影像,
再來將這張影像填充為 128 灰階值,
接著用 cv2.circle 在 (300, 300) 座標位置上畫圓,並且指定圓的顏色為紅色,
最後再用 imshow 顯示結果。
詳細程式碼如下:

opencv-circle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/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)

red_color = (0, 0, 255) # BGR
cv2.circle(image, (300, 200), 50, red_color, -1)

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

結果如下圖所示:

函式用法與參數解釋:
cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]])
img – Image where the circle is drawn.
center – Center of the circle.
radius – Radius of the circle.
color – Circle color.
thickness – Thickness of the circle outline, if positive. Negative thickness means that a filled circle is to be drawn.
lineType – Type of the circle boundary. See the line() description.
shift – Number of fractional bits in the coordinates of the center and in the radius value.

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

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