23

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

在此处输入图像描述

我的代码:

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

4 回答 4

37
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

仅对边框使用“厚度”参数。

于 2013-05-10T15:14:14.097 回答
28

只是一个附加信息:

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

import numpy as np
import cv2

height, width = 150, 200
img = np.zeros((height, width, 3), np.uint8)
img[:, :] = [255, 255, 255]

# Pixel position to draw at
row, col = 20, 100

# Draw a square with position 20, 100 as the top left corner
for i in range(row, 30):
    for j in range(col, 110):
        img[i, j] = [0, 0, 255]

# Will the following draw a circle at (20, 100)?
# Ans: No. It will draw at row index 100 and column index 20.
cv2.circle(img,(col, row), 5, (0,255,0), -1)

cv2.imwrite("square_circle_opencv.jpg", img)

python opencv cv2.circle 中心索引问题

于 2017-05-21T16:25:14.017 回答
3

尝试

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

有关更多详细信息,请参阅文档

于 2013-05-10T16:35:38.173 回答
1

要使用 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()
于 2021-05-26T15:34:16.467 回答