绘图
import numpy as np
import cv2
# Create a black image
# shape: 512*512 RGB 3个通道
# 初始化每个像素点 (0,0,0) 所以是黑色背景
img = np.zeros((512, 512, 3), np.uint8)
# 1.线
# draw a diagonal blue line with thickness of 1 px
cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 1)
cv2.line(img, (511, 0), (0, 511), (0, 255, 0), 1)
cv2.line(img, (0, 255), (511, 255), (0, 0, 255), 1)
cv2.line(img, (255, 0), (255, 511), (0, 0, 255), 1)
# 2.矩形
cv2.rectangle(img, (100, 100), (411, 411), (255, 255, 255), 2)
# 3.圆
cv2.circle(img, (255, 255), 100, (255, 255, 0), -1) # -1向内填充
# 4.椭圆
cv2.ellipse(img, (255, 255), (100, 50), 0, 0, 360, (255, 0, 255), -1) # (100,50) 长短半轴
# 5.多边形
# 指定每个顶点坐标,构建顶点数组: 行数*1*2,行数就是点的数目,这个数组必须为int32
pts1 = np.array([[20, 20], [30, 100], [70, 20]], np.int32)
# pts = pts.reshape((-1, 1, 2)) # -1表示第1维长度是根据后面的维度计算的
# 参数2: [pts] numpy.ndarray 转成 list 要求是 numpy 的 array,不能直接传入list数组,因为要求是np.int32格式
# 参数3: True isClosed 多边形是否闭合
cv2.polylines(img, [pts1], True, (0, 255, 255), 3)
# 右边再画个三角
pts2 = np.array([[492, 20], [482, 100], [442, 20]], np.int32)
cv2.polylines(img, [pts2], True, (0, 255, 255), 3)
# 6.图片上添加文字
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, 'OpenCV', (70, 490), font, 3, (255, 255, 255), 2, cv2.LINE_AA)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()