在图像opencv上画一个圆圈

时间:2013-05-10 14:38:24

标签: python opencv drawing

我使用python和opencv从网络摄像头获取图像,我想知道如何在我的图像上画一个圆圈,只是一个带有透明填充的简单绿色圆圈

enter image description here

我的代码:

import cv2
import numpy
import sys

if __name__ == '__main__':


    #get current frame from webcam
    cam = cv2.VideoCapture(0)
    img = cam.read()

    #how draw a circle????

    cv2.imshow('WebCam', img)

    cv2.waitKey()

提前致谢。

4 个答案:

答案 0 :(得分:14)

cv2.circle(img, center, radius, color, thickness=1, lineType=8, shift=0) → None
Draws a circle.

Parameters: 
img (CvArr) – Image where the circle is drawn
center (CvPoint) – Center of the circle
radius (int) – Radius of the circle
color (CvScalar) – Circle color
thickness (int) – Thickness of the circle outline if positive, otherwise this indicates that a filled circle is to be drawn
lineType (int) – Type of the circle boundary, see Line description
shift (int) – Number of fractional bits in the center coordinates and radius value

仅对边框使用“thickness”参数。

答案 1 :(得分:13)

只是一个额外的信息:

参数" center" OpenCV的绘图函数cv2.circle()取两个整数的元组。第一个是宽度位置,第二个是高度位置。这种排序与通常的数组索引不同。以下示例演示了此问题。

'compile_scss', 'watch_scss'

python opencv cv2.circle center indexing issue

答案 2 :(得分:2)

尝试

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

有关详细信息,请参阅documentation

答案 3 :(得分:0)

使用 OpenCV 动态绘制圆,

import numpy as np
import cv2
import math
drawing = False # true if mouse is pressed
ix,iy = -1,-1

# Create a function based on a CV2 Event (Left button click)
def draw_circle(event,x,y,flags,param):
    global ix,iy,drawing
    
    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        # we take note of where that mouse was located
        ix,iy = x,y
        
    elif event == cv2.EVENT_MOUSEMOVE:
        drawing == True
        
    elif event == cv2.EVENT_LBUTTONUP:
        radius = int(math.sqrt( ((ix-x)**2)+((iy-y)**2)))
        cv2.circle(img,(ix,iy),radius,(0,0,255), thickness=1)
        drawing = False

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# This names the window so we can reference it
cv2.namedWindow('image')

# Connects the mouse button to our callback function
cv2.setMouseCallback('image',draw_circle)

while(1):
    cv2.imshow('image',img)

    # EXPLANATION FOR THIS LINE OF CODE:
    # https://stackoverflow.com/questions/35372700/whats-0xff-for-in-cv2-waitkey1/39201163
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break
# Once script is done, its usually good practice to call this line
# It closes all windows (just in case you have multiple windows called)
cv2.destroyAllWindows()