OpenCV Python – How to draw curves using Mouse Events?


There are different types of muse events such as left or right button click, mouse move, left button double click etc. OpenCV provides us with different types of mouse events such as cv2.EVENT_LBUTTONDOWN for mouse left button down, cv2.EVENT_LBUTTONDBLCLK for left button double click and others. A mouse event returns the coordinates (x,y) of the mouse event.

To perform an action when an event happens we define a mouse callback function. We use cv2.EVENT_LBUTTONDOWN cv2.EVENT_MOUSEMOVE and cv2.EVENT_LBUTTONUP mouse events to draw curves on the image.

Steps

To draw curves using mouse events, follow the steps given below −

  • Import the required library OpenCV. Make sure you have it installed.

  • Create a black image. We draw the curves on this black image. We can also read an image using cv2.imread() method to draw the curves on it.

  • Define a mouse callback function 'draw_curve' to draw a curve on the image. The mouse callback function is executed when a mouse event happens. A mouse event gives the coordinate of the mouse event. To draw a curve, we draw small circles when the mouse moves.

  • Create a window 'Curve Window' and bind the mouse callback function 'draw_curve' to this window.

  • Display the image window "Curve Window". This window opens the image on which we draw the circle. To close the window press esc button.

Let's have a look at some Python programs to draw curves using mouse events on an image.

Example

In this Python example, we draw curves using three mouse events cv2.EVENT_LBUTTONDOWN, cv2.EVENT_MOUSEMOVE and cv2.EVENT_LBUTTONUP.

# import required libraries import cv2 import numpy as np drawing = False # true if mouse is pressed ix,iy = -1,-1 # define mouse callback function to draw circle def draw_curve(event, x, y, flags, param): global ix, iy, drawing, img if event == cv2.EVENT_LBUTTONDOWN: drawing = True elif event == cv2.EVENT_MOUSEMOVE: if drawing == True: cv2.circle(img, (x, y), 3,(0, 0, 255),-1) elif event == cv2.EVENT_LBUTTONUP: drawing = False cv2.circle(img, (x, y), 3,(0, 0, 255),-1) # Create a black image img = np.zeros((512,700,3), np.uint8) # Create a window and bind the function to window cv2.namedWindow("Curve Window") # Connect the mouse button to our callback function cv2.setMouseCallback("Curve Window", draw_curve) # display the window while True: cv2.imshow("Curve Window", img) if cv2.waitKey(10) == 27: break cv2.destroyAllWindows()

Output

When you execute the above Python code, it will open a window "Curve Window" showing a black image. To draw a curve, first press down the left mouse button (EVENT_LBUTTONDOWN) and move the mouse (EVENT_MOUSEMOVE) then release the button (EVENT_LBUTTONUP). While moving the mouse, at each point, small circles are drawn and when the button is released, it looks like a curve. See the following screenshot.


Updated on: 02-Dec-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements