OpenCV Python – Mouse Events


OpenCV Python – Handling Mouse Events



”;


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.

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 −


Mouse Events

Example 2

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()

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 line 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

”;

Leave a Reply

Your email address will not be published. Required fields are marked *