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),錯誤
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章