Site icon DataFlair

Drawing Circle, Line, Rectangle using OpenCV

drawing circle line rectrangle using opencv

Machine Learning courses with 110+ Real-time projects Start Now!!

Step into the fascinating world of computer vision and unleash your creativity with OpenCV! In this visual journey, we’ll explore the magical realm of drawing circles, lines, and rectangles on digital canvases. Whether you’re a seasoned artist or a curious tech enthusiast, prepare to be captivated as we bring static images to life with the stroke of Python code.

Let’s embark on an exciting adventure of shapes, colours, and boundless possibilities – all powered by the remarkable capabilities of OpenCV.

Creating lines with OpenCV’s cv2.line():

OpenCV’s cv2.line() function is a powerful tool that opens up a world of visual expression, allowing you to draw lines on images and create compelling graphics effortlessly. Whether you’re enhancing images, building computer vision applications, or exploring the realm of digital artistry, cv2.line() is an essential function in your creative toolkit.

Syntax :

cv2.line(img, pt1, pt2, color, thickness)

Parameters:

Code with Explanation:

Let’s walk through a simple example to illustrate how cv2.line() works:

import cv2
import numpy as np

# Create a blank canvas (black image) of size 800x600
canvas = np.zeros((600, 800, 3), dtype=np.uint8)
# Define the start and end points of the line
start_point = (100, 200)
end_point = (600, 400)

# Define the color of the line in BGR format (Green color)
color = (0, 255, 0)
# Set the thickness of the line
thickness = 2

# Draw the line on the canvas
cv2.line(canvas, start_point, end_point, color, thickness)
# Display the canvas with the drawn line
cv2.imshow('Canvas with Line', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()

Creating Rectangle in OpenCV with cv2.rectangle() :

OpenCV’s cv2.rectangle() function empowers you to breathe life into your images by effortlessly adding rectangles to your canvas. The ability to draw rectangles is a fundamental yet crucial skill for various applications, including object detection, image annotation, and creative artwork.

In this section, we’ll delve into the syntax, parameters, and a practical code example to master the art of geometric design with cv2.rectangle().

Syntax:

cv2.rectangle(img, pt1, pt2, color, thickness)

Parameters:

Code with Explanation :

Let’s explore a practical example to understand how cv2.rectangle() works :

import cv2
import numpy as np

# Create a blank canvas (black image) of size 800x600
canvas = np.zeros((600, 800, 3), dtype=np.uint8)

# Define the top-left and bottom-right points of the rectangle
top_left = (100, 200)
bottom_right = (600, 400)

# Define the color of the rectangle in BGR format (Blue color)
color = (255, 0, 0)

# Set the thickness of the rectangle border
thickness = 2

# Draw the rectangle on the canvas
cv2.rectangle(canvas, top_left, bottom_right, color, thickness)

# Display the canvas with the drawn rectangle
cv2.imshow('Canvas with Rectangle', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()

Creating Circle in OpenCV with cv2.circle():

OpenCV’s cv2.circle() function introduces you to the enchanting world of circles, where curves speak volumes and elegance knows no bounds. Drawing circles on images can breathe life into your visual creations, be it for object detection, image annotations, or artistic expression.

In this section, we’ll unravel the syntax, parameters, and a practical code example to help you embrace the beauty of curves with cv2.circle().

Syntax:

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

Parameters:

Code with Explanation :

Let’s immerse ourselves in a practical example to understand how cv2.circle() works:

import cv2
import numpy as np

# Create a blank canvas (black image) of size 800x600
canvas = np.zeros((600, 800, 3), dtype=np.uint8)

# Define the center coordinates of the circle
center = (400, 300)

# Define the radius of the circle in pixels
radius = 100

# Define the color of the circle in BGR format (Green color)
color = (0, 255, 0)

# Set the thickness of the circle's outline
thickness = 2

# Draw the circle on the canvas
cv2.circle(canvas, center, radius, color, thickness)

# Display the canvas with the drawn circle
cv2.imshow('Canvas with Circle', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()

Creating Ellipses in OpenCV: Crafting Curves of Elegance

In the realm of computer vision, shapes are the building blocks of understanding the visual world. Among these, ellipses stand as elegant symbols of symmetry and grace. OpenCV, with its versatile toolkit, provides us with the means to not only detect but also to create these graceful curves.

In this exploration, we delve into the art of Creating Ellipses in OpenCV, where we’ll unravel the syntax, parameters, and code that enable us to craft curves that transcend pixels and echo the language of beauty.

cv2.ellipse() – Syntax and Parameters :

cv2.ellipse(image, center, axes, angle, startAngle, endAngle, color, thickness)

Code Example – Creating Ellipses :

import cv2
import numpy as np

# Create a black image
image = np.zeros((400, 400, 3), dtype=np.uint8)

# Define ellipse parameters
center = (200, 200)
axes = (100, 50)
angle = 30
startAngle = 0
endAngle = 360
color = (0, 255, 0)
thickness = 2

# Draw the ellipse on the image
cv2.ellipse(image, center, axes, angle, startAngle, endAngle, color, thickness)

# Display the image
cv2.imshow('Ellipse', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this code example, we start by creating a black image using NumPy – a canvas for our ellipse. We then define the parameters of the ellipse, including the center coordinates, lengths of major and minor axes, rotation angle, and arc angles.

Using cv2.ellipse(), we draw the ellipse on the image with the specified parameters. The ellipse will be green (color (0, 255, 0)) and have a boundary line thickness of 2.

Finally, we display the image using cv2.imshow(), and the ellipses’ graceful curves come to life.

Creating ellipses in OpenCV isn’t merely about drawing shapes; it’s about crafting curves that encapsulate symmetry and elegance. Through the cv2.ellipse() function, we mold pixels into forms that mirror the beauty found in the natural world. As we master the art of creating ellipses, we unlock the potential to enhance our understanding of shapes and patterns, enriching our connection with the visual tapestry that surrounds us.

Summary

In this enlightening journey, we have delved into the fascinating world of computer vision and the boundless artistic potential of OpenCV. Through the use of three fundamental functions – cv2.circle(), cv2.line(), and cv2.rectangle() – we’ve harnessed the power to draw captivating circles, lines, and rectangles on digital canvases.

OpenCV has unveiled a realm of creative expression, offering a seamless blend of technology and artistry. So, wield the brush of code, let your creativity flow, and paint your imagination with the art of geometric design. OpenCV has ignited the spark, and the canvas of possibilities awaits your next stroke of brilliance.

Exit mobile version