【问题标题】:OpenCV + python red ball detection and trackingOpenCV + python 红球检测与跟踪
【发布时间】:2017-08-08 00:11:30
【问题描述】:

我正在研究对象检测和跟踪系统,输入是 rgb 网络摄像头流。 我的代码没有问题可以检测,例如黄色、绿色和蓝色的几何物体,比如球,但是当谈到红球时,我正在挑战一个问题。

# converting the input stream into HSV color space
hsv_conv_img = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

# because hue wraps up and to extract as many "red objects" as possible, I define lower and upper boundaries for brighter and for darker red shades
bright_red_lower_bounds = (0, 100, 100)
bright_red_upper_bounds = (10, 255, 255)
bright_red_mask = cv2.inRange(hsv_conv_img, bright_red_lower_bounds, bright_red_upper_bounds)

dark_red_lower_bounds = (160, 100, 100)
dark_red_upper_bounds = (179, 255, 255)
dark_red_mask = cv2.inRange(hsv_conv_img, dark_red_lower_bounds, dark_red_upper_bounds)

# after masking the red shades out, I add the two images 
weighted_mask = cv2.addWeighted(bright_red_mask, 1.0, dark_red_mask, 1.0, 0.0)

# then the result is blurred
blurred_mask = cv2.GaussianBlur(weighted_mask,(9,9),3,3)

# some morphological operations (closing) to remove small blobs 
erode_element = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
dilate_element = cv2.getStructuringElement(cv2.MORPH_RECT, (8, 8))
eroded_mask = cv2.erode(blurred_mask,erode_element)
dilated_mask = cv2.dilate(eroded_mask,dilate_element)

# on the color-masked, blurred and morphed image I apply the cv2.HoughCircles-method to detect circle-shaped objects 
detected_circles = cv2.HoughCircles(dilated_mask, cv2.HOUGH_GRADIENT, 1, 150, param1=100, param2=20, minRadius=20, maxRadius=200)
if detected_circles is not None:
    for circle in detected_circles[0, :]:
        circled_orig = cv2.circle(frame, (circle[0], circle[1]), circle[2], (0,255,0),thickness=3)
    cv2.imshow("original", circled_orig)
else:
    cv2.imshow("original", frame)

问题:通过定义从 HSV 中提取的广泛“红色”,我的手和脸的部分(当站在相机前,拿着球时)也被提取了。 后来,HoughCircles 方法在我手和脸的剩余区域检测到小圆圈。

我玩了一下 cv2.HoughCircles 的(不太容易调整的)参数,例如较小的 param2 值比较大的值检测到更多(错误)圆。

有人知道如何克服这个问题并消除错误检测到的圆圈吗?要求:系统对球的大小一无所知,它应该检测到很多。所以我无法定义最小或最大圆半径来消除误报。

提前非常感谢。 问候, 克里斯

p.s.:这段代码与this one密切相关

【问题讨论】:

  • 提供图片会有所帮助。

标签: python opencv tracking object-detection


【解决方案1】:

由于您脸上和手上的红色区域 - 我希望 - 远不如球均匀,因此请尝试在 HSV 阈值之前进行模糊处理。这应该会柔化您不想检测到的红色区域,并且在球上,颜色应该或多或少保持不变。

编辑: 如果您在评论中提供的示例与实际情况接近,则证明模糊可以解决您的问题。您基本上想要做的是创建多个轨迹栏并同时针对不同类型的模糊进行调整;形态学运算;和 HSV 阈值本身(因为模糊可能会改变阈值的最佳值)。使用生成的检测区域的实时视图进行试验,这应该可以帮助您找出哪些有帮助,哪些没有。

正如我的教授曾经说过的:“如果我们的眼睛能看到,计算机就能看到它”。这显然是这种情况 - 球的颜色与贝克汉姆的脸完全不同。

【讨论】:

  • 因为我不想发布自己的照片,所以我创建了一个示例,它很好地说明了问题所在cruzercloud.feste-ip.net/index.php/s/aq0lQvOfywj82Ao 正如你所看到的,球不是很明显,并且有一个部分“遮蔽了”。脸部的中间部分被错误地识别为一个圆圈。您关于在 HSV 阈值处理之前模糊图片的提示有所改善,但仍然存在错误识别的圆圈。
  • 这个例子向我证明了应该有一个模糊/形态过滤算法来解决这个问题。我会更新我的答案。
  • K,我也会尝试一些变体的模糊和形态学操作。
  • @UllusParvus 如果您找到解决问题的更好方法,请考虑接受答案或自己回答您的问题。
  • 我会这样做,但由于没有时间处理这个问题,我需要几天时间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-22
  • 2021-01-15
  • 2011-10-24
  • 2020-06-03
  • 1970-01-01
  • 2016-04-24
  • 1970-01-01
相关资源
最近更新 更多