1、利用cv2.rectangle()绘制bounding box
import cv2
cv2.rectangle(img, (x,y), (x+w,y+h), (B,G,R), Thickness)
参数如下:
img:图像
(x,y):bounding box左上角点坐标,图像最左上角坐标为(0,0)
(x+w,y+h):bounding box右下角点坐标
(B,G,R):颜色
Thickness:粗细
2、示例
import cv2
imgname = '001.jpg'
img = cv2.imread(imgname)
# 画矩形框
cv2.rectangle(img, (212,317), (290,436), (0,255,0), 4)
# 显示图像
cv2.imshow('show', img)
#一定要加这一句,否则图片会一闪而过
cv2.waitKey(0)
3、附加:读取groundtruth和检测结果文件,并绘制在同一图片上的代码
import cv2
gdfile = 'dataset/1/test.txt'
dtfile = 'result/box.txt'
imagefile = 'result/constant/yi'
with open(gdfile) as gddata, open(dtfile) as dtdata:
gdlines = gddata.readlines()
dtlines = dtdata.readlin