python opencv 绘制矩形框

代码:

img = cv2.imread(img_path)

# 注意这里的坐标点需要是int
start_point = (round(xmin), round(ymin))
end_point = (round(xmax), round(ymax))
cv2.rectangle(img, start_point, end_point,  (0, 255, 0), 2)

font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, f'{category_name} {confidence:.2f}', start_point, font, 1, (0, 0, 255), 3)

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.show()

需要注意的是:

  • rectangle里面的坐标参数必须为整形的,要不然会出TypeError: an integer is required (got type tuple),错误

你可能感兴趣的:(作图)