The basic operations of OpenCV is to draw over images. The ability to add different geometric shapes just like lines, circles and rectangle etc.
OpenCV provides many drawing functions to draw geometric shapes and write text on images. some of the drawing and text writing functions are listed below.
cv2.arrowedLine(): This function is used to draw arrowed-line on image
cv2.rectangle() : This function is used to draw rectangle on an image.
cv2.circle() : This function is used to draw circle on an image.
cv2.ellipse() : This function is used to draw ellipse on image.
cv2.polylines() : This function is used to draw polygon on image.
cv2.putText() : This function is used to write text on image.
cv2.line(img, (pt1, pt2), (pt3, pt4), color, thickness)
img – Image.
pt1, pt2 – Starting point(x1,y1) of the line segment.
pt3, pt4 – Ending point(x2,y2) of the line segment.
color – Line color in BGR mode.
thickness – Line thickness.
cv2.arrowedline(img, (pt1, pt2), (pt3, pt4), color, thickness)
img – Image.
pt1, pt2 – Starting point(x1,y1) of the line segment.
pt3, pt4 – Ending point(x2,y2) of the line segment.
color – Line color in BGR mode.
thickness – Line thickness.
cv2.rectangle(img, (pt1, pt2), (pt3, pt4), color, thickness)
img – Image.
pt1, pt2 – Top left corner point(x1,y1) of the rectangle.
pt3, pt4 – Bottom right corner point(x2,y2) of the rectangle.
color – Rectangle color in BGR mode.
thickness – Rectangle thickness.
(if the thickness = -1 then we get a filled rectangle)
cv2.circle(img, (pt1, pt2), radius, color, thickness)
img – Image.
pt1, pt2 – Center coordinates if the circle.
radius - Radius of the circle
color – Circle color in BGR mode.
thickness – Circle thickness.
(if the thickness = -1 then we get a filled circle)
cv2.polylines(img, array, true/false, color, thickness)
img – Image.
array – The array of coordinates
true/false – True, if it is a closed line
color – Color of the stroke.
thickness – Stroke thickness.
(if the thickness = -1 then we get a filled polygon)
cv2.ellipse(img, (pt1, pt2), (h, w), r_angle, s_angle, f_angle, color, thickness)
img – Image.
pt1, pt2 – Center Coordinates of the ellipse.
h, w – Length of the minor and major axes
r_angle - Rotation angle of the ellipse (calculated clockwise)
s_angle - Starting angle (calculated clockwise)
f_angle - Final angle (calculated clockwise)
color – Color of the text.
thickness – Text thickness.
(if the thickness = -1 then we get a filled ellipse)
cv2.putText(img, (pt1, pt2), font_style, size, color, thickness, line_style)
img – Image.
pt1, pt2 – Starting point of the text.
font_style – Style of the font.
color – Color of the text.
thickness – Text thickness.
line_style – Line style of the font.
- Writing text on image used to describe the image.
- By using gemetrical shapes we can highlight any particular area in the image.
- Geometrical shapes like line helps us to specify some object in the image.