- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- OpenCV Python – How to draw circles using Mouse Events?
- OpenCV Python – How to draw a rectangle using Mouse Events?
- How to work with Mouse Events using OpenCV in C++?
- How to draw filled ellipses in OpenCV using Python?
- How to draw polylines on an image in OpenCV using Python?
- How to make a polygon object react to the mouse events using FabricJS?
- How to draw polylines in OpenCV using Java?
- Find and Draw Contours using OpenCV in Python
- OpenCV Python – How to detect and draw keypoints in an image using SIFT?
- How to draw a line in OpenCV using Java?
- How to draw an ellipse in OpenCV using Java?
- How to draw a rectangle in OpenCV using Java?
- How to draw a circle in OpenCV using Java?
- How to draw a polygon in OpenCV using Java?
- How to draw Image Contours using Java OpenCV library?
