0

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

lkrss
  • 57
  • 1
  • 9
  • you're drawing a black rect, and probably you just don't see it. Try with a brighter tone of gray: `cv2.drawContours(img_8bit,[box],0,(200,0,0),2)` – Miki Jul 07 '21 at 17:08
  • No, `(0,0,255)` is red and `(255,0,0)` is blue – lkrss Jul 07 '21 at 18:13
  • https://stackoverflow.com/a/68295531/222216 – Birol Kuyumcu Jul 08 '21 at 04:23
  • since you're drawing on a 1 channel image, you should consider only the first channel. So (0, ..., ...) is black, and (255, ..., ...) is white. (200,...,...) is a light gray. – Miki Jul 08 '21 at 08:56
  • But `drawContours` is independently from my chanels of the image. I only scale with the values in the brackets the color of the line of the bounding box. Check **https://docs.opencv.org/3.4/d6/d6e/group__imgproc__draw.html#ga746c0625f1781f1ffc9056259103edbc**. I also tried it your way and it doesnt work. – lkrss Jul 08 '21 at 10:06
  • Try to draw and plot with your original BGR image (not with `img_8bit`) and let see the result. – R. Marolahy Jul 09 '21 at 13:15

2 Answers2

0

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.

Sushant Agarwal
  • 440
  • 4
  • 10
0

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.

lkrss
  • 57
  • 1
  • 9