OpenCV Python – How to draw circles using Mouse Events?

OpenCV provides various mouse events to interact with images, such as cv2.EVENT_LBUTTONDOWN for left button click, cv2.EVENT_RBUTTONDOWN for right button click, and cv2.EVENT_LBUTTONDBLCLK for double-click. These events return mouse coordinates (x,y) which we can use to draw shapes like circles through callback functions.

Steps to Draw Circles with Mouse Events

To draw circles using mouse events, follow these steps ?

  • Import the required library OpenCV and NumPy.

  • Create a black image or read an existing image using cv2.imread().

  • Define a mouse callback function that draws circles when mouse events occur.

  • Create a window and bind the callback function to it using cv2.setMouseCallback().

  • Display the image window and handle user interactions.

Drawing Circles on Mouse Click

This example draws circles with fixed radius when you click the left mouse button ?

import cv2
import numpy as np

# Create a black image
img = np.zeros((512, 700, 3), np.uint8)

# Mouse callback function to draw circle
def draw_circle(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        cv2.circle(img, (x, y), 100, (0, 255, 255), 2)

# Create window and bind callback
cv2.namedWindow("Circle Window")
cv2.setMouseCallback("Circle Window", draw_circle)

# Display image until ESC is pressed
while True:
    cv2.imshow("Circle Window", img)
    if cv2.waitKey(20) & 0xFF == 27:  # ESC key
        break

cv2.destroyAllWindows()

This code creates a window with a black image. Each left mouse click draws a yellow circle with radius 100 pixels at the clicked location.

Drawing Circles by Dragging

This example creates circles with variable radius based on mouse drag distance ?

import numpy as np
import cv2
import math

drawing = False
ix, iy = -1, -1

# Mouse callback function for drag-to-draw circles
def draw_circle(event, x, y, flags, param):
    global ix, iy, drawing
    
    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        ix, iy = x, y
    elif event == cv2.EVENT_LBUTTONUP:
        # Calculate radius based on drag distance
        radius = int(math.sqrt(((ix - x) ** 2) + ((iy - y) ** 2)))
        cv2.circle(img, (ix, iy), radius, (255, 0, 255), thickness=2)
        drawing = False

# Create black image
img = np.zeros((512, 700, 3), np.uint8)

# Create window and bind callback
cv2.namedWindow('Drag Circle Window')
cv2.setMouseCallback('Drag Circle Window', draw_circle)

# Display image
while True:
    cv2.imshow('Drag Circle Window', img)
    if cv2.waitKey(1) & 0xFF == 27:  # ESC key
        break

cv2.destroyAllWindows()

This creates circles where the radius depends on how far you drag the mouse from the starting point.

Drawing Filled Circles on Double-Click

This example draws filled circles on an image using double-click events ?

import numpy as np
import cv2

# Mouse callback function for filled circles
def draw_circle(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDBLCLK:
        cv2.circle(img, (x, y), 100, (255, 0, 0), -1)  # -1 for filled

# Read input image (replace with your image path)
img = cv2.imread('your_image.jpg')

# Create window and bind callback
cv2.namedWindow('Filled Circle Window')
cv2.setMouseCallback('Filled Circle Window', draw_circle)

# Display image
while True:
    cv2.imshow('Filled Circle Window', img)
    if cv2.waitKey(1) & 0xFF == 27:  # ESC key
        break

cv2.destroyAllWindows()

Mouse Event Types

Event Description Use Case
EVENT_LBUTTONDOWN Left button pressed Single click actions
EVENT_LBUTTONUP Left button released End of drag operations
EVENT_LBUTTONDBLCLK Left button double-click Special actions
EVENT_MOUSEMOVE Mouse movement Real-time drawing

Conclusion

OpenCV mouse events provide an interactive way to draw circles on images. Use single clicks for fixed-size circles, drag operations for variable-size circles, and double-clicks for special effects like filled shapes.

Updated on: 2026-03-26T22:52:46+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements