Thursday 16 November 2023

Creating Circle with OpenCV: A Comprehensive Guide

‘cv2.circle’ method is used to draw circles.

Signature

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

 

Following table summarizes the parameters of circle method.

 

Parameter

Description

image

Specifies the image on which you want to draw the circle.

center

It is a tuple, specify the center of the circle.

radius

It is an integer specify the radius of the circle.

color

Specify the color of circle, color is a tuple in BGR (Blue, Green, Red) format.

thickness

Specify the thickness of the circle's outline. If you set it to a negative value, the circle will be filled with given color.

lineType

Specifies the line type while drawing the rectangle.

 

Ex: cv2.LINE_8

shift

The number of fractional bits in the center and radius parameters. It is typically set to 0.

 

circle.py

import cv2 as cv
import numpy as np

# Create a black image
image = np.zeros((600, 600, 3), dtype=np.uint8)
image[:] = [255, 255, 255]

# Define the center and radius of the circle
center1 = (150, 150)
center2 = (250, 250)
center3 = (350, 350)
radius = 50

# Define the color (red in BGR format)
color1 = (0, 255, 0)
color2 = (255, 0, 0)
color3 = (0, 0, 255)

# Draw the circle
cv.circle(image, center1, radius, color1, thickness=2)
cv.circle(image, center2, radius, color2, thickness=-1)
cv.circle(image, center3, radius, color3, thickness=cv.FILLED)

# Display the image
cv.imshow("Circle Example", image)
cv.waitKey(0)
cv.destroyAllWindows()

 

Output


 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment