How to draw basic shapes on Images in Python using OpenCV – easy project – 2024

While playing with images in OpenCV we can do various fun stuff using its inbuilt functions. One of them is drawing basic shapes like circles, rectangles, ellipses, and lines on images.

In today’s very short blog we will see that how we can draw these basic shapes on a black image. This is going to be a very interesting blog, so without any further due, Let’s do it…

Step 1 – Import the required packages.

import cv2
import numpy as np

Step 2 – Create a Black image.

img = np.zeros((512,512,3), np.uint8)
  • Yes, you read that right. We can create an image also in OpenCV on which we will draw our shapes.
  • What this line will do is, it will create a 3-D array of all zeros with height and width both set to 512 pixels. The three dimensions here depict RGB channels.
  • When all these are 0 it will act as a black image because we know that (0,0,0) is black and (255,255,255) is white. Every pixel of this image will be (0,0,0).

Step 3 – Visualize our black image.

cv2.imshow('Black Image', img)
cv2.waitKey(0)
shapes
The black image that we created above

Let’s draw our shapes…

Line

Syntax – cv2.line(image, start_point, end_point, color, thickness)

Parameters:

imageIt is the image on which line is to be drawn.
start_pointIt is the starting coordinates of the line. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value).
end_pointIt is the ending coordinates of line. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value).
colorIt is the color of the line to be drawn. For BGR, we pass a tuple. eg: (255, 0, 0) for blue color.
thickness: It is the thickness of the line in px.
cv2.line(img, (0, 0), (512, 512), (0, 225, 0))
shapes drawn = line
line drawn

Rectangle

Syntax: cv2.rectangle(image, start_point, end_point, color, thickness)

Parameters:

imageIt is the image on which a rectangle is to be drawn.
start_pointIt is the starting coordinates of the rectangle. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value).
end_pointIt is the ending coordinates of the rectangle. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value).
colorIt is the color of the borderline of the rectangle to be drawn. For BGR, we pass a tuple. eg: (255, 0, 0) for blue color.
thicknessIt is the thickness of the rectangle borderline in px. The thickness of -1 px will fill the rectangle shape by the specified color.
cv2.rectangle(img, (20, 40), (160, 140), (225, 225, 225), -1)
shapes drawn = rectangle
rectangle drawn and filled using -1

Circle

Syntax: cv2.circle(image, center_coordinates, radius, color, thickness)

Parameters:

imageIt is the image on which a circle is to be drawn. 
center_coordinatesIt is the center coordinates of circle. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value). 
radiusIt is the radius of circle. 
colorIt is the color of border line of circle to be drawn. For BGR, we pass a tuple. eg: (255, 0, 0) for blue color. 
thicknessIt is the thickness of the circle border line in px. Thickness of -1 px will fill the circle shape by the specified color.
cv2.circle(img, (206, 206), 50, (225, 225, 225), -1)
shapes drawn = circle
circle drawn and filled using -1

Ellipse

Syntax: cv2.ellipse(image, center coordinates, axesLength, angle, startAngle, endAngle, color [, thickness[, lineType[, shift]]])

Parameters:

imageIt is the image on which ellipse is to be drawn.
center_coordinatesIt is the center coordinates of ellipse. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value).
axesLengthIt contains tuple of two variables containing major and minor axis of ellipse (major axis length, minor axis length).
angleEllipse rotation angle in degrees.
startAngleStarting angle of the elliptic arc in degrees.
endAngleEnding angle of the elliptic arc in degrees.
colorIt is the color of border line of shape to be drawn. For BGR, we pass a tuple. eg: (255, 0, 0) for blue color.
thicknessIt is the thickness of the shape border line in px. Thickness of -1 px will fill the shape by the specified color.
lineTypeThis is an optional parameter.It gives the type of the ellipse boundary.
shiftThis is an optional parameter. It denotes the number of fractional bits in the coordinates of the center and values of axes.
cv2.ellipse(img, (380, 380), (50, 20), 0, 0, 360, (225, 225, 225), -1)
shapes drwan = ellipse
Ellipse is drawn and filled using -1

NOTE: You can read more about drawing shapes here.

Do let me know if there’s any query regarding this topic by contacting me on email or LinkedIn.

So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…

Read my previous post: HOW TO RESIZE AN IMAGE IN PYTHON USING OPENCV

Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at machinelearningprojects.net.

Leave a Reply

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