Path: blob/master/modules/video/test/test_kalman.cpp
16339 views
/*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*/4041#include "test_precomp.hpp"42#include "opencv2/video/tracking_c.h"4344namespace opencv_test { namespace {4546class CV_KalmanTest : public cvtest::BaseTest47{48public:49CV_KalmanTest();50protected:51void run(int);52};535455CV_KalmanTest::CV_KalmanTest()56{57}5859void CV_KalmanTest::run( int )60{61int code = cvtest::TS::OK;62const int Dim = 7;63const int Steps = 100;64const double max_init = 1;65const double max_noise = 0.1;6667const double EPSILON = 1.000;68RNG& rng = ts->get_rng();69CvKalman* Kalm;70int i, j;7172CvMat* Sample = cvCreateMat(Dim,1,CV_32F);73CvMat* Temp = cvCreateMat(Dim,1,CV_32F);7475Kalm = cvCreateKalman(Dim, Dim);76CvMat Dyn = cvMat(Dim,Dim,CV_32F,Kalm->DynamMatr);77CvMat Mes = cvMat(Dim,Dim,CV_32F,Kalm->MeasurementMatr);78CvMat PNC = cvMat(Dim,Dim,CV_32F,Kalm->PNCovariance);79CvMat MNC = cvMat(Dim,Dim,CV_32F,Kalm->MNCovariance);80CvMat PriErr = cvMat(Dim,Dim,CV_32F,Kalm->PriorErrorCovariance);81CvMat PostErr = cvMat(Dim,Dim,CV_32F,Kalm->PosterErrorCovariance);82CvMat PriState = cvMat(Dim,1,CV_32F,Kalm->PriorState);83CvMat PostState = cvMat(Dim,1,CV_32F,Kalm->PosterState);84cvSetIdentity(&PNC);85cvSetIdentity(&PriErr);86cvSetIdentity(&PostErr);87cvSetZero(&MNC);88cvSetZero(&PriState);89cvSetZero(&PostState);90cvSetIdentity(&Mes);91cvSetIdentity(&Dyn);92Mat _Sample = cvarrToMat(Sample);93cvtest::randUni(rng, _Sample, cvScalarAll(-max_init), cvScalarAll(max_init));94cvKalmanCorrect(Kalm, Sample);95for(i = 0; i<Steps; i++)96{97cvKalmanPredict(Kalm);98for(j = 0; j<Dim; j++)99{100float t = 0;101for(int k=0; k<Dim; k++)102{103t += Dyn.data.fl[j*Dim+k]*Sample->data.fl[k];104}105Temp->data.fl[j]= (float)(t+(cvtest::randReal(rng)*2-1)*max_noise);106}107cvCopy( Temp, Sample );108cvKalmanCorrect(Kalm,Temp);109}110111Mat _state_post = cvarrToMat(Kalm->state_post);112code = cvtest::cmpEps2( ts, _Sample, _state_post, EPSILON, false, "The final estimated state" );113114cvReleaseMat(&Sample);115cvReleaseMat(&Temp);116cvReleaseKalman(&Kalm);117118if( code < 0 )119ts->set_failed_test_info( code );120}121122TEST(Video_Kalman, accuracy) { CV_KalmanTest test; test.safe_run(); }123124}} // namespace125/* End of file. */126127128