How to Draw Shapes (Lines, Circles, Rectangles) on Images Using OpenCV

In this tutorial, we’ll explore OpenCV, short for Open Source Computer Vision Library is one of the most popular and widely used libraries for Computer Vision and image processing. Originally developed by Intel in 1999, OpenCV has become a cornerstone in the field of Computer Vision, enabling developers and researchers to create real-time applications that analyze and interpret visual data.

What is OpenCV?

OpenCV is an open-source library designed to provide a unified infrastructure for Computer Vision applications. It supports image processing, video analysis, object detection, machine learning, and real-time operations. It is cross-platform, supporting programming languages like Python, C++, Java, and more, and runs on operating systems such as Windows, Linux, macOS, Android, and iOS.

How to Get Started with OpenCV

To start using OpenCV in Python:
Install the library:

pip install opencv-python

Next, import the library in your Python script:

import cv2
import numpy as np
Drawing Shapes on an Image

Let’s go step by step and learn how to draw lines, rectangles, and circles.

1. Create a Blank Image

First, create a blank canvas where you can draw shapes:

image = np.zeros((512, 512, 3), dtype=np.uint8)


2. Draw a Line

To draw a line, use the cv2.line() function:

# Draw a white line
cv2.line(image, (50, 50), (450, 50), (255, 255, 255), thickness=5)
  • Explaintation:

    1. image: The image where the line will be drawn.

    2. (50, 50): Starting point of the line.

    3. (450, 50): Ending point of the line.

    4. (255, 255, 255): Color in BGR format (white in this case).

    5. thickness==5: Thickness of the line.

output:

Line Image

Terminal:
array([[[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       ...,

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]], dtype=uint8)
3. Draw a Rectangle

Use cv2.rectangle( ) to draw a rectangle:

# Draw a green rectangle 
cv2.rectangle(image, (100, 100), (400, 300), (0, 255, 0), thickness=3)
  • Explaintation:

    1. (100, 100): Top-left corner of the rectangle.

    2. (400, 300): Bottom-right corner of the rectangle.

    3. (0, 255, 0): Green color in BGR format.

    4. thickness=3: Thickness of the rectangle border. Use -1 to fill it.

Output:

Rectangle Image 

Terminal:
array([[[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       ...,

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]], dtype=uint8)

4. Draw a Circle

To draw a circle, use cv2.circle():

# Draw a blue circle
cv2.circle(image, (256, 256), radius=50, color=(255, 0, 0), thickness=-1)
  • Explaintation:

    1. (256, 256): Center of the circle.

    2. radius=50: Radius of the circle.

    3. (255, 0, 0):  Blue color in BGR format.

    4. thickness=-1: Fills the circle when thickness is negative.

Output:

Circle Image

Terminal:
array([[[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       ...,

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]], dtype=uint8)

Displaying the Image

After drawing these shapes on your image:

# Display the image with shapes
cv2.imshow("Shapes", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Complete Code Example

Here’s the full code combining all the steps:

import cv2
import numpy as np

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

# Draw a white line
cv2.line(image, (50, 50), (450, 50), (255, 255, 255), thickness=5)

# Draw a green rectangle
cv2.rectangle(image, (100, 100), (400, 300), (0, 255, 0), thickness=3)

# Draw a blue circle
cv2.circle(image, (256, 256), radius=50, color=(255, 0, 0), thickness=-1)

# Display the result
cv2.imshow("Shapes", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:

OpenCV Image

Conclusion

OpenCV is a versatile tool that empowers developers to create innovative solutions in Computer Vision. Its open-source nature ensures continuous evolution through community contributions, making it a vital resource for academic research and commercial applications alike. Whether you’re a beginner or an expert, OpenCV offers the tools necessary to unlock the potential of visual data processing.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top