OpenCV Python ŌĆō How to Draw Circles Using Mouse Events?
If you are an OpenCV Python programmer, you may want to know how to draw circles using mouse events. This tutorial will show you how to do that using Python programming language.
Introduction
OpenCV is a powerful computer vision library that allows you to perform image processing and computer vision tasks. It is an open-source library that is widely used in the industry for various computer vision applications. OpenCV has a Python binding, which makes it easy to use with Python programming language. In this tutorial, we will learn how to draw circles using mouse events in OpenCV Python.
Prerequisites
Before we dive into the code, there are a few prerequisites that you need to have in place before you start writing the code. You need to have the following installed on your machine:
- Python 3.7 or higher
- OpenCV Python library
- NumPy library
Drawing Circles with Mouse Events in OpenCV Python
Here we will take a look at how to draw circles using mouse events. Mouse events are used to track the mouse clicks on the frame. Let’s take a look at the code below which allows us to draw circles using mouse events:
import cv2
import numpy as np
# callback function
def draw_circle(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDBLCLK:
cv2.circle(img,(x,y),100,(255,0,0),-1)
# Create a black image, a window and bind the function to window
img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
while(1):
cv2.imshow('image',img)
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()
The code above creates a black image with 512×512 pixels and a window. We then bind the mouse event to the window using cv2.setMouseCallback()
. When the user double-clicks the left mouse button, it draws a blue circle on the image with a radius of 100 pixels. Once the circle is drawn, it will be shown in the window using cv2.imshow()
. The while loop will continue running until the user presses the ESC key. Then, the program will terminate.
Explanation of the Code
Let’s take a closer look at the code and explain how it works.
# callback function
def draw_circle(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDBLCLK:
cv2.circle(img,(x,y),100,(255,0,0),-1)
The draw_circle
function is a callback function that will be called when the mouse button is clicked. The event
parameter is an event that occurred, x
and y
are the coordinates of the mouse where the event occurred, flags
are the mouse button status, and param
is the variable that we can use to pass user-defined information.
# Create a black image, a window and bind the function to window
img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
Here we create a black image with np.zeros()
function, and set window named as image
. Then, we bind the draw_circle()
function to the window using cv2.setMouseCallback('image',draw_circle)
. This means that when the left mouse button is double-clicked, the draw_circle()
function is called.
while(1):
cv2.imshow('image',img)
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()
The while
loop is used to continuously display the image in the window. It will break when the ESC
key is pressed. Finally, we use cv2.destroyAllWindows()
to destroy the window and release any system resources.
Conclusion
We have learned how to draw circles using mouse events in OpenCV Python. We learned how to use the cv2.setMouseCallback()
function to handle mouse events and how to draw a circle on an image. Mouse events are very useful when you need to interact with the users in your computer vision applications.