Skip to content Skip to sidebar Skip to footer

Tracking White Ball In White Background (python/opencv)

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. I use the function cv2.Canny() and cv2.findContours() to fin

Solution 1:

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.

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

Solution 2:

You can use cv.HoughCircles to estimate the best matching complete circle. The good thing with ball tracking is that a ball will always appear as a circle in the image, irrespective of the camera angle.

Post a Comment for "Tracking White Ball In White Background (python/opencv)"