Reputation: 63
I'm trying to draw a circle on my webcam's image everytime I click. However, I would like circles to stay on the image, and add new circles with each new click. Is there a way to modify my current code to do this?
import cv2
cap = cv2.VideoCapture(0)
center = (0,0)
def mouseCallback(event,x,y,flags,param):
global center
if event == cv2.EVENT_LBUTTONDOWN:
center = (x,y)
def draw_circle(frame, center):
cv2.circle(frame, center = center, radius = 50,color = (0, 0, 255),thickness=5)
cv2.namedWindow('frame')
cv2.setMouseCallback('frame',mouseCallback)
while True:
ret, img = cap.read()
draw_circle(img,center)
cv2.imshow('frame',img)
if cv2.waitKey(20) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
I do not find any information about how to make a shape stay on the video.
Any help is appreciated :)
Upvotes: 1
Views: 330
Reputation:
You can use a list to store the coordinates of the circles. Fast code sketch:
import cv2
cap = cv2.VideoCapture(0)
circles = []
def mouseCallback(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
circles.append((x,y))
def draw_circle(frame, circles):
for center in circles:
cv2.circle(frame, center = center, radius = 50,color = (0, 0, 255),thickness=5)
cv2.namedWindow('frame')
cv2.setMouseCallback('frame',mouseCallback)
while True:
ret, img = cap.read()
draw_circle(img, circles)
cv2.imshow('frame',img)
if cv2.waitKey(20) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
Upvotes: 2