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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course