OpenCV Tutorial

Image Processing

Feature Detection and Description

Drawing Functions

Video Processing

Applications and Projects

Draw a circle in OpenCV

Drawing a circle in OpenCV is straightforward using the circle() function. In this tutorial, I'll guide you through the steps to draw a circle on an image using OpenCV.

Steps to Draw a Circle using OpenCV:

1. Setup:

First, ensure you have OpenCV installed:

pip install opencv-python

Next, import necessary libraries:

import cv2
import numpy as np

2. Load or Create an Image:

For this tutorial, let's create a blank image. You can also load an existing image if you prefer:

# Create a blank image (white background)
image = np.ones((500, 500, 3), np.uint8) * 255

Or load an existing image:

# image = cv2.imread('path_to_image.jpg')

3. Draw a Circle:

The cv2.circle() function allows you to draw a circle:

cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]])
  • img: The image on which the circle is drawn.
  • center: Center of the circle.
  • radius: Radius of the circle.
  • color: Circle color.
  • thickness (optional): Thickness of the circle outline. If this is set to a negative value (e.g., -1), it will fill the circle.
  • lineType (optional): Type of the circle boundary.
  • shift (optional): Number of fractional bits in the coordinates of the center and the radius value.

Here's an example:

center = (250, 250) # Center of the circle
radius = 100
color = (0, 255, 0)  # Green color in BGR
thickness = 2  # Set this to -1 if you want to fill the circle

cv2.circle(image, center, radius, color, thickness)

4. Display the Image:

cv2.imshow('Circle', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

When you run the code, you'll see a window displaying an image with a green circle. You can adjust the center, radius, color, and thickness as desired.

That concludes our tutorial! Using the circle() function, you can quickly draw circles on images in OpenCV, making it useful for various tasks like highlighting regions of interest or adding annotations.

  1. Drawing Circles in OpenCV:

    • Description: Introduction to basic circle drawing in OpenCV.
    • Code:
      import cv2
      import numpy as np
      
      # Create a black image
      img = np.zeros((300, 400, 3), dtype=np.uint8)
      
      # Define circle center and radius
      center = (200, 150)
      radius = 50
      
      # Draw circle on the image
      cv2.circle(img, center, radius, (0, 255, 0), 2)
      
      # Display the image
      cv2.imshow('Circle Drawing', img)
      cv2.waitKey(0)
      cv2.destroyAllWindows()