Overview
OpenCV is a powerful computer vision library that is widely used in areas such as image processing and video analysis. This article will introduce in detail how to use OpenCV to implement the mouse event callback function, drawing circles and rectangles on images through mouse operations. We will demonstrate this functionality through two sample programs.
Environmental preparation
Make sure the OpenCV library is installed. You can install it using the following command:
pip install opencv-python
Example 1: Double-click the left mouse button to draw a circle
Detailed code explanation
import numpy as np import cv2 as cv # Mouse callback functiondef draw_circle(event, x, y, flags, param): if event == cv.EVENT_LBUTTONDBLCLK: (img, (x, y), 100, (255, 0, 0), -1) # Create a black image, a window, and bind the function to the windowimg = ((512, 512, 3), np.uint8) ('image') ('image', draw_circle) while True: ('image', img) if (20) & 0xFF == 27: # Press the ESC key to exit break ()
Code parsing
1. Import the necessary libraries
import numpy as np import cv2 as cv
import numpy as np: Import the NumPy library and alias it to create and manipulate arrays.
import cv2 as cv: Import the OpenCV library and alias it to cv for image processing and window management.
2. Define the mouse callback function
def draw_circle(event, x, y, flags, param): if event == cv.EVENT_LBUTTONDBLCLK: (img, (x, y), 100, (255, 0, 0), -1)
def draw_circle(event, x, y, flags, param): Define a mouse callback function draw_circle, the parameters are the x and y coordinates of the mouse event type, the mouse position, and the additional flags and parameters.
if event == cv.EVENT_LBUTTONDBLCLK: Check whether the mouse event is a double-click left button.
(img, (x, y), 100, (255, 0, 0), -1): Draw a blue circle on the image img, with the center of the circle (x, y), a radius of 100, and a blue color (255, 0, 0). Fill mode -1 means fill circle.
3. Create a black image, a window, and bind the function to the window
img = ((512, 512, 3), np.uint8) ('image') ('image', draw_circle)
img = ((512, 512, 3), np.uint8): Creates a 512x512 black image with an unsigned 8-bit integer.
('image'): Create a window called image.
('image', draw_circle): Bind the mouse callback function draw_circle to the window image.
4. Main loop
while True: ('image', img) if (20) & 0xFF == 27: # Press the ESC key to exit break ()
while True: Enter an infinite loop and continue to display the image.
('image', img): Displays image img in window image.
if (20) & 0xFF == 27: Wait for 20 milliseconds, and exit the loop if the ESC key is pressed (ASCII value is 27).
(): Close all OpenCV windows.
Example 2: Drag the mouse to draw rectangles and circles
Detailed code explanation
import numpy as np import cv2 as cv drawing = False # true if mouse is pressed mode = True # if True, draw rectangle. Press 'm' to toggle to curve ix, iy = -1, -1 # Mouse callback functiondef draw_circle(event, x, y, flags, param): global ix, iy, drawing, mode if event == cv.EVENT_LBUTTONDOWN: drawing = True ix, iy = x, y elif event == cv.EVENT_MOUSEMOVE: if drawing == True: if mode == True: (img, (ix, iy), (x, y), (0, 255, 0), -1) else: (img, (x, y), 5, (0, 0, 255), -1) elif event == cv.EVENT_LBUTTONUP: drawing = False if mode == True: (img, (ix, iy), (x, y), (0, 255, 0), -1) else: (img, (x, y), 5, (0, 0, 255), -1) img = ((512, 512, 3), np.uint8) ('image') ('image', draw_circle) while True: ('image', img) k = (1) & 0xFF if k == ord('m'): mode = not mode elif k == 27: break ()
Code parsing
1. Import the necessary libraries
import numpy as np import cv2 as cv
import numpy as np: Import the NumPy library and alias it to create and manipulate arrays.
import cv2 as cv: Import the OpenCV library and alias it to cv for image processing and window management.
2. Initialize variables
drawing = False # true if mouse is pressed mode = True # if True, draw rectangle. Press 'm' to toggle to curve ix, iy = -1, -1
drawing = False: Boolean variable indicating whether the mouse is pressed.
mode = True: Boolean variable, indicating the current drawing mode, True is drawing rectangle, and False is drawing circle.
ix, iy = -1, -1: Initialize the coordinates when the mouse is pressed.
3. Define the mouse callback function
def draw_circle(event, x, y, flags, param): global ix, iy, drawing, mode if event == cv.EVENT_LBUTTONDOWN: drawing = True ix, iy = x, y elif event == cv.EVENT_MOUSEMOVE: if drawing == True: if mode == True: (img, (ix, iy), (x, y), (0, 255, 0), -1) else: (img, (x, y), 5, (0, 0, 255), -1) elif event == cv.EVENT_LBUTTONUP: drawing = False if mode == True: (img, (ix, iy), (x, y), (0, 255, 0), -1) else: (img, (x, y), 5, (0, 0, 255), -1)
def draw_circle(event, x, y, flags, param): Define a mouse callback function draw_circle, the parameters are the x and y coordinates of the mouse event type, the mouse position, and the additional flags and parameters.
global ix, iy, drawing, mode: declare global variables.
if event == cv.EVENT_LBUTTONDOWN: Check whether the mouse event is left-clicked. If so, set drawing to True and record the coordinates when the mouse is pressed.
elif event == cv.EVENT_MOUSEMOVE: Checks whether the mouse event is moving. If it is and drawing is True, then draw a rectangle or circle according to the current mode.
elif event == cv.EVENT_LBUTTONUP: Check whether the mouse event is left loose. If so, set drawing to False and finish drawing.
4. Create a black image, a window, and bind the function to the window
img = ((512, 512, 3), np.uint8) ('image') ('image', draw_circle)
img = ((512, 512, 3), np.uint8): Creates a 512x512 black image with an unsigned 8-bit integer.
('image'): Create a window called image.
('image', draw_circle): Bind the mouse callback function draw_circle to the window image.
5. Main loop
while True: ('image', img) k = (1) & 0xFF if k == ord('m'): mode = not mode elif k == 27: break ()
while True: Enter an infinite loop and continue to display the image.
('image', img): Displays image img in window image.
k = (1) & 0xFF: Wait for 1 millisecond to get the key value.
if k == ord('m'): If the ‘m’ key is pressed, switch the drawing mode.
elif k == 27: If the ESC key is pressed (ASCII value is 27), the loop exits.
(): Close all OpenCV windows.
Summarize
This article describes how to use OpenCV to implement the mouse event callback function, drawing circles and rectangles on images through mouse operations. By understanding these basic concepts and techniques, we can have more flexibility in applying OpenCV in image processing and interactive applications.
The above is the detailed content of using OpenCV to implement the mouse event callback function and draw graphics. For more information about OpenCV mouse event callback, please pay attention to my other related articles!