1. 程式人生 > >Python 用 OpenCV 畫點和圓

Python 用 OpenCV 畫點和圓

利用 opencv 裡自帶的circle() 函式可以繪製以一個點為圓心特定半徑的圓,其函式的宣告如下:

cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]])

函式引數含義如下:

  • img:要畫的圓所在的矩形或影象
  • center:圓心座標,如 (100, 100)
  • radius:半徑,如 10
  • color:圓邊框顏色,如 (0, 0, 255) 紅色,BGR
  • thickness:正值表示圓邊框寬度. 負值表示畫一個填充圓形
  • lineType:圓邊框線型,可為 0,4,8
  • shift:圓心座標和半徑的小數點位數

畫點實際上就是畫半徑很小的實心圓,畫點和畫圓的完整程式碼如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@Time    : 2018-11-09 21:39
@Author  : jianjun.wang
@Email   : [email protected]
"""

import numpy as np
import cv2 as cv
 
img = np.zeros((320, 320, 3), np.uint8) #生成一個空灰度影象
print img.shape # 輸出:(480, 480, 3)

point_size = 1
point_color = (0, 0, 255) # BGR
thickness =
4 # 可以為 0 、4、8 # 要畫的點的座標 points_list = [(160, 160), (136, 160), (150, 200), (200, 180), (120, 150), (145, 180)] for point in points_list: cv.circle(img, point, point_size, point_color, thickness) # 畫圓,圓心為:(160, 160),半徑為:60,顏色為:point_color,實心線 cv.circle(img, (160, 160), 60, point_color, 0) cv.namedWindow("image") cv.
imshow('image', img) cv.waitKey (10000) # 顯示 10000 ms 即 10s 後消失 cv.destroyAllWindows()

執行後效果如下: 在這裡插入圖片描述