Opencv Python 简明教程
OpenCV Python - Handling Mouse Events
OpenCV 能够用一个回调函数来注册各种鼠标相关事件。这用于根据鼠标事件的类型来启动一个特定用户定义的动作。
OpenCV is capable of registering various mouse related events with a callback function. This is done to initiate a certain user defined action depending on the type of mouse event.
Sr.No |
Mouse event & Description |
1 |
cv.EVENT_MOUSEMOVE When the mouse pointer has moved over the window. |
2 |
cv.EVENT_LBUTTONDOWN Indicates that the left mouse button is pressed. |
3 |
cv.EVENT_RBUTTONDOWN Event of that the right mouse button is pressed. |
4 |
cv.EVENT_MBUTTONDOWN Indicates that the middle mouse button is pressed. |
5 |
cv.EVENT_LBUTTONUP When the left mouse button is released. |
6 |
cv.EVENT_RBUTTONUP When the right mouse button is released. |
7 |
cv.EVENT_MBUTTONUP Indicates that the middle mouse button is released. |
8 |
cv.EVENT_LBUTTONDBLCLK This event occurs when the left mouse button is double clicked. |
9 |
cv.EVENT_RBUTTONDBLCLK Indicates that the right mouse button is double clicked. |
10 |
cv.EVENT_MBUTTONDBLCLK Indicates that the middle mouse button is double clicked. |
11 |
cv.EVENT_MOUSEWHEEL Positive for forward and negative for backward scrolling. |
要根据鼠标事件来调用函数,就必须使用 setMouseCallback() 函数来注册。对应的命令如下所示:
To fire a function on a mouse event, it has to be registered with the help of setMouseCallback() function. The command for the same is as follows −
cv2.setMouseCallback(window, callbak_function)
此函数将事件的类型和位置传递给回调函数以进行进一步处理。
This function passes the type and location of the event to the callback function for further processing.
Example 1
以下代码会在窗口图像背景上发生左键双击事件时绘制一个圆:
Following code draws a circle whenever left button double click event occurs on the window showing an image as background −
import numpy as np
import cv2 as cv
# mouse callback function
def drawfunction(event,x,y,flags,param):
if event == cv.EVENT_LBUTTONDBLCLK:
cv.circle(img,(x,y),20,(255,255,255),-1)
img = cv.imread('lena.jpg')
cv.namedWindow('image')
cv.setMouseCallback('image',drawfunction)
while(1):
cv.imshow('image',img)
key=cv.waitKey(1)
if key == 27:
break
cv.destroyAllWindows()
Output
运行以上程序然后在随机位置双击。将会看到类似的输出:
Run the above program and double click at random locations. The similar output will appear −
Example 2
根据用户输入(1、2 或 3)交互式绘制矩形、线或圆:
Following program interactively draws either rectangle, line or circle depending on user input (1,2 or 3) −
import numpy as np
import cv2 as cv
# mouse callback function
drawing=True
shape='r'
def draw_circle(event,x,y,flags,param):
global x1,x2
if event == cv.EVENT_LBUTTONDOWN:
drawing = True
x1,x2 = x,y
elif event == cv.EVENT_LBUTTONUP:
drawing = False
if shape == 'r':
cv.rectangle(img,(x1,x2),(x,y),(0,255,0),-1)
if shape == 'l':
cv.line(img,(x1,x2),(x,y),(255,255,255),3)
if shape=='c':
cv.circle(img,(x,y), 10, (255,255,0), -1)
img = cv.imread('lena.jpg')
cv.namedWindow('image')
cv.setMouseCallback('image',draw_circle)
while(1):
cv.imshow('image',img)
key=cv.waitKey(1)
if key==ord('1'):
shape='r'
if key==ord('2'):
shape='l'
if key==ord('3'):
shape='c'
#print (shape)
if key == 27:
break
cv.destroyAllWindows()
如果按下“1”,将在鼠标左键按下和抬起的坐标之间绘制一个矩形。
On the window surface, a rectangle is drawn between the coordinates of the mouse left button down and up if ‘1’ is pressed.
如果用户选择 2,将使用坐标作为端点绘制一条线。
If user choice is 2, a line is drawn using coordinates as endpoints.
如果选择 3 绘制圆,则会在鼠标抬起事件的坐标处绘制一个圆。
On choosing 3 for the circle, it is drawn at the coordinates of the mouse up event.
在成功执行以上程序后,输出将显示如下图像:
Following image will be the output after the successful execution of the above mentioned program −