OpenCV Python ŌĆō How to draw curves using Mouse Events?
OpenCV is a popular computer vision library that includes a wide variety of image processing tasks. One important feature of OpenCV is the ability to use mouse events to interact with images. In this article, we’ll learn how to use mouse events to draw curves in OpenCV Python.
Required Libraries
Before we get started, we need to make sure we have the necessary libraries installed. In order to run OpenCV in Python, we need the following libraries:
pip install opencv-python
pip install opencv-python-headless
We also need the following libraries for handling mouse events:
pip install Pillow
pip install numpy
Creating a blank canvas
The first step in drawing curves is to create a blank canvas. To do this, we import the necessary libraries and create a blank image:
import cv2
import numpy as np
# Create a blank canvas
canvas = np.ones((500, 500, 3), dtype="uint8") * 255
We create a canvas with a height and width of 500 pixels each and a depth of 3 (for RGB values). We set the initial pixel values to white (255).
Next, we define a function that will be called every time the mouse is clicked or dragged:
def draw_curve(event, x, y, flags, params):
# Check if the left mouse button was pressed
if event == cv2.EVENT_LBUTTONDOWN:
# Store the starting points of the curve
params.start_points = [(x, y)]
# Check if the mouse is being dragged
if event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON:
# Draw the curve as the mouse is being dragged
cv2.line(params.canvas, params.start_points[-1], (x, y), (0, 0, 0), thickness=3)
# Update the starting points of the curve
params.start_points.append((x, y))
# Check if the left mouse button was released
if event == cv2.EVENT_LBUTTONUP:
# Draw the final curve
cv2.line(params.canvas, params.start_points[-1], (x, y), (0, 0, 0), thickness=3)
This function takes five arguments:
event
: the type of mouse event (e.g. click, drag, release)x
andy
: the coordinates of the mouseflags
: any additional flags that are sent with the mouse eventparams
: additional parameters we want to pass to the function
The function first checks if the left mouse button was pressed. If it was, we store the starting point of the curve.
If the mouse is being dragged (i.e. the left mouse button is down and moving), we draw a line on the canvas as the mouse is being dragged. We also update the starting point of the curve.
If the left mouse button is released, we draw the final curve on the canvas.
Running the program
Now that we have our canvas and mouse event function, we can create a window and set the mouse event callback function:
# Store the starting points of the curve
params = cv2.SimpleNamespace(start_points=(), canvas=canvas)
# Create a window for the canvas
cv2.namedWindow("Canvas")
# Set the mouse callback function
cv2.setMouseCallback("Canvas", draw_curve, param=params)
# Keep the window open
while True:
cv2.imshow("Canvas", params.canvas)
if cv2.waitKey(1) == ord("q"):
break
cv2.destroyAllWindows()
We create a namespace object to store the starting points of the curve and the canvas. We then create a window and set the mouse event callback function to our draw_curve
function.
Finally, we display the canvas and wait for a keyboard event. When the “q” key is pressed, we break out of the loop and close the window.
Conclusion
In this article, we learned how to use mouse events to draw curves in OpenCV Python. By creating a blank canvas and setting a mouse event callback function, we were able to interact with the canvas and draw curves using the left mouse button. This technique can be applied to a wide variety of image processing tasks, including object detection, segmentation, and tracking.