import cv2
events=[i for i in dir(cv2) if 'EVENT'in i]
print(events)
['EVENT_FLAG_ALTKEY', 'EVENT_FLAG_CTRLKEY', 'EVENT_FLAG_LBUTTON', 'EVENT_FLAG_MBUTTON', 'EVENT_FLAG_RBUTTON', 'EVENT_FLAG_SHIFTKEY', 'EVENT_LBUTTONDBLCLK', 'EVENT_LBUTTONDOWN', 'EVENT_LBUTTONUP', 'EVENT_MBUTTONDBLCLK', 'EVENT_MBUTTONDOWN', 'EVENT_MBUTTONUP', 'EVENT_MOUSEHWHEEL', 'EVENT_MOUSEMOVE', 'EVENT_MOUSEWHEEL', 'EVENT_RBUTTONDBLCLK', 'EVENT_RBUTTONDOWN', 'EVENT_RBUTTONUP']
import cv2
#鼠标回调函数定义
def draw_circle(event,x,y,flags,param):
if event==cv2.EVENT_LBUTTONDBLCLK:
cv2.circle(img,(x,y),10,(255,0,0),1)
# 创建图像与窗口并将窗口与回调函数绑定
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
img = cv2.imread('test.jpg')
while(1):
cv2.imshow('image', img)
if cv2.waitKey(20)&0xFF==27:
break
cv2.destroyAllWindows()
import cv2
import math
import numpy as np
# 当鼠标按下时变为 True
drawing=False
# 如果 mode 为 true 绘制矩形。按下 'm' 变成绘制曲线。
mode=True
ix,iy=-1,-1
l = 10
# 创建回调函数
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing,mode, l
# 当按下左键是返回起始位置坐标
if event==cv2.EVENT_LBUTTONDOWN:
drawing=True
ix,iy=x,y
elif event==cv2.EVENT_MOUSEMOVE and flags==cv2.EVENT_FLAG_LBUTTON:
lengthX = (x-ix)
lengthY = (y-iy)
l = math.sqrt(lengthX*lengthX + lengthY * lengthY)
# 当鼠标松开停止绘画。
elif event==cv2.EVENT_LBUTTONUP:
if drawing==True:
if mode==True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),1)
else:
l = int(l) ##重点数值类型转换
cv2.circle(img,(ix,iy),l,(0,0,255),1)
drawing==False
img = cv2.imread('test.jpg')
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
while(1):
cv2.imshow('image',img)
k=cv2.waitKey(1)&0xFF
if k==ord('m'):
mode=not mode
elif k&0xFF ==27:
break