【问题标题】:Bounding box is not shown in plot图中未显示边界框
【发布时间】:2021-09-18 04:05:51
【问题描述】:

我试图用中心点和边界框绘制我的图像。在变量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)

感谢您的帮助

【问题讨论】:

  • 您正在绘制一个黑色矩形,而您可能只是没有看到它。尝试使用更亮的灰色调:cv2.drawContours(img_8bit,[box],0,(200,0,0),2)
  • 不,(0,0,255) 是红色,(255,0,0) 是蓝色
  • 由于您在单通道图像上绘图,因此您应该只考虑第一个通道。所以 (0, ..., ...) 是黑色的,而 (255, ..., ...) 是白色的。 (200,...,...) 是浅灰色。
  • 但是drawContours 独立于我的图片频道。我只用括号中的值缩放边界框线的颜色。检查 docs.opencv.org/3.4/d6/d6e/…。我也尝试过你的方法,但它不起作用。

标签: python opencv bounding-box


【解决方案1】:

您使用的是 3 通道图像,所以不要使用 plt.imshow()

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 2019-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多