Handle mouse events in Python – OpenCV

OpenCV is one of the most popular computer vision libraries. If you want to start your journey in the field of computer vision, then a thorough understanding of the concepts of OpenCV is of paramount importance.

Note: For more information, refer to Introduction to OpenCV

Handling Mouse Evenets

OpenCV sometimes helps to control and manage different types of mouse events and gives us the flexibility to manage them. There can be different types of mouse events such as left button click, right button click, double_click, etc. To manage these events we need to design callback functions for each type of mouse click event while the window or frame is opened by OpenCV.The callback function will be helpful to implement what type of functionality you want with a particular mouse click event.

Below is the code to show how we can perform operations with a right click and left click events.

Code:



filter_none

edit
close

play_arrow

link
brightness_4
code

import cv2
  
  
# read image
img = cv2.imread('image.jpg')
  
# show image
cv2.imshow('image', img)
   
#define the events for the
# mouse_click.
def mouse_click(event, x, y, 
                flags, param):
      
    # to check if left mouse 
    # button was clicked
    if event == cv2.EVENT_LBUTTONDOWN:
          
        # font for left click event
        font = cv2.FONT_HERSHEY_TRIPLEX
        LB = 'Left Button'
          
        # display that left button 
        # was clicked.
        cv2.putText(img, LB, (x, y), 
                    font, 1
                    (255, 255, 0), 
                    2
        cv2.imshow('image', img)
          
          
    # to check if right mouse 
    # button was clicked
    if event == cv2.EVENT_RBUTTONDOWN:
           
        # font for right click event
        font = cv2.FONT_HERSHEY_SCRIPT_SIMPLEX
        RB = 'Right Button'
          
        # display that right button 
        # was clicked.
        cv2.putText(img, RB, (x, y),
                    font, 1
                    (0, 255, 255),
                    2)
        cv2.imshow('image', img)
  
cv2.setMouseCallback('image', mouse_click)
   
cv2.waitKey(0)
  
# close all the opened windows.
cv2.destroyAllWindows()

chevron_right


Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.