【发布时间】: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