简体   繁体   中英

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

You are using a 3 channel image, so instead of plt.imshow() use

cv2.imshow("image name",img_8bit)
cv2.waitKey(0)
cv2.destroyAllWindows()

This worked for me as well. Hope this solves your problem.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM