Today we are going to explore an example of code for drawing images, actually we can draw polygons like triangles, quadrangles, etc… circles, lines.
We are using Numpy (Numerical Python) library for generating an image.
import cv2
import numpy as np
image = np.zeros((600,600,3), np.uint8)
cv2.line(image, (600,600), (0,0), (255,99,71), 5)
cv2.imshow(“Blue Line”, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
line 1 we import cv2 library
line 2 we import numpy library and we assign the alias of np
line 3 we create an image variable with np where np.uint8 is the type (unsigned integer (0 to 255)) and np.zeroes creates a new array of given shape (int or tuple of ints) and type, filled with zeros.
line 4 we draw a line on image
line 5 we show the image
line 6 wait for a key pressed
line 7 liberate resources

Of course we can draw as I said polygons, circles etc…
for example replacing line 4 by this line we can draw a rectangle :
cv2.rectangle(image, (200,200), (600,500), (255,99,71), -1)
We can draw a circle replacing line 4 by the following line :
cv2.circle(image, (700, 700), 180, (255,99,71), -1)
We can draw a polygon replacing line 4 by the following :
points = np.array( [[15,60], [600,80], [120,150], [55,200]], np.int32)
points = points.reshape((-1,1,2))
cv2.polylines(image, [points], True, (255,99,71), 3)
That’s it… plane and simple…