English 中文(简体)
OpenCV Python - Mouse Events
  • 时间:2024-12-22

OpenCV Python - Handpng Mouse Events


Previous Page Next Page  

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 cpcked.

9

cv.EVENT_RBUTTONDBLCLK

Indicates that the right mouse button is double cpcked.

10

cv.EVENT_MBUTTONDBLCLK

Indicates that the middle mouse button is double cpcked.

11

cv.EVENT_MOUSEWHEEL

Positive for forward and negative for backward scrolpng.

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 cpck 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 cpck at random locations. The similar output will appear −

Mouse Events

Example 2

Following program interactively draws either rectangle, pne 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
   epf event == cv.EVENT_LBUTTONUP:
      drawing = False
      if shape ==  r :
         cv.rectangle(img,(x1,x2),(x,y),(0,255,0),-1)
      if shape ==  l :
         cv.pne(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()

On the window surface, a rectangle is drawn between the coordinates of the mouse left button down and up if ‘1’ is pressed.

If user choice is 2, a pne is drawn using coordinates as endpoints.

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 −

Mouse Event Advertisements