Main Content

vision.KalmanFilter

Correction of measurement, state, and state estimation error covariance

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.

Creation

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 theconfigureKalmanFilterobject 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.

In the state space system, the state transition model,A, and the measurement model,H, are set as follows:

Variable Value
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]

Description

example

kalmanFilter= Vision.KalmanFilterreturns a kalman filter for a discrete time, constant velocity system.

kalmanFilter= Vision.KalmanFilter(StateTransitionModel,MeasurementModel)additionally configures the control model,B.

kalmanFilter= Vision.KalmanFilter(StateTransitionModel,MeasurementModel,ControlModel,Name,Value)配置Kalman过滤对象属性,指定为一个或多个Name,Valuepair arguments. Unspecified properties have default values.

Properties

expand all

Model describing state transition between time steps (A), specified as anM-by-Mmatrix. After the object is constructed, this property cannot be changed. This property relates to theAvariable in the state-space model.

Model describing state to measurement transformation (H) , specified as anN-by-Mmatrix. After the object is constructed, this property cannot be changed. This property relates to theHvariable in the state-space model.

Model describing control input to state transformation (B) , specified as anM-by-Lmatrix. After the object is constructed, this property cannot be changed. This property relates to theBvariable in thestate-space model.

State (x), specified as a scalar or anM-element vector. If you specifyStateas a scalar, it will be extended to anM-element vector. This property relates to thexvariable in the state-space model.

State estimation error covariance (P), specified as a scalar or anM-by-Mmatrix. If you specifyStateCovarianceas a scalar it will be extended to anM-by-Mdiagonal matrix. This property relates to thePvariable in the state-space system.

Process noise covariance (Q) , specified as a scalar or anM-by-Mmatrix. If you specifyProcessNoiseas a scalar it will be extended to anM-by-Mdiagonal matrix. This property relates to theQvariable in the state-space model.

Measurement noise covariance (R) , specified as a scalar or anN-by-Nmatrix. If you specifyMeasurementNoiseas a scalar it will be extended to anN-by-Ndiagonal matrix. This property relates to theRvariable in the state-space model.

Object Functions

Use thepredictandcorrectfunctions based on detection results. Use thedistancefunction to find the best matches.

  • 检测到跟踪对象时,请使用predictandcorrectfunctions with the Kalman filter object and the detection measurement. Call the functions in the following order:

    [...] = predict(kalmanFilter);[...] =正确(kalmanFilter,measurement);

  • When the tracked object is not detected, call thepredictfunction, but not thecorrectfunction. When the tracked object is missing or occluded, no measurement is available. Set the functions up with the following logic:

    [...] = predict(kalmanFilter);Ifmeasurementexists [...] = correct(kalmanFilter,measurement);end

  • If the tracked object becomes available after missing for the pastt-1 contiguous time steps, you can call thepredictfunctionttimes. This syntax is particularly useful to process asynchronous video.. For example,

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

correct Correction of measurement, state, and state estimation error covariance
predict Prediction of measurement
distance Confidence value of measurement

Examples

collapse all

跟踪物理对象向一个方向移动的位置。

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} = [];foridx = 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; holdon;ylabel ('地点');ylim([0,50]); xlabel('Time');xlim([0,长度(检测到)]);

Figure contains an axes. The axes is empty.

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 = [];foridx = 1: length(detectedLocations) location = detectedLocations{idx};ifisempty(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];endelsetrackedLocation = predict(kalman);if~isempty(location) plot(idx, location,'k+');d = distance(kalman,location); title(sprintf('Distance:%f', d)); trackedLocation = correct(kalman,location);elsetitle('Missing detection');endpause(0.2); plot(idx,trackedLocation,'ro');endendlegend('Detected locations','Predicted/corrected locations');

Figure contains an axes. The axes with title Distance:3.126145 contains 66 objects of type line. These objects represent 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);foridx = 1: len predict(obj); z_corr(idx) = correct(obj,z(idx));end

情节结果。

图,图(x *一个(1,len),'g-');holdon;plot(1:len,z,'b+',1:len,z_corr,'r-');legend('Original signal','Noisy signal','Filtered signal');

Figure contains an axes. The axes contains 3 objects of type line. These objects represent Original signal, Noisy signal, Filtered signal.

Algorithms

expand all

参考

[1] 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.

[2] Blackman, S.带有雷达应用的多目标跟踪。Artech House, Inc., pp. 93, 1986.

Extended Capabilities

Introduced in R2012b