矩形、圆形、任意多边形的绘制
import cv2
import numpy as np
newImgInfo = (500, 500, 3)
dst = np.zeros(newImgInfo, np.uint8)
# 1) 蓝色矩形 参数: 图片dst 左上角:(50, 100) 右下角:(200, 300) 颜色:(255,0,0)
# 填充类型5:不填充(线条宽度) <0(-1): 填充
cv2.rectangle(dst, (50, 100), (200, 300), (255, 0, 0), 5)
# 2) 绿色圆 参数:图片dst 圆心 半径 颜色 画笔宽度:2
cv2.circle(dst, (250, 250), 100, (0, 255, 0), 5)
# 3) 圆弧 参数:图片 center 轴 偏转角angle 5begin 6end 7color 8填充与否:-1填充
cv2.ellipse(dst, (256,256), (150, 100), 0, 0, 180, (255, 255, 0), -1)
# 4)任意多边形
points = np.array([[150, 50], [140, 140], [200, 170], [250, 250], [150, 50]], np.int32)
print(points.shape) # (5, 2)
# 转置
points = points.reshape(-1, 1, 2)
print(points.shape) # (5, 1, 2)
cv2.polylines(dst, [points], True, (0, 255, 255))
cv2.imshow('dst', dst)
cv2.waitKey(0)
结果如下:

网友评论