OpenCV-Python——第4章:鼠标事件

1 鼠标回调函数

cv2.setMouseCallback(windowName, onMouse, param=None) 

用于将鼠标事件与窗口联系起来

  • windowName:窗口名称
  • onMouse:鼠标响应函数
  • param:响应函数传递的的参数

2 鼠标响应函数

onMouse(event, x, y, flags, param)

用于设置鼠标事件

  • event:鼠标事件,可用参数对应值代替
  • x:鼠标x坐标
  • y:鼠标y坐标
  • flags:鼠标状态,可用参数对应值代替
  • param:param是用户定义的传递到setMouseCallback函数调用的参数

鼠标事件和状态可用以下程序获得

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']

获得的结果按顺序其代表的含义为:

  对应值 含义

flags

 

 

cv2.EVENT_FLAG_ALTKEY

32

按Alt不放

cv2.EVENT_FLAG_CTRLKEY

8

按Ctrl不放

cv2.EVENT_FLAG_LBUTTON

1

左键拖拽

cv2.EVENT_FLAG_MBUTTON

4

中键拖拽

cv2.EVENT_FLAG_RBUTTON

2

右键拖拽

cv2.EVENT_FLAG_SHIFTKEY

16

按住Shift不放

event

 

 

cv2.EVENT_LBUTTONDBLCLK

7

左键双击

cv2.EVENT_LBUTTONDOWN

1

左键按下

cv2.EVENT_LBUTTONUP

4

左键释放

cv2.EVENT_MBUTTONDBLCLK

8

中键双击

cv2.EVENT_MBUTTONDOWN

2

中键按下

cv2.EVENT_MBUTTONUP

5

中键释放

cv2.EVENT_MOUSEHWHEEL

11

横向滚轮滚动

cv2.EVENT_MOUSEMOVE

0

鼠标移动

cv2.EVENT_MOUSEWHEEL

10

滚轮滚动

cv2.EVENT_RBUTTONDBLCLK

8

右键双击

cv2.EVENT_RBUTTONDOWN

2

右键按下

cv2.EVENT_RBUTTONUP

5

右键释放

下面举一个例子

import cv2
import numpy as np

drawing = False  # 当鼠标按下变为True
mode = True  # 按下‘m’变成False,true为绘制矩形,false为画笔
ix, iy = -1, -1  # 初始化鼠标位置


def onmouse(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 and flags == cv2.EVENT_FLAG_LBUTTON:  # 当按下左键拖拽鼠标时
        if drawing is True:
            if mode is True:
                cv2.rectangle(img, (ix, iy), (x, y), (0, 255, 0), 1)  # 当模式为True时画矩形
            else:
                cv2.circle(img, (x, y), 3, (0, 0, 255), -1)  # 当模式为False时画线
    elif event == cv2.EVENT_MOUSEWHEEL:
        if drawing is True:
            if mode is True:
                cv2.circle(img, (x, y), 50, (255, 0, 0), -1)  # 滚轮画实心圆
    elif event == cv2.EVENT_LBUTTONDBLCLK:
        if drawing is True:
            if mode is True:
                cv2.circle(img, (x, y), 50, (255, 255, 0), 3)  # 双击左键画空心圆
    elif event == cv2.EVENT_LBUTTONUP:  # 当鼠标松开停止绘画
        drawing is False
        if mode is True:
            cv2.rectangle(img, (ix, iy), (x, y), (255, 0, 0), 5)  # 松开鼠标后画一个蓝色矩形


img = np.zeros((600, 800, 3), np.uint8)  # 创建空图像
cv2.namedWindow('image')  # 创建空窗口
cv2.setMouseCallback('image', onmouse)  # 将回调函数与窗口绑定
while(1):
    cv2.imshow('image', img)
    k = cv2.waitKey(1) & 0xFF
    if k == ord('m'):  # 按m更换模式
        mode = not mode
    elif k == 27:  # 按ESC退出
        break
cv2.destroyAllWindows()

结果如下: 

OpenCV-Python——第4章:鼠标事件_第1张图片

你可能感兴趣的:(—OpenCV)