【目标】
- 学会用OpenCV处理鼠标事件
- 学会调用和编写鼠标的回调函数
【代码】
鼠标左键双击画圆
### 鼠标左键双击画圆
import numpy as np
import cv2
# 创建一个黑色的图像和定义一个窗口
img = np.zeros((512, 512, 3), np.uint8)
cv2.namedWindow('image')
# 鼠标左键画蓝色的圆
def draw_circle(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDBLCLK:
cv2.circle(img, (x, y), 100, (255, 0, 0), -1)
# 设置鼠标的回调函数
cv2.setMouseCallback('image', draw_circle)
# 按 ESC 键退出
while (1):
cv2.imshow('image', img)
if cv2.waitKey(20) & 0xFF == 27:
break
# 销毁窗口
cv2.destroyAllWindows()
画矩形和画圆进行模式切换, 按 m 进行模式切换
# 画矩形和画圆,可以通过 m 切换模式
import numpy as np
import cv2
img = np.zeros((512, 512, 3), np.uint8)
cv2.namedWindow('image')
# 如果鼠标按下为 True
drawing = False
# 模式选择,如果为 True画矩形,否则画圆
mode = True
ix,iy = -1,-1
rectLineThinkness = 5
# 鼠标的回调函数
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing,mode
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
if mode == True:
cv2.rectangle(img, (ix, iy), (x, y),
(0, 255, 0), rectLineThinkness)
else:
cv2.circle(img,(x,y),5,(0,0,255),-1)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
if mode == True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0), rectLineThinkness)
else:
cv2.circle(img,(x,y),5,(0,0,255),-1)
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 == 27:
break
cv2.destroyAllWindows()
【接口】
void cv::setMouseCallback(const String & winname,
MouseCallback onMouse,
void * userdata = 0
);
cv2.setMouseCallback(winname, onMouse [, userdata])
参数
- winname: 窗口名称
- onMouse: 鼠标的回调函数
- userdata: 可选的参数传入到函数里面
鼠标事件标志
标志名称 | 描述 |
---|---|
EVENT_FLAG_LBUTTON Python: cv2.EVENT_FLAG_LBUTTON | 鼠标左键按下 |
EVENT_FLAG_RBUTTON Python: cv2.EVENT_FLAG_RBUTTON | 鼠标右键按下 |
EVENT_FLAG_MBUTTON Python: cv2.EVENT_FLAG_MBUTTON | 鼠标中键按下 |
EVENT_FLAG_CTRLKEY Python: cv2.EVENT_FLAG_CTRLKEY | CTRL 键按下 |
EVENT_FLAG_SHIFTKEY Python: cv2.EVENT_FLAG_SHIFTKEY | SHIFT 键按下 |
EVENT_FLAG_ALTKEY Python: cv2.EVENT_FLAG_ALTKEY | ALT 键按下 |
鼠标事件类型
类型名称 | 描述 |
---|---|
EVENT_MOUSEMOVE Python: cv2.EVENT_MOUSEMOVE | 鼠标指针移动 |
EVENT_LBUTTONDOWN Python: cv2.EVENT_LBUTTONDOWN | 鼠标左键按下 |
EVENT_RBUTTONDOWN Python: cv2.EVENT_RBUTTONDOWN | 鼠标右键按下 |
EVENT_MBUTTONDOWN Python: cv2.EVENT_MBUTTONDOWN | 鼠标中键按下 |
EVENT_LBUTTONUP Python: cv2.EVENT_LBUTTONUP | 鼠标左键弹起 |
EVENT_RBUTTONUP Python: cv2.EVENT_RBUTTONUP | 鼠标右键弹起 |
EVENT_MBUTTONUP Python: cv2.EVENT_MBUTTONUP | 鼠标中键弹起 |
EVENT_LBUTTONDBLCLK Python: cv2.EVENT_LBUTTONDBLCLK | 鼠标左键双击 |
EVENT_RBUTTONDBLCLK Python: cv2.EVENT_RBUTTONDBLCLK | 鼠标右键双击 |
EVENT_MBUTTONDBLCLK Python: cv2.EVENT_MBUTTONDBLCLK | 鼠标中键双击 |
EVENT_MOUSEWHEEL Python: cv2.EVENT_MOUSEWHEEL | 鼠标滚轮滚动,正负值代表前后滚动 |
EVENT_MOUSEHWHEEL Python: cv2.EVENT_MOUSEHWHEEL | 正负值代表左右滚动 |
【小栗子】
以上程序画出来的图像矩形框重复覆盖了,如果只想画一个单独的矩形,那么该如何编写呢?
之前的程序是因为在一个画布上重复画矩形,所以重叠了,要解决此问题,就必须不断的刷新画布,在鼠标移动的时候不断刷新,鼠标左键弹起后,不再作画。
SHOW ME THE CODE
# 画矩形,不重复
import numpy as np
import cv2
# 定义画布
imgsrc = np.zeros((512, 512, 3), np.uint8)
cv2.namedWindow('image')
# 开始点和结束点
startPT = (-1, -1)
endPT = (-1, -1)
rectLineThinkness = 5
drawing = False
# 鼠标回调函数,画矩形
def draw_rect(event, x, y, flags, param):
global startPT, endPT, drawing
mouseMat = param.copy()
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
startPT = (x, y)
elif event == cv2.EVENT_MOUSEMOVE:
if drawing:
endPT = (x, y)
cv2.rectangle(mouseMat, startPT, endPT,
(0, 255, 0), rectLineThinkness)
elif event == cv2.EVENT_LBUTTONUP:
endPT = (x, y)
drawing = False
cv2.rectangle(mouseMat, startPT, endPT, (0, 255, 0), rectLineThinkness)
cv2.imshow('image', mouseMat)
cv2.setMouseCallback('image', draw_rect, imgsrc)
cv2.imshow('image', imgsrc)
cv2.waitKey(0)
cv2.destroyAllWindows()
【参考】
- https://docs.opencv.org/4.5.5/db/d5b/tutorial_py_mouse_handling.html