Amazing simple ball tracker based on its color. (Used green color in this project)
All thanks to Adrian Rosebrock (from pyimagesearch) for making great tutorials. This project is inspired from his blog: Ball Tracking with OpenCV. I have included the author's code and the one i wrote my self as well.
- Steps involved:
- Detect the ball in the image
- Track the ball as it moves around in the video. Draw it's previous locations.
- Assumptions:
- There is only one ball of the green color in the video.
- The green ball is the largest green color object in the image.
- There is support for both recorded video ball detection and live ball detection using webcam.
- We can specify the desired length of contrail size of the ball's previous locations.
- HSV color space is used to detect the green ball. Hence we convert the input RGB image to HSV color space.
- Finds the centroid (center of the circular ball) using moments.
- Stores the previous ball location in a queue.
- The script is suitable for real time tracking as it has a very good frame rate (>32 FPS).
- The ball can be partially occluded and our script will still successfully track the ball.
- python (3.7.3)
- opencv (4.1.0)
- numpy (1.61.4)
- imutils (0.5.2)
For detecting green ball in a recorded video
python ball_tracking.py --video ball_tracking_example.mp4
For live detection of green ball via webcam
python ball_tracking.py
The result is great. The ball is successfully detected. Also if the ball looses the frame, we catch it later when it comes in the frame.
- It can't track multiple green balls
- Need to update the script if want to track a ball of different color
- No other large green object should be there, otherwise we won't be able to track the ball successfully.