Search code examples
pythonopencvbounding-box

Bounding box is not shown in plot


I tried to plot my image with the center point and the bounding-box. In the variable rect are the information for heigh, weidth, angle and center point, so I assume that the contours and everything is found correctly. But why is it not shown in the plot?

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)

Thanks for your help


Solution

  • Since the image was binary, first I need to convert it back to 3 channels. After this the bounding box is shown correctly in the image.