边界框未显示在图中

Bounding box is not shown in plot

我尝试用中心点和边界框绘制我的图像。在变量 rect 中是高度、宽度、角度和中心点的信息,因此我假设轮廓和所有内容都已正确找到。但是为什么剧情里没有显示呢?

hierachy, img_threshold_32bit = cv2.threshold(img_hr, 100, 255, cv2.THRESH_BINARY)
img_8bit = np.array(img_threshold_32bit,dtype=np.uint8)    
contours,_ = cv2.findContours(img_8bit, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)    
cv2.drawContours(img_8bit, contours, -1, (0, 255, 0), 2, cv2.LINE_AA)

for cnt in contours:         
      rect = cv2.minAreaRect(cnt)
      box = cv2.boxPoints(rect)
      box = np.int0(box)
      cv2.drawContours(img_8bit,[box],0,(0,0,255),2)
      cv2.circle(img_8bit,(int(rect[0][0]),int(rect[0][1])),5,(255,0,0),-1)

plt.imshow(img_8bit)

感谢您的帮助

您使用的是 3 通道图像,因此请使用

而不是 plt.imshow()
cv2.imshow("image name",img_8bit)
cv2.waitKey(0)
cv2.destroyAllWindows()

这对我也有用。希望这能解决您的问题。

由于图像是二进制的,首先我需要将它转换回 3 通道。在此之后边界框在图像中正确显示。