/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// Intel License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000, Intel Corporation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of Intel Corporation may not be used to endorse or promote products26// derived from this software without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall the Intel Corporation or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39//M*/40#include "precomp.hpp"4142namespace cv43{4445KalmanFilter::KalmanFilter() {}46KalmanFilter::KalmanFilter(int dynamParams, int measureParams, int controlParams, int type)47{48init(dynamParams, measureParams, controlParams, type);49}5051void KalmanFilter::init(int DP, int MP, int CP, int type)52{53CV_Assert( DP > 0 && MP > 0 );54CV_Assert( type == CV_32F || type == CV_64F );55CP = std::max(CP, 0);5657statePre = Mat::zeros(DP, 1, type);58statePost = Mat::zeros(DP, 1, type);59transitionMatrix = Mat::eye(DP, DP, type);6061processNoiseCov = Mat::eye(DP, DP, type);62measurementMatrix = Mat::zeros(MP, DP, type);63measurementNoiseCov = Mat::eye(MP, MP, type);6465errorCovPre = Mat::zeros(DP, DP, type);66errorCovPost = Mat::zeros(DP, DP, type);67gain = Mat::zeros(DP, MP, type);6869if( CP > 0 )70controlMatrix = Mat::zeros(DP, CP, type);71else72controlMatrix.release();7374temp1.create(DP, DP, type);75temp2.create(MP, DP, type);76temp3.create(MP, MP, type);77temp4.create(MP, DP, type);78temp5.create(MP, 1, type);79}8081const Mat& KalmanFilter::predict(const Mat& control)82{83CV_INSTRUMENT_REGION();8485// update the state: x'(k) = A*x(k)86statePre = transitionMatrix*statePost;8788if( !control.empty() )89// x'(k) = x'(k) + B*u(k)90statePre += controlMatrix*control;9192// update error covariance matrices: temp1 = A*P(k)93temp1 = transitionMatrix*errorCovPost;9495// P'(k) = temp1*At + Q96gemm(temp1, transitionMatrix, 1, processNoiseCov, 1, errorCovPre, GEMM_2_T);9798// handle the case when there will be measurement before the next predict.99statePre.copyTo(statePost);100errorCovPre.copyTo(errorCovPost);101102return statePre;103}104105const Mat& KalmanFilter::correct(const Mat& measurement)106{107CV_INSTRUMENT_REGION();108109// temp2 = H*P'(k)110temp2 = measurementMatrix * errorCovPre;111112// temp3 = temp2*Ht + R113gemm(temp2, measurementMatrix, 1, measurementNoiseCov, 1, temp3, GEMM_2_T);114115// temp4 = inv(temp3)*temp2 = Kt(k)116solve(temp3, temp2, temp4, DECOMP_SVD);117118// K(k)119gain = temp4.t();120121// temp5 = z(k) - H*x'(k)122temp5 = measurement - measurementMatrix*statePre;123124// x(k) = x'(k) + K(k)*temp5125statePost = statePre + gain*temp5;126127// P(k) = P'(k) - K(k)*temp2128errorCovPost = errorCovPre - gain*temp2;129130return statePost;131}132133}134135136