在白色背景下跟踪白球(Python/OpenCV) [英] Tracking white ball in white background (Python/OpenCV)

查看:267
本文介绍了在白色背景下跟踪白球(Python/OpenCV)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Python 3中使用OpenCV来检测白色区域上的白色/黑色球,并给出准确的(x,y,半径)和颜色.

i'm using OpenCV in Python 3 to detect a white/black ball on a white field and give it's exact (x,y,radius) and color.

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

I use the function cv2.Canny() and cv2.findContours() to find it but the problem is cv2.Canny() dont's always detect the complete shape of the circle (most of time only 3/4 of the circle). So when i use cv2.findContours() it don't detect it as a 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)

因此,如果您能帮助我找到一个很好的解决方案,那就更好了! 也许有一个功能可以完善圆的形状,以便我可以检测到它?

So if you can help me find a solution that would be great! Maybe is there a function that complete the shape of the circle so i can detect it?

推荐答案

我建议您在使用Canny检测之前应用一些中间颜色过滤.这样可以确保在球图像和场地之间的边界清楚的情况下进行Canny边缘检测.

What I would suggest is that you apply some intermediate colour filtering before using canny detection. This will ensure that the canny edge detection takes place on a well defined border between the ball image and the field.

这是一个使用轨迹栏的python代码,您可以自定义颜色阈值:

Here is a python code that uses trackbars for you to be able to customize the color threshold:

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))

这篇关于在白色背景下跟踪白球(Python/OpenCV)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆