- OpenCV Python - Digit Recognition
- OpenCV Python - Feature Matching
- OpenCV Python - Feature Detection
- OpenCV Python - Meanshift/Camshift
- OpenCV Python - Face Detection
- OpenCV Python - Video from Images
- OpenCV Python - Images From Video
- OpenCV Python - Play Videos
- OpenCV Python - Capture Videos
- OpenCV Python - Fourier Transform
- OpenCV Python - Image Blending
- OpenCV Python - Image Addition
- OpenCV Python - Image Pyramids
- OpenCV Python - Template Matching
- OpenCV Python - Image Contours
- OpenCV Python - Transformations
- OpenCV Python - Color Spaces
- OpenCV Python - Histogram
- OpenCV Python - Edge Detection
- OpenCV Python - Image Filtering
- OpenCV Python - Image Threshold
- OpenCV Python - Resize and Rotate
- OpenCV Python - Add Trackbar
- OpenCV Python - Mouse Events
- OpenCV Python - Shapes and Text
- OpenCV Python - Bitwise Operations
- OpenCV Python - Image Properties
- OpenCV Python - Using Matplotlib
- OpenCV Python - Write Image
- OpenCV Python - Reading Image
- OpenCV Python - Environment
- OpenCV Python - Overview
- OpenCV Python - Home
OpenCV Python Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
OpenCV Python - Handpng 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 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 −
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 −
Advertisements