Skip to content

Latest commit

 

History

History
157 lines (103 loc) · 4.61 KB

Chirag _OpenCV_GeometricShapes.md

File metadata and controls

157 lines (103 loc) · 4.61 KB

Draw geometric shapes and write text on images using OpenCV

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.line() : This function is used to draw line on an image.
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.

Now let's see the attributes of each function in order to use it.

1) cv2.line()

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

Parameters:

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.

Figure 1. Parameters of Line :




2) cv2.arrowedLine()

cv2.arrowedline(img, (pt1, pt2), (pt3, pt4), color, thickness)

Parameters:

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.

Figure 2. Parameters of Arrowedline :

3) cv2.rectangle()

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

Parameters:

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)

Figure 3. Parameters of Rectangle :

4) cv2.circle()

cv2.circle(img, (pt1, pt2), radius, color, thickness)

Parameters:

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)

Figure 4. Parameters of Circle :

5) cv2.polylines()

cv2.polylines(img, array, true/false, color, thickness)

Parameters:

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)

Figure 5. Parameters of Polygon :

6) cv2.ellipse()

cv2.ellipse(img, (pt1, pt2), (h, w), r_angle, s_angle, f_angle, color, thickness)

Parameters:

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)

Figure 6. Parameters of Ellipse :

7) cv2.putText()

cv2.putText(img, (pt1, pt2), font_style, size, color, thickness, line_style)

Parameters:

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.

Figure 7. Parameters of Text :



Why to draw shapes and write text on images :

  • 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.