【问题标题】:Tracking white ball in white background (Python/OpenCV)在白色背景中跟踪白球(Python/OpenCV)
【发布时间】:2017-11-22 10:09:30
【问题描述】:

我在 Python 3 中使用 OpenCV 来检测白场上的白/黑球并给出准确的(x、y、半径)和颜色。

我使用函数 cv2.Canny() 和 cv2.findContours() 来找到它,但问题是 cv2.Canny() 并不总是检测到圆的完整形状(大多数时候只有圆的 3/4 )。 因此,当我使用 cv2.findContours() 时,它不会将其检测为 Contour。

请看:

这是最重要的代码:

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 10, 40) # 10 and 40 to be more perceptive
contours_canny= cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2]

之后我使用: approx = cv2.approxPolyDP(contour,0.01*cv2.arcLength(contour,True),True)

所以如果你能帮我找到一个很棒的解决方案! 也许有一个功能可以完成圆形的形状以便我可以检测到它?

【问题讨论】:

  • 你试过霍夫圆吗?

标签: python c++ opencv shape trackball


【解决方案1】:

您可以使用 cv.HoughCircles 来估计最佳匹配的完整圆。球跟踪的好处是,无论摄像机角度如何,球在图像中始终显示为圆形。

【讨论】:

    【解决方案2】:

    我建议您在使用精明检测之前应用一些中间颜色过滤。这将确保精确边缘检测发生在球图像和场地之间定义明确的边界上。

    这是一个 python 代码,它使用轨迹栏让您能够自定义颜色阈值:

    cv2.namedWindow('temp')
    cv2.createTrackbar('bl', 'temp', 0, 255, nothing)
    cv2.createTrackbar('gl', 'temp', 0, 255, nothing)
    cv2.createTrackbar('rl', 'temp', 0, 255, nothing)
    cv2.createTrackbar('bh', 'temp', 255, 255, nothing)
    cv2.createTrackbar('gh', 'temp', 255, 255, nothing)
    cv2.createTrackbar('rh', 'temp', 255, 255, nothing)
    
    bl_temp=cv2.getTrackbarPos('bl', 'temp')
    gl_temp=cv2.getTrackbarPos('gl', 'temp')
    rl_temp=cv2.getTrackbarPos('rl', 'temp')
    
    bh_temp=cv2.getTrackbarPos('bh', 'temp')
    gh_temp=cv2.getTrackbarPos('gh', 'temp')
    rh_temp=cv2.getTrackbarPos('rh', 'temp')
    thresh=cv2.inRange(frame,(bl_temp,gl_temp,rl_temp),(bh_temp,gh_temp,rh_temp))
    

    【讨论】:

    • 非常感谢!现在更容易了。
    猜你喜欢
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多