This project uses stereo vision with two cameras to track a tennis ball on a 3D court.
Authors: ANTALUCA Robert, ANTALUCA Ioana
- Ball Detection: The ball is detected and its coordinates are extracted in the two frames via color filtering.
- 3D Coordinates Calculation: Using photogrammetry formulas and the coordinates from both the left and right image frames, the 3D coordinates of the ball within each camera's reference frame are calculated.
- Common Reference System Transformation: The 3D coordinates are transformed into a common reference system to visualize the ball's trajectory on the 3D court.
The footage comes from a tennis simulation developed in Unity, designed to capture the scene simultaneously from two different camera perspectives.
The algorithm to compute the 3D position from the stereo images uses the following photogrammetry formulas:
Given:
-
$e$ : distance between the two cameras -
$f $ : focal length -
$x_R $ : x-coordinate in the right camera image -
$x_L $ : x-coordinate in the left camera image -
$y_R $ : y-coordinate in the right camera image -
$y_L $ : y-coordinate in the left camera image
The disparity
The 3D coordinates of the ball in one of the camera's reference frame are calculated as:
$x = \frac{x_R \cdot e}{d}$ $y = \frac{y_R \cdot e}{d}$ $z = \frac{e \cdot f}{d}$
The transformation matrix
The transformed coordinates
where (
left_short.mp4
In your terminal, run:
pip3 install opencv-python
pip3 install numpy
pip3 install matplotlib
git clone https://github.com/rantaluca/ball_tracking.git
cd ball_tracking
- Place your left camera video in the `videos` directory and name it `left_short.mp4`.
- Place your right camera video in the `videos` directory and name it `right_short.mp4`.
Note: The transformation matrix T will need to be redefined depending on the positions of the cameras relative to the tennis court in your scene. Check this link to learn about the transformation matrix.
python ball_tracking_clean.py
- The processed videos and images will be saved in the `videos` and the current directory respectively.
- The 3D trajectory plot will be displayed and saved as `Trajectoire_3D_repere.png`.
Note: A version with a one-line command to define the two source videos and only display the result is coming soon.