OpenCV中的rectangle() 为绘制矩形命令,形式如下:
# (img: cv2.typing.MatLike, pt1: cv2.typing.Point, pt2: cv2.typing.Point, color: cv2.typing.Scalar, thickness: int = ..., lineType: int = ..., shift: int = ...)
cv2.rectangle(img, pt1, pt2, color, thickness, lineType, shift)
参数说明:
img:要绘制的图片, pt1:矩形框左上角坐标(x,y), pt2矩形框右下角坐标 (x,y), color:颜色(B,G,R), thickness:线宽(整数), lineType:线型(整数,可不填), shift:坐标小数点位数(整数,可不填)
示例:
import numpy as np
import cv2 as cv
img = np.zeros((255, 255, 3), np.uint8) # 生成一个255×255大小的三通道黑色图像
# 矩形左上角和右上角的坐标(x,y),绘制一个蓝色矩形
cv.rectangle(img, pt1=(30, 40), pt2=(160, 220), color=(255, 0, 0), thickness=1, lineType=1)
cv.namedWindow("exp")
cv.imshow('exp', img)
cv.waitKey(0) # 按空格退出
cv.destroyAllWindows()