OpenCV_Python API 官方文档学习_ cv2 控制鼠标动作

官方官方文档

Goal

  • Learn to handle mouse events in OpenCV
  • You will learn these functions : cv2.setMouseCallback()

实现目标:

1. 学习在OpenCV中如何使用鼠标动作

2. 学习函数cv2.setMouseCallback()


简单案例:

首先,需要创建一个鼠标回调函数,在发生鼠标相关事件时执行该函数。鼠标事件可以是任何与鼠标相关的东西,如鼠标左键按下,鼠标左键弹起,左键双击等等.它给出了每个鼠标事件的坐标(x,y)。有了这个事件和坐标定位,我们可以做任何我们想做的事。有关鼠标的所有相关回调函数有:

'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
import numpy as np

# mouse callback function
def draw_circle(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDBLCLK:
        cv2.circle(img,(x,y),100,(255,0,0),-1)

# Create a black image, a window and bind the function to window
img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)

while(1):
    cv2.imshow('image',img)
    if cv2.waitKey(20) & 0xFF == 27:
        break
cv2.destroyAllWindows()


一个更具应用性的案例:

一个更实用的案例。在这个案例中,我们通过拖动鼠标来绘制矩形或圆圈(取决于我们选择的模式),就像在画图应用程序中所做的那样。因此,我们的鼠标回调函数有两个部分,一个用来绘制矩形,另一个用来绘制圆圈。这个具体的例子将有助于创建和理解一些交互式应用程序,如对象跟踪、图像分割等。

代码:

import cv2
import numpy as np

drawing = False # true if mouse is pressed
mode = True # if True, draw rectangle. Press 'm' to toggle to curve
ix,iy = -1,-1

# mouse callback function
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),-1)
            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),-1)
        else:
            cv2.circle(img,(x,y),5,(0,0,255),-1)
OpenCV_Python API 官方文档学习_ cv2 控制鼠标动作_第1张图片

接下来,将此鼠标回调函数绑定到OpenCV窗口。在主循环中,我们应该为键“m”设置一个键盘绑定,以在矩形和圆形之间切换。

import cv2
import numpy as np

drawing = False # true if mouse is pressed
mode = True # if True, draw rectangle. Press 'm' to toggle to curve
ix,iy = -1,-1

# mouse callback function
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),-1)
            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),-1)
        else:
            cv2.circle(img,(x,y),5,(0,0,255),-1)

# img = np.zeros((512,512,3), np.uint8)
# cv2.namedWindow('image')
# cv2.setMouseCallback('image',draw_circle)
# while(1):
#     cv2.imshow('image',img)
#     if cv2.waitKey(20) & 0xFF == 27:
#         break
# cv2.destroyAllWindows()
img = np.zeros((512,512,3), np.uint8)
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 == 27:
        break

cv2.destroyAllWindows()
OpenCV_Python API 官方文档学习_ cv2 控制鼠标动作_第2张图片

你可能感兴趣的:(OpenCV)