60
OpenCV provides robust functionality to handle mouse events, enabling interactive applications.
With mouse callbacks, you can respond to actions like clicks, drags, and movements, making it a powerful tool for creating GUI-based applications.
What You’ll Learn
1. Introduction to Mouse Events
OpenCV captures mouse events using the function cv2.setMouseCallback(window_name, callback_function). The callback function processes mouse events like left-click, right-click, and movement.
Mouse events include:
- cv2.EVENT_LBUTTONDOWN: Left mouse button pressed
- cv2.EVENT_RBUTTONDOWN: Right mouse button pressed
- cv2.EVENT_MOUSEMOVE: Mouse moved
- cv2.EVENT_LBUTTONUP: Left mouse button released
- cv2.EVENT_LBUTTONDBLCLK: Left mouse button double-clicked
2. Setting Up a Mouse Callback
Example: Basic Mouse Callback
import cv2 # Mouse callback function def mouse_event(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: print(f"Left button clicked at ({x}, {y})") elif event == cv2.EVENT_RBUTTONDOWN: print(f"Right button clicked at ({x}, {y})") elif event == cv2.EVENT_MOUSEMOVE: print(f"Mouse moved to ({x}, {y})") # Create a black image and window image = cv2.imread("input.jpg") cv2.namedWindow("Mouse Events") # Set the mouse callback cv2.setMouseCallback("Mouse Events", mouse_event) while True: cv2.imshow("Mouse Events", image) if cv2.waitKey(1) & 0xFF == ord('q'): # Quit on 'q' break cv2.destroyAllWindows()
3. Tracking Mouse Movements
Example: Display Real-Time Mouse Coordinates
import cv2 # Mouse callback function def show_coordinates(event, x, y, flags, param): if event == cv2.EVENT_MOUSEMOVE: print(f"Mouse moved to ({x}, {y})") # Create a blank window image = cv2.imread("input.jpg") cv2.namedWindow("Mouse Tracker") # Set the mouse callback cv2.setMouseCallback("Mouse Tracker", show_coordinates) while True: cv2.imshow("Mouse Tracker", image) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows()
4. Handling Click Events
Example: Draw Points on Click
import cv2 # List to store points points = [] # Mouse callback function def draw_point(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: points.append((x, y)) print(f"Point added: ({x}, {y})") # Create a blank image image = cv2.imread("input.jpg") cv2.namedWindow("Draw Points") cv2.setMouseCallback("Draw Points", draw_point) while True: # Draw all points for point in points: cv2.circle(image, point, 5, (0, 255, 0), -1) cv2.imshow("Draw Points", image) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows()
5. Drawing Shapes with Mouse
Example: Draw Rectangles
import cv2 # Variables to store the starting and ending points start_point = None end_point = None drawing = False # Mouse callback function def draw_rectangle(event, x, y, flags, param): global start_point, end_point, drawing if event == cv2.EVENT_LBUTTONDOWN: start_point = (x, y) drawing = True elif event == cv2.EVENT_MOUSEMOVE and drawing: end_point = (x, y) elif event == cv2.EVENT_LBUTTONUP: end_point = (x, y) drawing = False # Create a blank image image = cv2.imread("input.jpg") temp_image = image.copy() cv2.namedWindow("Draw Rectangle") cv2.setMouseCallback("Draw Rectangle", draw_rectangle) while True: # Create a copy of the image to draw on temp_image = image.copy() if start_point and end_point: cv2.rectangle(temp_image, start_point, end_point, (255, 0, 0), 2) cv2.imshow("Draw Rectangle", temp_image) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows()
Example: Draw Circles
import cv2 # Mouse callback function def draw_circle(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: cv2.circle(image, (x, y), 20, (0, 0, 255), -1) # Create a blank image image = cv2.imread("input.jpg") cv2.namedWindow("Draw Circle") cv2.setMouseCallback("Draw Circle", draw_circle) while True: cv2.imshow("Draw Circle", image) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows()
6. Practical Examples
6.1 Color Picker
import cv2 # Mouse callback function def pick_color(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: color = image[y, x] print(f"Color at ({x}, {y}): BGR {color}") # Load an image image = cv2.imread("input.jpg") cv2.namedWindow("Color Picker") cv2.setMouseCallback("Color Picker", pick_color) while True: cv2.imshow("Color Picker", image) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows()
6.2 Freehand Drawing
import cv2 import numpy as np drawing = False # Mouse callback function def draw_freehand(event, x, y, flags, param): global drawing if event == cv2.EVENT_LBUTTONDOWN: drawing = True elif event == cv2.EVENT_MOUSEMOVE: if drawing: cv2.circle(image, (x, y), 3, (0, 255, 0), -1) elif event == cv2.EVENT_LBUTTONUP: drawing = False # Create a blank canvas image = np.zeros((500, 500, 3), dtype="uint8") cv2.namedWindow("Freehand Drawing") cv2.setMouseCallback("Freehand Drawing", draw_freehand) while True: cv2.imshow("Freehand Drawing", image) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows()
7. Summary
Key Functions
- cv2.setMouseCallback(window_name, callback_function): Sets a mouse callback for a specific window.
- Mouse Events:
- cv2.EVENT_LBUTTONDOWN: Left button down.
- cv2.EVENT_RBUTTONDOWN: Right button down.
- cv2.EVENT_MOUSEMOVE: Mouse movement.
- cv2.EVENT_LBUTTONUP: Left button up.
Best Practices
- Use global variables to store state (e.g., start and end points).
- Use real-time updates (imshow) to provide visual feedback.
- Combine mouse events with OpenCV drawing functions for interactivity.