1 import numpy as np 2 import cv2 3 4 img = np.zeros((512, 512, 3), np.uint8) #创建灰度值都为0的彩图 5 6 img = cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5) #五个参数:图片名称、起点坐标、终点坐标、颜色数组、线宽 7 8 img = cv2.rectangle(img, (384, 0), (510, 128), (0, 255, 0), 3) #五个参数:图片名称、两个对角点坐标、颜色数组、线宽 9 10 img = cv2.circle(img, (447, 63), 63, (0, 0, 255), -1) #五个参数:图片名称、圆心坐标、半径、颜色数组、线宽 11 12 img = cv2.ellipse(img, (256, 256), (100, 50), 0, 90, 360, (0, 255, 0), -1) #有八个参数:图像、中心坐标、长轴短轴、旋转角度、起始角度、结束角度、颜色数组、线宽 13 14 pts = np.array([[10, 5], [25, 30], [100, 50], [50, 10]], np.int32) 16 pts = pts.reshape(-1, 1, 2) 17 img = cv2.polylines(img, [pts], True, (0, 255, 255), 3) #五个参数:图像、顶点集、是否闭合、颜色数组、线宽
19 20 font = cv2.FONT_HERSHEY_SIMPLEX 21 cv2.putText(img, 'OpenCV', (10, 500), font, 4, (255, 255, 255), 2, cv2.LINE_AA) #八个参数:图片、字符串、坐标、字体、字号、颜色数组、线宽、线条种类 22 23 cv2.imshow('demo', img) 24 cv2.waitKey(0)
效果如图: