画矩形 python
时间: 2023-11-07 07:04:14 浏览: 41
画矩形的python代码如下:
```python
import numpy as np
import cv2 as cv
img = np.zeros((320, 320, 3), np.uint8) # 生成一个空灰度图像
ptLeftTop = (60, 60) # 矩形左上角的坐标
ptRightBottom = (260, 260) # 矩形右下角的坐标
point_color = (0, 255, 0) # 线条颜色,BGR格式
thickness = 1 # 线条宽度
lineType = 4
# 绘制绿色矩形
cv.rectangle(img, ptLeftTop, ptRightBottom, point_color, thickness, lineType)
ptLeftTop = (120, 100) # 红色矩形左上角的坐标
ptRightBottom = (200, 150) # 红色矩形右下角的坐标
point_color = (0, 0, 255) # 线条颜色,BGR格式
thickness = 1 # 线条宽度
lineType = 8
# 绘制红色矩形
cv.rectangle(img, ptLeftTop, ptRightBottom, point_color, thickness, lineType)
cv.namedWindow("AlanWang")
cv.imshow('AlanWang', img)
cv.waitKey(10000) # 显示 10000 ms 即 10s 后消失
cv.destroyAllWindows()
```