NumPy provides multidimensional arrays for numerical data. In camera work, OpenCV returns each captured frame as a NumPy ndarray, so image pixels can be inspected, created, and passed directly to OpenCV drawing functions. section 8.3.1 section 8.5
A frame with shape (480, 640, 3) has 480 rows, 640 columns, and three color components per pixel. Captured camera data uses the uint8 data type in this workflow. section 8.3.1
Create a blank image
Import NumPy as np and call np.zeros(shape=(480, 640, 3), dtype=np.uint8) to make a black 640-by-480 BGR image. OpenCV can then draw circles, lines, rectangles, and text onto that array before displaying it. section 8.3.2
Listing 8.1 draw.py: Drawing shapes of different sizes and positions using OpenCV
#!/usr/bin/env python3
import string
import numpy as np
import cv2
BLUE = (255, 0, 0)
GREEN = (0, 255, 0)
RED = (0, 0, 255)
img = np.zeros(shape=(480, 640, 3), dtype=np.uint8)
cv2.circle(img, center=(200, 110), radius=100, color=RED)
cv2.line(img, pt1=(0, 0), pt2=(200, 110), color=GREEN)
cv2.rectangle(img, pt1=(50, 250), pt2=(350, 350), color=BLUE)
text = string.ascii_lowercase
font = cv2.FONT_HERSHEY_SIMPLEX
pos = (10, 380)
cv2.putText(img, text, org=pos, fontFace=font, fontScale=1, color=RED)
cv2.imshow('preview', img)
cv2.waitKey()
This runnable script creates a zero-filled NumPy image array and uses it as the canvas for OpenCV drawing operations. section 8.3.2






