Sub topic 6
Predicting Motion with a Kalman Filter
Assume you know Python, NumPy, bounding boxes, vectors, matrices, and basic Gaussian probability. This chapter uses a constant-velocity Kalman filter to predict an object's position between video frames.
Why motion prediction matters
A detector may miss an object for a frame, return a slightly shifted box, or produce several nearby detections. A tracker needs an estimate of where each existing object should appear before it matches detections to tracks.
A Kalman filter combines two sources of information:
- A motion model, which predicts the next position.
- A measurement, such as the center of a detected bounding box.
The filter also tracks uncertainty. A prediction becomes less certain when several frames pass without a measurement.
The working model
For a two-dimensional object center, use this state vector:
x = [cx, cy, vx, vy]Here, cx and cy are the center coordinates, while vx and vy are the velocities in pixels per frame.
With a time step dt, the constant-velocity model predicts:
cx_next = cx + vx * dt
cy_next = cy + vy * dt
vx_next = vx
vy_next = vyIn matrix form:
x_next = F xThe matrix F contains the motion rules. The filter also stores a covariance matrix P, which represents uncertainty in the state. Process noise Q allows the real object to accelerate or move unpredictably.
A detector supplies a measurement:
z = [cx_detected, cy_detected]The measurement matrix H selects the position components from the full state. Measurement noise R describes how unreliable the detector is.
Predict and correct
Each frame follows two phases:
- Predict: project the previous state into the current frame.
- Correct: compare the prediction with the detection and adjust the state.
The difference between the detection and the prediction is the innovation, or residual. The Kalman gain determines how strongly the filter responds to that residual.
- High measurement uncertainty makes the filter trust its motion prediction.
- High prediction uncertainty makes the filter trust the new detection.
A NumPy implementation
This filter estimates only the center of a box. A production tracker can add width, height, scale, or aspect ratio to the state.
import numpy as np
class ConstantVelocityKalman:
def __init__(self, cx, cy, dt=1.0):
self.x = np.array([cx, cy, 0.0, 0.0], dtype=float)
self.P = np.diag([10.0, 10.0, 100.0, 100.0])
self.F = np.array([
[1, 0, dt, 0],
[0, 1, 0, dt],
[0, 0, 1, 0],
[0, 0, 0, 1],
], dtype=float)
self.H = np.array([
[1, 0, 0, 0],
[0, 1, 0, 0],
], dtype=float)
self.Q = np.eye(4) * 0.01
self.R = np.eye(2) * 1.0
def predict(self):
self.x = self.F @ self.x
self.P = self.F @ self.P @ self.F.T + self.Q
return self.x[:2].copy()
def update(self, measurement):
z = np.asarray(measurement, dtype=float)
residual = z - self.H @ self.x
innovation_covariance = (
self.H @ self.P @ self.H.T + self.R
)
gain = (
self.P @ self.H.T
@ np.linalg.inv(innovation_covariance)
)
self.x = self.x + gain @ residual
self.P = (np.eye(4) - gain @ self.H) @ self.P
kalman = ConstantVelocityKalman(100, 50)
for detection in [(103, 50), (106, 51), (109, 51)]:
predicted = kalman.predict()
kalman.update(detection)
print("prediction:", predicted, "state:", kalman.x)
print("next position:", kalman.predict())The velocity estimate starts uncertain, then becomes positive in the horizontal direction as the measurements move right. If a detection is missing, call predict() without update(). The track can continue temporarily, but its uncertainty grows.
> callout note > A Kalman filter does not identify objects. It predicts motion. Data association still decides which detection belongs to which track.
Choosing the noise parameters
Q controls how much the object may depart from constant velocity. Increase it for abrupt motion. R controls detector trust. Increase it when boxes are noisy or unstable. These values usually require validation on representative video.
Exercise
Why should a tracker call predict() even when no detection is available, and what should happen to uncertainty during that gap?
Show answer
Prediction keeps the track's estimated position moving according to its motion model. Its covariance should increase because no measurement corrected the estimate.