[Tutorial OpenCV] “Ball Tracker” using Kalman filter

A few time ago I published on YouTube a video about a “simple” software capable to identify a blue ball moving on a table and to track its movements, estimating its potition even under occlusions. The video, available below, has got great success and has been viewed more than 5000 times. A lot of people requested me to write a tutorial or to get the source code… the source code has been distributed all around the world, now it’s the time to write the tutorial.

 This is the source code:


The software is tuned such to identify a blue ball. You can modify the color simply changing the values of the HUE “MIN_H_BLUE” and “MAX_H_BLUE” at the very beginning of the code.

Even if the code is really short, the theory behind it is really complicated and requires high level mathematical knowledge. The tracking uses what is known in literature as “Kalman Filter“, it is an “asymptotic state estimator”, a mathematical tool that allows to estimate the position of the tracked object using the cinematic model of the object and its “history”. Wikipedia has a good page about Kalman filter, the explaination is really well done, even if it is not really easy to understand it if you do not have enough mathematical capabilities. We can simply say that the Kalman filter works in two steps: PREDICTION and UPDATE. The PREDICTION step allows to predict the position of the object knowing its history, the speed of its movements and knowing the equations that identify its movements. The PREDICTION is corrected every time a measure of the state of the object is available, this correction makes the UPDATE step. Unfortunately, the measure is not perfect, each measure has errors, so PREDICTION and UPDATE are weighted using information about measure and prediction errors. After this attempt to describe the Kalman Filter using simple words, we can move to the description of the code. First of all lets define our system, that is the information about the ball at each instant “t”.

The vector [x,y,v_x,v_y,w,h] (line45) defines the state:

  • x, y: centroid of the ball
  • v_x, v_y: speed of the centroid (pixel/sec)
  • w, h: size of the bounding box

Now we must define the “measure vector” [z_x,z_y,z_w,z_h] (line 46):

  • z_x, z_y: centroid of the identified ball
  • z_w, z_h: size of the identified ball

A note: the “measure vector” is used during the UPDATE phase only if the ball has been detected in the current frame, otherwise (OCCLUSION) we make only the PREDICTION step.

From line 50 to line 88 we initialize the matrices that realize the two phases of the Kalman Filter. A matrix makes the “linear model” of the movements of the ball. For this example has been made the strong assumption that the ball moves in a linear way, which unfortunately is not true for most of the more complicated dynamic systems. H makes the measure. Q matrix is the matrix related to the “Process covariance“, a difficult name to define the noise of the system, that is the errors about the estimation of the state of the ball at each instant.

Now its time to realize the tracking process in the “while” cycle. The variable “dT” takes the measure of the time elapsed from a phase to the next, the time is essential for the state estimation. If the ball has been detected before (found is TRUE) the tracking is active, from line 132 to line 156 we realize the “PRECITION” phase, thanks to the OpenCV function “predict“. From line 158 to line 208 the “classical” detection algorithm is applied: blurring, HSV conversion, morphological filtering, thresholding and dimensional filtering.

From line 231 to line 277 we realize the “UPDATE” of the state of the ball, using the information extracted with the detection, particularly from line 162 to line 266 the initial position and size of the ball are initialized with the first measure (found is FALSE). The variable “found” is used to indicate that the ball has been previously detected and the tracking is active. If we lose the ball for more than 10 consecutive frames “found” is set to FALSE and the tracking process is stopped and reinitialized if we detect the ball again next.

From line 212 to line 229 we show the result of the tracking. The green rectangle identifies the bounding box of the ball detected during the measure step. The red rectangle shows the “estimated state” of the ball, the result of the prediction step of the Kalman Filter. It is really interesting to note how the estimated state overlaps the measured state meanwhile the ball moves linearly, but overall it is interesting to note that the estimation of the state stays valid even if the ball is behind the bottle.

The tutorial is over.

OpenCV has a good powerful mathematical tool, not really easy to be used, such as the Kalman Filter. I hope that this short guide can help you to use it in your “tracking project”. Please feel free to use my email address (developer@myzhar.com) to contact me if you have any doubt.

The code is available also on Github:

One Comment:

  1. Pingback: SORT 多目标跟踪算法笔记 - 算法网

Leave a Reply

Your email address will not be published. Required fields are marked *