【问题标题】:opencv-python: why the incorrect bounding box detected (several bounding boxes)?opencv-python:为什么检测到不正确的边界框(几个边界框)?
【发布时间】:2022-01-07 12:45:29
【问题描述】:

我想找到图片中的粉红色木头。 代码正确地做到了这一点,但在某些图像中,例如下图,它正确地找到了粉色棒,但错误地找到了其他几个框。 事实上,它不是找到一个坐标(x,y,w,h),而是误诊了几个坐标。

下图很清晰

import numpy as np

imagePath = "core4.jpg"

import cv2
from cv2 import *
im = cv2.imread(imagePath)

im = cv2.bilateralFilter(im,9,75,75)
im = cv2.fastNlMeansDenoisingColored(im,None,10,10,7,21)
hsv_img = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)   # HSV image


COLOR_MIN = np.array([130,0,220],np.uint8)    
COLOR_MAX = np.array([170,255,255],np.uint8)  

frame_threshed = cv2.inRange(hsv_img, COLOR_MIN, COLOR_MAX)     # Thresholding image
imgray = frame_threshed
ret,thresh = cv2.threshold(frame_threshed,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
    x,y,w,h = cv2.boundingRect(cnt)
    print(x,y,w,h)
    cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imwrite("core4_cropped.jpg", im)

输入

输出(检测到不正确的几个框)

它没有找到一个坐标 (x,y,w,h),而是误诊了几个坐标边界框。

57 2410 113 148
153 2534 4 6
136 2526 15 13
101 2525 3 3
102 2520 3 7
103 2519 3 3
145 2488 3 5
134 2484 7 9
129 2481 8 12
155 2454 6 7
141 2452 7 9
148 2451 7 9
136 2448 3 3
135 2446 3 4
109 1416 4 3
106 1414 1 1

这个错误的原因可能是什么?

注意:数字写在粉红色的木头上。

【问题讨论】:

  • 扩展您的 H 和 V 范围。然后查看 inRange() 的结果,看看它是否根据需要填充。如果这不能完全帮助,那么使用一些形态来填补任何漏洞。
  • 只使用第一层(最外层)的轮廓。我相信 findContours 的 RETR_EXTERNAL 标志可以做到这一点。

标签: python image opencv image-processing computer-vision


【解决方案1】:

我会降低粉红色的阈值或对 img 应用模糊以破坏所有小细节,如本例中的数字。另外我会推荐这样的东西使用“旋转矩形”。它将显示确切的矩形并旋转它。在您的情况下,您使用的是“直线边界矩形”。您可以通过here 了解更多信息。滚动到 7.b。

【讨论】:

    猜你喜欢
    • 2021-10-20
    • 2013-12-09
    • 2013-01-21
    • 2020-07-22
    • 2017-02-28
    • 2015-07-13
    • 2019-06-18
    • 2018-06-03
    • 2013-04-06
    相关资源
    最近更新 更多