# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

# vision.KalmanFilter class

Package: vision

Kalman filter for object tracking

## Description

The Kalman filter object is designed for tracking. You can use it to predict a physical object's future location, to reduce noise in the detected location, or to help associate multiple physical objects with their corresponding tracks. A Kalman filter object can be configured for each physical object for multiple object tracking. To use the Kalman filter, the object must be moving at constant velocity or constant acceleration.

The Kalman filter algorithm involves two steps, prediction and correction (also known as the update step). The first step uses previous states to predict the current state. The second step uses the current measurement, such as object location, to correct the state. The Kalman filter implements a discrete time, linear State-Space System.

### Note

To make configuring a Kalman filter easier, you can use the `configureKalmanFilter` object to configure a Kalman filter. It sets up the filter for tracking a physical object in a Cartesian coordinate system, moving with constant velocity or constant acceleration. The statistics are the same along all dimensions. If you need to configure a Kalman filter with different assumptions, do not use the function, use this object directly.

## Construction

`obj = vision.KalmanFilter` returns a Kalman filter object for a discrete time, constant velocity system. In this State-Space System, the state transition model, A, and the measurement model, H, are set as follows:

VariableValue
A`[1 1 0 0; 0 1 0 0; 0 0 1 1; 0 0 0 1]`
H`[1 0 0 0; 0 0 1 0]`

`obj = vision.KalmanFilter(StateTransitionModel,MeasurementModel)` configures the state transition model, A, and the measurement model, H.

`obj = vision.KalmanFilter(StateTransitionModel,MeasurementModel,ControlModel)` additionally configures the control model, B.

`obj = vision.KalmanFilter(StateTransitionModel,MeasurementModel,ControlModel,Name,Value)` configures the Kalman filter object properties, specified as one or more `Name,Value` pair arguments. Unspecified properties have default values.

### To Track Objects:

Use the and methods based on detection results.

• When the tracked object is detected, use the `predict` and `correct` methods with the Kalman filter object and the detection measurement. Call the methods in the following order:

```[...] = predict(`obj`); [...] = correct(`obj`,measurement);```

• When the tracked object is not detected, call the `predict` method, but not the `correct` method. When the tracked object is missing or occluded, no measurement is available. Set the methods up with the following logic:

```[...] = predict(`obj`); If measurement exists [...] = correct(`obj`,measurement); end```

• If the tracked object becomes available after missing for the past t-1 contiguous time steps, you can call the `predict` method t times. This syntax is particularly useful to process asynchronous video.. For example,

```for i = 1:k [...] = predict(obj); end [...] = correct(obj,measurement) ```

Use the `distance` method to find the best matches. The computed distance values describe how a set of measurements matches the Kalman filter. You can thus select a measurement that best fits the filter. This strategy can be used for matching object detections against object tracks in a multiobject tracking problem. This distance computation takes into account the covariance of the predicted state and the process noise. The `distance` method can only be called after the `predict` method.

`d = distance(obj, z_matrix)` computes a distance between the location of a detected object and the predicted location by the Kalman filter object. Each row of the N-column `z_matrix` input matrix contains a measurement vector. The `distance` method returns a row vector where each distance element corresponds to the measurement input.

## State-Space System

This object implements a discrete time, linear state-space system, described by the following equations.

 State equation: `$x\left(k\right)=Ax\left(k-1\right)+Bu\left(k-1\right)+w\left(k-1\right)$` Measurement equation: `$z\left(k\right)=Hx\left(k\right)+v\left(k\right)$`

## Properties

 `StateTransitionModel` Model describing state transition between time steps (A) Specify the transition of state between times as an M-by-M matrix. After the object is constructed, this property cannot be changed. This property relates to the A variable in the State-Space System. Default: `[1 1 0 0; 0 1 0 0; 0 0 1 1; 0 0 0 1]` `MeasurementModel` Model describing state to measurement transformation (H) Specify the transition from state to measurement as an N-by-M matrix. After the object is constructed, this property cannot be changed. This property relates to the H variable in the State-Space System. Default: `[1 0 0 0; 0 0 1 0]` `ControlModel` Model describing control input to state transformation (B) Specify the transition from control input to state as an M-by-L matrix. After the object is constructed, this property cannot be changed. This property relates to the B variable in the State-Space System. Default: [] `State` State (x) Specify the state as a scalar or an M-element vector. If you specify it as a scalar it will be extended to an M-element vector. This property relates to the x variable in the State-Space System. Default: [`0`] `StateCovariance` State estimation error covariance (P) Specify the covariance of the state estimation error as a scalar or an M-by-M matrix. If you specify it as a scalar it will be extended to an M-by-M diagonal matrix. This property relates to the P variable in the State-Space System. Default: [`1`] `ProcessNoise` Process noise covariance (Q) Specify the covariance of process noise as a scalar or an M-by-M matrix. If you specify it as a scalar it will be extended to an M-by-M diagonal matrix. This property relates to the Q variable in the State-Space System. Default: [`1`] `MeasurementNoise` Measurement noise covariance (R) Specify the covariance of measurement noise as a scalar or an N-by-N matrix. If you specify it as a scalar it will be extended to an N-by-N diagonal matrix. This property relates to the R variable in the State-Space System. Default: [`1`]

## Methods

 correct Correction of measurement, state, and state estimation error covariance distance Confidence value of measurement predict Prediction of measurement
Common to All System Objects
`clone`

Create System object™ with same property values

`getNumInputs`

Expected number of inputs to a System object

`getNumOutputs`

Expected number of outputs of a System object

`isLocked`

Check locked states of a System object (logical)

`release`

Allow System object property value changes

## Examples

expand all

Track the location of a physical object moving in one direction.

Generate synthetic data which mimics the 1-D location of a physical object moving at a constant speed.

`detectedLocations = num2cell(2*randn(1,40) + (1:40));`

Simulate missing detections by setting some elements to empty.

```detectedLocations{1} = []; for idx = 16: 25 detectedLocations{idx} = []; end```

Create a figure to show the location of detections and the results of using the Kalman filter for tracking.

```figure; hold on; ylabel('Location'); ylim([0,50]); xlabel('Time'); xlim([0,length(detectedLocations)]);```

Create a 1-D, constant speed Kalman filter when the physical object is first detected. Predict the location of the object based on previous states. If the object is detected at the current time step, use its location to correct the states.

```kalman = []; for idx = 1: length(detectedLocations) location = detectedLocations{idx}; if isempty(kalman) if ~isempty(location) stateModel = [1 1;0 1]; measurementModel = [1 0]; kalman = vision.KalmanFilter(stateModel,measurementModel,'ProcessNoise',1e-4,'MeasurementNoise',4); kalman.State = [location, 0]; end else trackedLocation = predict(kalman); if ~isempty(location) plot(idx, location,'k+'); d = distance(kalman,location); title(sprintf('Distance:%f', d)); trackedLocation = correct(kalman,location); else title('Missing detection'); end pause(0.2); plot(idx,trackedLocation,'ro'); end end legend('Detected locations','Predicted/corrected locations');```

Use Kalman filter to remove noise from a random signal corrupted by a zero-mean Gaussian noise.

Synthesize a random signal that has value of 1 and is corrupted by a zero-mean Gaussian noise with standard deviation of 0.1.

```x = 1; len = 100; z = x + 0.1 * randn(1,len);```

Remove noise from the signal by using a Kalman filter. The state is expected to be constant, and the measurement is the same as state.

```stateTransitionModel = 1; measurementModel = 1; obj = vision.KalmanFilter(stateTransitionModel,measurementModel,'StateCovariance',1,'ProcessNoise',1e-5,'MeasurementNoise',1e-2); z_corr = zeros(1,len); for idx = 1: len predict(obj); z_corr(idx) = correct(obj,z(idx)); end```

Plot results.

```figure, plot(x * ones(1,len),'g-'); hold on; plot(1:len,z,'b+',1:len,z_corr,'r-'); legend('Original signal','Noisy signal','Filtered signal');```

## References

Welch, Greg, and Gary Bishop, An Introduction to the Kalman Filter, TR 95–041. University of North Carolina at Chapel Hill, Department of Computer Science.