OpenCV helps us to control and manage different types of mouse events and gives us the flexibility to operate them. There are many types of mouse events. These events can be displayed by running the following code segment :
import cv2 [print(i) for i in dir(cv2) if 'EVENT' in i]
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
Now let us see how to display the coordinates of the points clicked on the image. We will be displaying both the points clicked by right-click as well as left-click.
- Import the
- Import the image using the
- Display the image the image using the
- Call the
cv2.setMouseCallback()fucniton and pass the image window and the user-defined function as parameters.
- In the user-defined function, check for left mouse clicks using the
- Display the coordinates on the Shell.
- Display the coordinates on the created window.
- Do the same for right mouse clicks using the
cv2.EVENT_RBUTTONDOWNattribute. Change the color while displaying the coordinates on the image to distinguish from left clicks.
- Outside the user-defined function, use the
cv2.destroyAllWindows()functions to close the window and terminate the program.
We will be using the colored version of the Lena image.
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.
- PyQtGraph – Menu Clicked Signal of Image View
- PyQtGraph – Export Clicked Signal of Image View
- PyQtGraph – ROI Clicked Signal of Image View
- PyQt5 QCalendarWidget - Clicked signal
- PyQt5 QCommandLinkButton - Clicked Signal
- Python - Displaying real time FPS at which webcam/video file is processed using OpenCV
- MoviePy – Displaying a Frame of Video Clip using inbuilt display method
- Tableau - Displaying data in Worksheet
- PyCairo - Displaying Text
- Django project - Creating a Basic E-commerce Website for Displaying Products
- Python | Calculate geographic coordinates of places using google geocoding API
- Python | Reverse Geocoding to get location on a map using geographic coordinates
- Parallel Coordinates Plot using Plotly in Python
- Python | Convert location coordinates to tuple
- PyQt5 – How to get Label coordinates ?
- Python - Adjacent Coordinates in N dimension
- Python - Group Adjacent Coordinates
- Python - Character coordinates in Matrix
- Python Program to convert complex numbers to Polar coordinates
- Image-to-Image Translation using Pix2Pix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.