Kalman filter
lastmod 2025-02-11created 2017-10-2511 edits
Uses an object's previous state to predict its next state.
The Kalman filter is a recursive filter, developed by Rudolf Kálmán, that tracks the state of a linear dynamical system containing noise. The Kalman filter is used in many fields such as computer vision, robotics, and radar, and in many cases it is a very efficient
The algorithm is based on measurements taken over time. It can be expected to give more accurate results than using only the measurements taken at a single moment. As a filter that recursively processes input data including noise, it can make an optimal statistical prediction of the current state.
The whole algorithm can be divided into two parts: prediction and update. Prediction means predicting the current state, and update means making a more accurate prediction using values that also include the measurements observed in the current state.
The extended Kalman filter works on nonlinear systems.
Steps
- Measurement (init) - measure the object's position, velocity, acceleration, etc.
- Prediction (predict) - estimate the current value from previous values (recursive)
- Correction (correct, update) - recalculate based on the predicted state and the actual state
Types
- Linear Kalman filter (LKF, Linear Kalman Filter)
- Real environments are nonlinear and the noise is non-Gaussian -> poor fit
- Extended Kalman filter (EKF, Extended Kalman Filter)
- Keeps updating the reference point for linearization
- Applicable to moderately nonlinear models; almost the standard in navigation
- Unscented Kalman filter (UKF, The unscented Kalman filter)
- Used for highly nonlinear cases
- Computationally heavy, slow
Example
The Kalman filter uses an object's previous state to predict its next state. The algorithm uses a linear stochastic difference equation to determine the next state.
import cv2
import numpy as np
measured=[]
predicted=[]
dr_frame = np.zeros((400,400,3), np.uint8)
mp = np.array((2,1), np.float32)
tp = np.zeros((2,1), np.float32)
def on_mouse(k,x,y,s,p):
global mp,measured
mp = np.array([[np.float32(x)],[np.float32(y)]])
measured.append((x,y))
def paint_canvas():
global dr_frame,measured,predicted
for i in range(len(measured)-1): cv2.line(dr_frame,measured[i],measured[i+1],(0,100,0))
for i in range(len(predicted)-1): cv2.line(dr_frame,predicted[i],predicted[i+1],(0,0,200))
def reset_canvas():
global measured,predicted,dr_frame
measured=[]
predicted=[]
dr_frame = np.zeros((400,400,3), np.uint8)
cv2.namedWindow("Sample")
cv2.setMouseCallback("Sample",on_mouse);
kalman_fil = cv2.KalmanFilter(4,2)
kalman_fil.measurementMatrix = np.array([[1,0,0,0],[0,1,0,0]],np.float32)
kalman_fil.transitionMatrix = np.array([[1,0,1,0],[0,1,0,1],[0,0,1,0],[0,0,0,1]],np.float32)
kalman_fil.processNoiseCov = np.array([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]],np.float32) * 0.03
while True:
kalman_fil.correct(mp)
tp = kalman_fil.predict()
predicted.append((int(tp[0]),int(tp[1])))
paint_canvas()
cv2.imshow("Output",dr_frame)
k = cv2.waitKey(30) &0xFF
if k == 27: break
if k == 32: reset_canvas()
See also
Favorite site
- Wikipedia (en) Kalman filter
- Understanding of the Kalman Filter 1
- Understanding the Basis of the Kalman Filter Via a Simple and Intuitive Derivation 2
- Kalman Filter explained
- Kalman filter
- Naver blog Money Money - Kalman filter
OpenCV Example
- OpenCV Kalman filter C++ mouse tracking (source code) Kalman Filter
- Github - opencv/samples/cpp/kalman.cpp
- Simple Kalman filter for tracking using OpenCV 2.2