Draw geometric shapes on images with OpenCV



Some of the drawing functions:

cv2.line (): Used to draw line on an image.
cv2.rectangle ( ): Used to draw rectangle on an image.
cv2.circle (): Used to draw circle on an image.
cv2.putText ( ): Used to write text on image.

To demonstrate the use of the above functions, we need a 400 X 400 image filled with a solid color (in this case, black). To do this, we can use the numpy.zeroes function to create the required image.

# Python3 program for painting with solid color
# image using numpy.zeroes ()

import numpy as np

import cv2

 
# Create a black image with 3 channels
# RGB and unsigned int type

img = np.zeros (( 400 , 400 , 3 ), dtype = " uint8 " )

cv2.imshow ( `dark` , img)

 
# Allows us to see the image
# before forced closure

cv2.waitKey ( 0 )

cv2.destroyAllWindows ()

Output:

Now let`s draw some geometric shapes on this solid black image.

Draw a line:

cv2.line (imageObjectName , (`start_coordinates`), (`end_coordinates`), (`color_in_bgr`), `line_thickness`)

# Python3 line drawing program
# solid image form

import numpy as np

import cv2

 
# Create a black image with 3 channels
# RGB and unsigned int type

img = np.zeros (( 400 , 400 , 3 ), dtype = "uint8" )

  
# Create line

cv2.line (img, ( 20 , 160 ), ( 100 , 160 ), ( 0 , 0 , 255 ), 10 )

 

cv2.imshow ( `dark` , img)

  
# Allows us to see the image
# until forced closure

cv2.waitKey ( 0 )

cv2.destroyAllWindows ()

Output:

Draw a rectangle:

cv2.rectangle (imageObjectName, (`top_left_vertex_coordinates`), (` lower_right_vertex_coordinates`), (`stroke_color_in_bgr`), `stroke_thickness `)

# Python3 rectangle drawing program
# solid shape image

import numpy as np

import cv2

 
# Create a black image with 3
# RGB channels and unsigned int type

img = np.zeros (( 400 , 400 , 3 ), dtype = " uint8 " )

  
# Create rectangle

cv2.rectangle (img, ( 30 , 30 ), ( 300 , 200 ), ( 0 , 255 , 0 ), 5 )

 

cv2.imshow ( `dark` , img)

 
# Allows us to see the image
# before forced closure

cv2.waitKey ( 0 )

cv2.destroyAllWindows ()

Output:

Draw a circle:

cv2.circle (imageObjectName, (`center_coordinates`), (`circle_radius`), (`color_in_bgr`), `stroke_thickness` )

# Python3 circle drawing program
# solid image shape

import numpy as np

import cv2

 
# Create a black image with 3
# RGB channels and unsigned int type

img = np.zeros (( 400 , 400 , 3 ), dtype = "uint8" )

 
# Create a circle

cv2.circle (img , ( 200 , 200 ), 80 , ( 255 , 0 , 0 ), 3 )

 

cv2.imshow ( `dark` , img)

 
# Lets us see the image
# before forced closure

cv2.waitKey ( 0 )

cv2.destroyAllWindows ()

Output:

Writing text:

cv2.putText ( imageObjectName, `TextContent`, (`text_starting_point_coordinates`), `fontToBeUsed`, `font_size`, (`text_color`, `text_thickness`, `line_type`)

# Python3 writing program
# solid text

import numpy as np

import cv2

 
# Create a black image with 3
# RGB channels and unsigned int type

img = np.zeros (( 400 , 400 , 3 ), dtype = "uint8" )

 
# writing text

font = cv2.FONT_HERSHEY_SIMPLEX

cv2.putText (img, `GeeksForGeeks` , ( 50 , 50 ),

font, 0.8 , ( 0 , 255 , 0  ), 2 , cv2.LINE_AA)

 

cv2.imshow ( `dark` , img)

 
# Allows us to see the image
# before forced closing

cv2.waitKey ( 0 )

cv2.destroyAllWindows ()

Output:


Applications for drawing figures on images:

  • Drawing geometric shapes can help us highlight parts of an image.
  • Geometric shapes such as a line can help us with show or identify specific areas in an image.
  • Writing text on specific areas of an image can add description to that area.

Link:
https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html