How to draw basic shapes on images using OpenCV?
Drawing shapes on the images is very easy with OpenCV. In this section, we will learn how to draw lines, circles, rectangles, ellipses, and text on the image.
Drawing Line
To draw a line, we will use OpenCV's line() function. The formal parameters of the line() function implemented in OpenCV are shown as below:
line(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
Parameter |
Description |
img |
The input image. |
pt1 |
First point of the line segment. |
pt2 |
Second point of the line segment. |
color |
Line color. |
thickness |
Line thickness. |
lineType |
Type of the line, such as filled line etc. |
shift |
Number of fractional bits in the point coordinates |
Let us use the line() function to draw a line on a dark image.
1
2
3
4
5
6
7
|
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = np.zeros((200,200,3), np.uint8)
cv2.line(img,(0,100),(200,100),(255,0,0),5)
plt.imshow(img)
plt.axis('off')
|
Line 4: A new black RGB image is created using Numpy
Line 5: A red line is drawn in the middle of the image, as shown below.
Line 6: The image is displayed using PyPlot package. Note that I provided the thickness parameter (5) in the line() method to make the line think.
Figure 1. Draw line on image using line() method.
Drawing Circle
We can draw a circle in OpenCV using the circle() function. To draw a circle, we need its center coordinates and radius. Let us see an example. The following code shows how to draw a red circle on a black image.
cv.circle( img, center, radius, color[, thickness[, lineType[, shift]]] )
Parameter |
Description |
img |
The input image. |
center |
Center of the circle. |
radius |
Radius of the circle. |
color |
Circle color. |
thickness |
Thickness of the circle outline, if positive. Negative values, like FILLED, mean that a filled circle is to be drawn. |
lineType |
Type of the line, such as filled line etc. |
shift |
Number of fractional bits in the point coordinates |
Let us use the circle() function to draw a circle on a dark image.
1
2
3
4
5
6
7
|
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = np.zeros((200,200,3), np.uint8)
cv2.circle(img,(100,100),50,(255,0,0),2)
plt.imshow(img)
plt.axis('off')
|
Line 6: A red circle is drawn with a center at (100,100) and a radius 50.
Figure 2. Draw circle on image using circle() method.
We can also draw a filled circle by changing the thickness parameter to -1 in the circle() function. Line 6 in above code can be updated as below:
|
cv2.circle(img,(100,100),50,(255,0,0),-1)
|
Figure 3. Draw a filled circle on image.
Drawing Ellipse
We can draw an ellipse in OpenCV using the ellipse() function. The complete signature of the function is given below:
cv.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]])
Parameter |
Description |
img |
The input image. |
center |
Center of the ellipse. |
Axes |
Axes lengths (major axis, minor axis) |
angle |
The angle of rotation of ellipse in anti-clockwise direction |
startAngle |
The starting of ellipse arc measured in the clockwise direction from the major axis |
endAngle |
The endAngle denotes the ending of ellipse arc measured in clockwise direction from major axis. If we give startangle=0 and endangle=360, it will give the full ellipse |
color |
Circle color. |
thickness |
Thickness of the circle outline, if positive. Negative values, like FILLED, mean that a filled circle is to be drawn. |
lineType |
Type of the line, such as filled line etc. |
shift |
Number of fractional bits in the point coordinates |
Let us use the ellipse() function to draw an ellipse on a dark image. The following code will draw an ellipse on an image.
1
2
3
4
5
6
7
|
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = np.zeros((200,200,3), np.uint8)
cv2.ellipse(img,(100,100),(80,40),45,0,360,(255,0,0),2)
plt.imshow(img)
plt.axis('off')
|
Line 5: This function draws the ellipse. The center is located at (100,100), length of major axis and minor axis is (80,40), the angle of ellipse is 45, it starts drawing from 0 to 360 means a full ellipse. Then we provide color and thickness. The output is as shown below:
Figure 4. Draw ellipse on image.
We can draw a filled ellipse by updating the line 6 as shown below:
|
cv2.ellipse(img,(100,100),(80,40),0,0,360,(255,0,0),-1)
|
Notice that we updated the thickness to -1. We get the following output:
Figure 5. Draw a filled ellipse on image.
Drawing polygons
We can draw an polygons in OpenCV using the polylines() function or we can also use fillpoly() to draw filled polygons. The complete signature of the polylines() function is given below:
polylines( img, pts, isClosed, color[, thickness[, lineType[, shift]]] )
The signature of the fillpoly() is almost the same as the polylines() function:
cv.fillPoly( img, pts, color[, lineType[, shift[, offset]]] )
Parameter |
Description |
img |
The input image. |
Pts |
Array of polygons where each polygon is represented as an array of points. |
isClosed |
Flag indicating whether the drawn polylines are closed or not. If they are closed, the function draws a line from the last vertex of each curve to its first vertex. |
color |
Line color. |
thickness |
Thickness of the outline. |
lineType |
Type of the line, such as filled line etc. |
shift |
Number of fractional bits in the point coordinates |
To draw a polygon, the method expects a Numpy array. It will be an array of vertices of the polygon. The following code example shows how you can draw a polygon using polylines() method;
1
2
3
4
5
6
7
8
|
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = np.zeros((200,200,3), np.uint8)
vertices = np.array([[100, 50], [150, 150], [50, 150]], np.int32)
cv2.polylines(img, [vertices], True, (255,0,0), 2)
plt.imshow(img)
plt.axis('off')
|
Line 4: A Numpy array is created with three points which mean we are going to draw a triangle
Line 5:Using polylines() function, the polygon is drawn. Notice that we provided the array within []. Also, we mentioned we want to draw a closed polygon.
The output obtained for the above code is shown below:
Figure 6. Draw a polygon on image.
When we change the third parameter to False, then we get the following output:
Figure 7. Draw a polygon on image.
You can see the difference. When we set the IsClosed to False, the last and first points of the polygon are not connected. Similarly, we can draw a filled polygon using fillpoly() function. We can update line 6 in the above as follows to draw a filled polygon:
|
cv2.fillPoly(img, [vertices], (255,0,0))
|
We get the following output when we update line 6 in the above code:
Figure 8. Draw a filled polygon on image.
Interestingly, we can draw multiple polygons at the same time using drawpoly() or fillpoly() functions. In this case, we will provide a list of multiple polygons. The following code draws multiple polygons on an image:
1
2
3
4
5
6
7
8
|
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = np.zeros((200,200,3), np.uint8)
vertices = np.array([[[100, 50], [150, 150], [50, 150]],[[10, 10], [40, 10], [50, 50]],[[100, 10], [150, 10], [150, 50]]], np.int32)
cv2.polylines(img, vertices, True, (255,0,0), 2)
plt.imshow(img)
plt.axis('off')
|
Executing above code will produce the output shown below. It is very flexible to draw multiple polygons at the same time using the polylines() function in OpenCV.
Figure 9. Draw a filled polygon on image.