Python 用 OpenCV 畫點和圓 (2)


利用 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()

運行後效果如下:
在這裏插入圖片描述

原文英文文檔
本文地址

Python 安裝 OpenCV 及顯示圖像 (1)

Python 用 OpenCV 畫點和圓 (2)

Python 用 OpenCV 畫直線 (3)

Python 用 OpenCV 畫矩形 (4)

Python 用 OpenCV 畫橢圓 (5)

Python 用 OpenCV 顯示文字 (6)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章