1,使用回调函数绘制标准的圆:
鼠标左键按下画圆
import cv2
import numpy as np
events = [i for i in dir(cv2) if "EVENT" in i]
print(events)
img = np.zeros((720,640,3), np.uint8)
def draw_circle(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(img,(x,y),60,(255,0,0),-1)
cv2.namedWindow('image')
cv2.setMouseCallback('image', draw_circle)
while True:
cv2.imshow('image', img)
key = cv2.waitKey(1)
if key == ord('q') or key == 27:
break
cv2.destroyAllWindows()
2、自定义绘制矩形:
import cv2
import numpy as np
events = [i for i in dir(cv2) if "EVENT" in i]
print(events)
xi,yi = -1,-1
drawing = False
img = np.zeros((720,640,3), np.uint8)
def draw_circle(event,x,y,flags,param):
global xi, yi, drawing
if event == cv2.EVENT_LBUTTONDOWN:
drawing =True
xi,yi =x,y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing:
cv2.rectangle(img,(xi,yi),(x,y),(0,255,0),-1)
elif event == cv2.EVENT_LBUTTONUP:
if drawing:
cv2.rectangle(img,(xi,yi),(x,y),(0,255,0),-1)
drawing = False
cv2.namedWindow('image')
cv2.setMouseCallback('image', draw_circle)
while True:
cv2.imshow('image', img)
key = cv2.waitKey(1)
if key == ord('q') or key == 27:
break
cv2.destroyAllWindows()