OpenCV-Python 是一個 Python 鏈接庫,旨在解決計算機視覺問題。 cv2.circle() 方法用於在任意圖像上畫圓。
語法: cv2.circle(image, center_coordinates, radius, color, thickness)
參數:
image:要畫圓的圖片。
center_coordinates: strong> 圓心坐標。坐標表示為兩個值的元組,即(X坐標值,Y坐標值)。
半徑:是
color:是要繪製的圓的邊界線的顏色。對於 BGR,我們傳遞一個元組。 eg: (255, 0, 0) 表示藍色。
thickness: 圓形邊框線的粗細,以px為單位。 -1 px 的厚度會用指定的顏色填充圓形。
返回值:它返回一個圖像。
如何在圖像 OpenCV 上畫一個圓圈?
StackOverflow 問題
我使用 python 和 opencv 從網絡攝像頭獲取圖像,我想知道如何在我的圖像上畫一個圓圈,只是一個帶有透明填充的簡單綠色圓圈
我的代碼:
import cv2 import numpy import sys if __name__ == `__main__`: #get current frame from webcam cam = cv2.VideoCapture(0) img = cam.read() #ho w畫一個圓??? cv2.imshow(`WebCam`, img) cv2.waitKey()
提前致謝。
回答
cv2.circle(img,中心、半徑、顏色、厚度=1, lineType=8, shift=0) ‚Üí None 畫一個圓圈。參數:img (CvArr) , 圓是 繪製中心的圖像 (CvPoint) , 圓中心半徑 ( int) ‚Äì 圓顏色半徑 (CvScalar) ‚Äì 圓顏色粗細 (int) ‚Äì 圓輪廓粗細 if為正,否則表示要畫一個實心圓是 lineType (int ) ‚Äì Type 圓的邊界,見 Line description shift (int) ‚Äì 中的小數位數中心坐標和半徑值
僅對邊框使用“厚度”參數。
示例 #1
def blob(x): """給定一個 Nx3 的 blob 位置和大小矩陣,創建 N 個 img_size x img_size 圖像,每個都有一個由 x 的每一行中的值給出的 blob 一行 x = [x,y,radius].""" y = np.zeros((x.shape[0], img_size, img_size)) for i , 枚舉中的粒子 (x): rr, cc = skimage.draw.circle(particle[0],particle[1], max(particle[2], 1), shape=(img_size, img_size)) y[i, rr, cc] = 1 return y #%% # 名稱(暫時僅供參考!)
Example #2
def update(self, radarData): self .img = np.zeros((self.height, self.width, self.channels), np.uint8) cv2.line(self.img, (10, 0), (self.width/2 - 5, self.高度), (100, 255, 255)) cv2.line(self.img, (self.width - 10, 0), (self.width/2 + 5, self.height), (100, 255, 255) ) for track_number in range(1, 65): if str(track_number)+`_track_range` in radarData: track_range = radarData[str(track_number)+`_track_range`] track_angle = (float(radarData[str(track_number)+`_track_angle `])+90.0)*math.pi/180 x_pos = math.cos(track_angle)*track_range*4 y_pos = math.sin(track_angle)*track_range*4 cv2.circle(self.img, (self.w idth/2 + int(x_pos), self.height - int(y_pos) - 10), 5, (255, 255, 255)) #cv2.putText(self.img, str(track_number), # (self.width /2 + int(x_pos)-2, self.height - int(y_pos) - 10), self.font, 1, (255,255,255), 2) cv2.imshow("雷達", self.img) cv2.waitKey( 2)
Example #3
def ProcessFrame(self, frame): # segment arm region segment = self.SegmentArm(frame) # 複製分割後的圖像進行繪圖draw = cv2.cvtColor(segment, cv2.COLOR_GRAY2RGB) # 繪製一些幫助以正確放置手 cv2.circle(draw,(self.imgWidth/2,self.imgHeight/2),3,[255,102,0],2) cv2.rectangle(draw, (self.imgWidth/3,self.imgHeight/3), (self.imgWidth*2/3, self.imgHeight*2/3), [255,102,0],2) # 找到船體分割區域,並在此基礎上找到 # 凸面缺陷 [contours,defects] = self.FindHullDefects(segment) # 根據輪廓和凸面缺陷檢測手指的數量 # 繪製屬於手指綠色的缺陷,其他紅色[nofingers,draw] = self.DetectN umberFingers(contours, defects, draw) # 在圖像上打印手指的數量 cv2.putText(draw, str(nofingers), (30,30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255)) return draw
示例#4
def mark_hand_center(frame_in,cont): max_d=0 pt=(0,0) x,y,w,h = cv2.boundingRect(cont) for ind_y in xrange(int( y+0.3*h),int(y+0.8*h)): #ind_x in xrange(int(x+0.3*w),int(x+) 的高度約為 0.25 到 0.6 區域(計算速度快,結果良好) 0.6*w)): #about 0.3 to 0.6 region of width (更快的計算結果很好) dist= cv2.pointPolygonTest(cont,(ind_x,ind_y),True) if(dist>max_d): max_d=dist pt=( ind_x,ind_y) if(max_d>radius_thresh*frame_in.shape[1]): thresh_score=True cv2.circle(frame_in,pt,int(max_d),(255,0,0),2) else: thresh_score=False 返回frame_in,pt,max_d,thresh_score #6。查找並顯示手勢
示例#5
def update(self, radarData): self.img = np.zeros((self.height, self.width, self.channels) , np.uint8) cv2.line(self.img, (10, 0), (self.width/2 - 5, self.height), (100, 255, 255)) cv2.line(self.img, ( self.width - 10, 0), (self.width/2 + 5, self.height), (100, 255, 255)) for track_number in range(1, 65): if str(track_number)+`_track_range`在radarData中:track_range =radarData[str(track_number)+`_track_range`] track_angle = (float(radarData[str(track_number)+`_track_angle`])+90.0)*math.pi/180 x_pos = math.cos(track_angle) *track_range*4 y_pos = math.sin(track_angle)*track_range*4 cv2.circle(self.img, (self.width/2 + int(x_pos), self.height - int(y_pos) - 10), 5, (255, 255, 255)) #cv2.putText(self.img, str(track_number), # (self.width/2 + int(x_pos)-2, self.height - int(y_pos) - 10), self .font, 1, (255,255,255), 2) cv2.imshow("雷達", self.img) cv2.waitKey(2)
存檔版本
OpenCV-Python —¬†是一個 Python 綁定解決計算機視覺問題的庫。¬†cv2.circle ()
用於在任何圖像上畫一個圓圈。
語法: cv2.circle(圖像、中心坐標、半徑、顏色、厚度)
參數:
圖像:它是要在其上繪製圓的圖像。
center_coordinates: 它是圓的中心坐標。¬†坐標表示為兩個值的元組 ‚Äã‚Äãie ( X 坐標值, Y 坐標值)。
radius:是圓的半徑。
color: 是要繪製的圓的邊框線的顏色。¬†對於 BGR ,我們通過sa tuple.¬†eg: (255, 0, 0) 表示藍色。
thickness: 它是圓形邊框線的粗細,以 px 為單位。¬ † -1 px 的厚度將按指定填充 rectangle-method/">rectangle 形狀顏色。返回值:它返回一個圖像。
blockquote>該圖像用於以下所有示例:
示例 #1:
# Python程序講解cv2.circle()方法
 
# cv2 import
import
cv2 
 
# path
path
=
r
`C: UsersRajnishDesktoppythonengineeringgeeks.png`
 
¬†# 默認讀取圖片
image
=
cv2.imread (path)
 
# 窗口名稱圖像顯示
window_name
=
`Image `
 
¬†# 中心坐標
center_coordinates
=
(
120
,
50
)
 
# 圓半徑
半徑
=
20
 
¬†# BGR 中的藍色
顏色
=
(
255
<代碼類 = "plain">,0
,
0
)
 
# 線寬 2 px
粗細
=
2
 
# 使用 cv2.circle () 方法
# 畫一個 2 px 藍色邊框的圓
image
=
cv2.circle (圖像、中心坐標、半徑、顏色、厚度)
 
# 顯示圖片
cv2.imshow ( window_name, image) 
輸出:
示例 2:
使用 -用紅色填充 rectangle-method/">矩形 的 1 px 厚度。
# Python程序講解cv2.circle () 方法
 
# cv2 import
import
cv2 
 
# path
path
=
r
`C: UsersRajnishDesktoppythonengineeringgeeks.png`
 p> 
# 以默認模式讀取圖像
image
=
cv2.imread (path)
 
# 顯示圖像的窗口名稱
window_name
=
`Image`
 
¬†# 中心坐標
center_c坐標
=
(
120
,
100
)
¬ †
¬†# 圓半徑
半徑
=
30
 
¬†# BGR 中的紅色
color
=
(
0
,
0
代碼>,
255
)
 
# 線條粗細-1 px
粗細
=
-
1
 
# 使用cv2.circle () 方法
# 畫一個-1px的紅色圓圈
image
=
cv2.circle(圖像、中心坐標、半徑、顏色、厚度)
< code class = "undefined spaces">  
# 顯示圖片
cv2.imshow (window_name, image ) 
 
輸出: