Skip to content
AI360XpertBeta

Kalman Filter Predicts Then Corrects

The Kalman filter tracks by predicting where the object should be from its motion, then splitting the difference between that prediction and the new measurement by trust.

Each frame predicts the box forward from velocity, then corrects the prediction toward the detection by the Kalman gain
Each frame predicts the box forward from velocity, then corrects the prediction toward the detection by the Kalman gain

Why Does This Exist?

Detections are noisy and intermittent. A detector jitters a box by a few pixels each frame, misses frames entirely during occlusion, and reports no velocity to aim with. Naive tracking that copies the last detection inherits all of that: jittery boxes and lost IDs behind every pillar.

The Kalman filter from 1960 is the optimal estimator for linear motion with Gaussian noise, and the workhorse motion model inside classical object tracking pipelines such as SORT, extended by multi-object tracking association and covered generally in object tracking. It keeps a running belief of position plus velocity with an uncertainty, predicts it forward each frame, and corrects it with each measurement weighted by relative trust. During occlusion there is no measurement, so it coasts on prediction until the object reappears.

Think of It Like This

Catching a ball with your eyes half closed

You watch a ball arc toward you, blink, and your brain keeps simulating the arc during the blink. When your eyes open, the ball sits slightly off from where you imagined, and you instantly revise. The longer the blink, the less you trust the simulation; the blurrier your vision, the less you trust your eyes.

Prediction is the simulation during the blink and the measurement is the bleary glimpse. Where it stops: your brain also replays spin, wind, and bounce, while the plain filter believes only straight-line motion with bell-curve noise, so sharp maneuvers need extra process noise or a richer model.

How It Actually Works

The state holds position and velocity, x=[p,v]Tx = [p, v]^T. Each step predicts forward with constant velocity, p←p+vΔtp \leftarrow p + v \Delta t with vv unchanged, while uncertainty PP grows by process noise QQ: coasting means getting less sure. A new measurement zz with noise RR arrives, and the gain K=P/(P+R)K = P / (P + R) splits the difference: state ←\leftarrow prediction +K(z−prediction)+ K (z - \text{prediction}), uncertainty ←(1−K)P\leftarrow (1 - K) P. High KK chases measurements, low KK rides the model. With no detection the update is skipped, prediction repeats, and uncertainty compounds so the matching gate widens honestly.

Worked example

Predicted position 1212, measurement 12.512.5, gain 0.50.5. The residual is 12.5−12=0.512.5 - 12 = 0.5. The update is 12+0.5×0.5=12.2512 + 0.5 \times 0.5 = 12.25, exactly halfway because both sides were trusted equally. At gain 0.90.9 the same pair gives 12+0.9×0.5=12.4512 + 0.9 \times 0.5 = 12.45, chasing the sensor. Uncertainty after the 0.50.5-gain update is (1−0.5)P=0.5P(1 - 0.5) P = 0.5 P: halved by one trusted look. Coast two frames with no detection and PP grows twice by QQ with no halving, which is why long occlusions return wide gates.

Code

The point of this snippet is the update rule, not a tracker: pure standard-library code recomputes the worked example at both gains.

def kalman_update(pred: float, measure: float, gain: float) -> float:    return pred + gain * (measure - pred)
print(round(kalman_update(12.0, 12.5, 0.5), 2))# -> 12.25print(round(kalman_update(12.0, 12.5, 0.9), 2))# -> 12.45

Gain 0.50.5 lands exactly halfway at 12.2512.25; gain 0.90.9 chases the measurement to 12.4512.45. Real trackers compute the gain from PP and RR each frame instead of fixing it.

Watch Out For

Constant velocity lies during maneuvers

The model assumes velocity barely changes, so a sharp turn or sudden stop leaves predictions flying straight while the object is gone. The filter recovers once measurements return, but IDs may already have switched. If tracks swap at every turn, raise process noise QQ or move to an interacting-multiple-model setup.

Tuning Q and R by feel

QQ for motion surprise and RR for sensor slop decide everything, and hand-tuned values from one camera fail on another. The symptom is a tracker that is perfect on one video and lost on the next. Measure RR from the detector's real jitter on static scenes, then set QQ from the fastest accelerations that must be followed.

The Quick Version

  • The Kalman filter predicts position from velocity, then corrects with the measurement weighted by the gain.
  • Prediction 1212 with measurement 12.512.5 at gain 0.50.5 lands exactly at 12.2512.25.
  • Uncertainty halves on a trusted update and compounds while coasting, so occlusion gates widen honestly.
  • Optimal only for linear motion with Gaussian noise; maneuvers need more process noise.
  • The motion core of SORT-style trackers; nonlinear cases graduate to particle filters.