test_ukf.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2015, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "test_precomp.hpp"
  42. #include "opencv2/tracking/kalman_filters.hpp"
  43. namespace opencv_test { namespace {
  44. using namespace cv::detail;
  45. // In this two tests Unscented Kalman Filter are applied to the dynamic system from example "The reentry problem" from
  46. // "A New Extension of the Kalman Filter to Nonlinear Systems" by Simon J. Julier and Jeffrey K. Uhlmann.
  47. class BallisticModel: public UkfSystemModel
  48. {
  49. static const double step;
  50. Mat diff_eq(const Mat& x)
  51. {
  52. double x1 = x.at<double>(0, 0);
  53. double x2 = x.at<double>(1, 0);
  54. double x3 = x.at<double>(2, 0);
  55. double x4 = x.at<double>(3, 0);
  56. double x5 = x.at<double>(4, 0);
  57. const double h0 = 9.3;
  58. const double beta0 = 0.59783;
  59. const double Gm = 3.9860044 * 1e5;
  60. const double r_e = 6374;
  61. const double r = sqrt( x1*x1 + x2*x2 );
  62. const double v = sqrt( x3*x3 + x4*x4 );
  63. const double d = - beta0 * exp( ( r_e - r )/h0 ) * exp( x5 ) * v;
  64. const double g = - Gm / (r*r*r);
  65. Mat fx = x.clone();
  66. fx.at<double>(0, 0) = x3;
  67. fx.at<double>(1, 0) = x4;
  68. fx.at<double>(2, 0) = d * x3 + g * x1;
  69. fx.at<double>(3, 0) = d * x4 + g * x2;
  70. fx.at<double>(4, 0) = 0.0;
  71. return fx;
  72. }
  73. public:
  74. void stateConversionFunction(const Mat& x_k, const Mat& u_k, const Mat& v_k, Mat& x_kplus1)
  75. {
  76. Mat v = sqrt(step) * v_k.clone();
  77. v.at<double>(0, 0) = 0.0;
  78. v.at<double>(1, 0) = 0.0;
  79. Mat k1 = diff_eq( x_k ) + v;
  80. Mat tmp = x_k + step*0.5*k1;
  81. Mat k2 = diff_eq( tmp ) + v;
  82. tmp = x_k + step*0.5*k2;
  83. Mat k3 = diff_eq( tmp ) + v;
  84. tmp = x_k + step*k3;
  85. Mat k4 = diff_eq( tmp ) + v;
  86. x_kplus1 = x_k + (1.0/6.0)*step*( k1 + 2.0*k2 + 2.0*k3 + k4 ) + u_k;
  87. }
  88. void measurementFunction(const Mat& x_k, const Mat& n_k, Mat& z_k)
  89. {
  90. double x1 = x_k.at<double>(0, 0);
  91. double x2 = x_k.at<double>(1, 0);
  92. double x1_r = 6374.0;
  93. double x2_r = 0.0;
  94. double R = sqrt( pow( x1 - x1_r, 2 ) + pow( x2 - x2_r, 2 ) );
  95. double Phi = atan( (x2 - x2_r)/(x1 - x1_r) );
  96. R += n_k.at<double>(0, 0);
  97. Phi += n_k.at<double>(1, 0);
  98. z_k.at<double>(0, 0) = R;
  99. z_k.at<double>(1, 0) = Phi;
  100. }
  101. };
  102. const double BallisticModel::step = 0.05;
  103. TEST(UKF, br_landing_point)
  104. {
  105. const double abs_error = 0.1;
  106. const int nIterations = 4000; // number of iterations before landing
  107. const double landing_coordinate = 2.5; // the expected landing coordinate
  108. const double alpha = 1;
  109. const double beta = 2.0;
  110. const double kappa = -2.0;
  111. int MP = 2;
  112. int DP = 5;
  113. int CP = 0;
  114. int type = CV_64F;
  115. Mat processNoiseCov = Mat::zeros( DP, DP, type );
  116. processNoiseCov.at<double>(0, 0) = 1e-14;
  117. processNoiseCov.at<double>(1, 1) = 1e-14;
  118. processNoiseCov.at<double>(2, 2) = 2.4065 * 1e-5;
  119. processNoiseCov.at<double>(3, 3) = 2.4065 * 1e-5;
  120. processNoiseCov.at<double>(4, 4) = 1e-6;
  121. Mat processNoiseCovSqrt = Mat::zeros( DP, DP, type );
  122. sqrt( processNoiseCov, processNoiseCovSqrt );
  123. Mat measurementNoiseCov = Mat::zeros( MP, MP, type );
  124. measurementNoiseCov.at<double>(0, 0) = 1e-3*1e-3;
  125. measurementNoiseCov.at<double>(1, 1) = 0.13*0.13;
  126. Mat measurementNoiseCovSqrt = Mat::zeros( MP, MP, type );
  127. sqrt( measurementNoiseCov, measurementNoiseCovSqrt );
  128. RNG rng( 117 );
  129. Mat state( DP, 1, type );
  130. state.at<double>(0, 0) = 6500.4;
  131. state.at<double>(1, 0) = 349.14;
  132. state.at<double>(2, 0) = -1.8093;
  133. state.at<double>(3, 0) = -6.7967;
  134. state.at<double>(4, 0) = 0.6932;
  135. Mat initState = state.clone();
  136. initState.at<double>(4, 0) = 0.0;
  137. Mat P = 1e-6 * Mat::eye( DP, DP, type );
  138. P.at<double>(4, 4) = 1.0;
  139. Mat measurement( MP, 1, type );
  140. Mat q( DP, 1, type );
  141. Mat r( MP, 1, type );
  142. Ptr<BallisticModel> model( new BallisticModel() );
  143. UnscentedKalmanFilterParams params( DP, MP, CP, 0, 0, model );
  144. params.stateInit = initState.clone();
  145. params.errorCovInit = P.clone();
  146. params.measurementNoiseCov = measurementNoiseCov.clone();
  147. params.processNoiseCov = processNoiseCov.clone();
  148. params.alpha = alpha;
  149. params.beta = beta;
  150. params.k = kappa;
  151. Ptr<UnscentedKalmanFilter> uncsentedKalmanFilter = createUnscentedKalmanFilter(params);
  152. Mat correctStateUKF( DP, 1, type );
  153. Mat u = Mat::zeros( DP, 1, type );
  154. for (int i = 0; i<nIterations; i++)
  155. {
  156. rng.fill( q, RNG::NORMAL, Scalar::all(0), Scalar::all(1) );
  157. q = processNoiseCovSqrt*q;
  158. rng.fill( r, RNG::NORMAL, Scalar::all(0), Scalar::all(1) );
  159. r = measurementNoiseCovSqrt*r;
  160. model->stateConversionFunction(state, u, q, state);
  161. model->measurementFunction(state, r, measurement);
  162. uncsentedKalmanFilter->predict();
  163. correctStateUKF = uncsentedKalmanFilter->correct( measurement );
  164. }
  165. double landing_y = correctStateUKF.at<double>(1, 0);
  166. ASSERT_NEAR(landing_coordinate, landing_y, abs_error);
  167. }
  168. TEST(UKF, DISABLED_br_mean_squared_error)
  169. {
  170. const double velocity_treshold = 0.09;
  171. const double state_treshold = 0.9;
  172. const int nIterations = 4000; // number of iterations before landing
  173. const double alpha = 1;
  174. const double beta = 2.0;
  175. const double kappa = -2.0;
  176. int MP = 2;
  177. int DP = 5;
  178. int CP = 0;
  179. int type = CV_64F;
  180. Mat processNoiseCov = Mat::zeros( DP, DP, type );
  181. processNoiseCov.at<double>(0, 0) = 1e-14;
  182. processNoiseCov.at<double>(1, 1) = 1e-14;
  183. processNoiseCov.at<double>(2, 2) = 2.4065 * 1e-5;
  184. processNoiseCov.at<double>(3, 3) = 2.4065 * 1e-5;
  185. processNoiseCov.at<double>(4, 4) = 1e-6;
  186. Mat processNoiseCovSqrt = Mat::zeros( DP, DP, type );
  187. sqrt( processNoiseCov, processNoiseCovSqrt );
  188. Mat measurementNoiseCov = Mat::zeros( MP, MP, type );
  189. measurementNoiseCov.at<double>(0, 0) = 1e-3*1e-3;
  190. measurementNoiseCov.at<double>(1, 1) = 0.13*0.13;
  191. Mat measurementNoiseCovSqrt = Mat::zeros( MP, MP, type );
  192. sqrt( measurementNoiseCov, measurementNoiseCovSqrt );
  193. RNG rng( 464 );
  194. Mat state( DP, 1, type );
  195. state.at<double>(0, 0) = 6500.4;
  196. state.at<double>(1, 0) = 349.14;
  197. state.at<double>(2, 0) = -1.8093;
  198. state.at<double>(3, 0) = -6.7967;
  199. state.at<double>(4, 0) = 0.6932;
  200. Mat initState = state.clone();
  201. Mat initStateKF = state.clone();
  202. initStateKF.at<double>(4, 0) = 0.0;
  203. Mat P = 1e-6 * Mat::eye( DP, DP, type );
  204. P.at<double>(4, 4) = 1.0;
  205. Mat measurement( MP, 1, type );
  206. Mat q( DP, 1, type);
  207. Mat r( MP, 1, type);
  208. Ptr<BallisticModel> model( new BallisticModel() );
  209. UnscentedKalmanFilterParams params( DP, MP, CP, 0, 0, model );
  210. params.stateInit = initStateKF.clone();
  211. params.errorCovInit = P.clone();
  212. params.measurementNoiseCov = measurementNoiseCov.clone();
  213. params.processNoiseCov = processNoiseCov.clone();
  214. params.alpha = alpha;
  215. params.beta = beta;
  216. params.k = kappa;
  217. Mat predictStateUKF( DP, 1, type );
  218. Mat correctStateUKF( DP, 1, type );
  219. Mat errors = Mat::zeros( nIterations, 4, type );
  220. Mat u = Mat::zeros( DP, 1, type );
  221. for (int j = 0; j<100; j++)
  222. {
  223. Ptr<UnscentedKalmanFilter> uncsentedKalmanFilter = createUnscentedKalmanFilter(params);
  224. state = initState.clone();
  225. for (int i = 0; i<nIterations; i++)
  226. {
  227. rng.fill( q, RNG::NORMAL, Scalar::all(0), Scalar::all(1) );
  228. q = processNoiseCovSqrt*q;
  229. rng.fill( r, RNG::NORMAL, Scalar::all(0), Scalar::all(1) );
  230. r = measurementNoiseCovSqrt*r;
  231. model->stateConversionFunction(state, u, q, state);
  232. model->measurementFunction(state, r, measurement);
  233. predictStateUKF = uncsentedKalmanFilter->predict();
  234. correctStateUKF = uncsentedKalmanFilter->correct( measurement );
  235. Mat errorUKF = state - correctStateUKF;
  236. for (int l = 0; l<4; l++)
  237. errors.at<double>(i, l) += pow( errorUKF.at<double>(l, 0), 2.0 );
  238. }
  239. }
  240. errors = errors/100.0;
  241. sqrt( errors, errors );
  242. double max_x1 = cvtest::norm(errors.col(0), NORM_INF);
  243. double max_x2 = cvtest::norm(errors.col(1), NORM_INF);
  244. double max_x3 = cvtest::norm(errors.col(2), NORM_INF);
  245. double max_x4 = cvtest::norm(errors.col(3), NORM_INF);
  246. ASSERT_GE( state_treshold, max_x1 );
  247. ASSERT_GE( state_treshold, max_x2 );
  248. ASSERT_GE( velocity_treshold, max_x3 );
  249. ASSERT_GE( velocity_treshold, max_x4 );
  250. }
  251. //In this test Unscented Kalman Filter are applied to the univariate nonstationary growth model (UNGM).
  252. //This model was used in example from "Unscented Kalman filtering for additive noise case: Augmented vs. non-augmented"
  253. //by Yuanxin Wu and Dewen Hu.
  254. class UnivariateNonstationaryGrowthModel: public UkfSystemModel
  255. {
  256. public:
  257. void stateConversionFunction(const Mat& x_k, const Mat& u_k, const Mat& v_k, Mat& x_kplus1)
  258. {
  259. double x = x_k.at<double>(0, 0);
  260. double n = u_k.at<double>(0, 0);
  261. double q = v_k.at<double>(0, 0);
  262. double u = u_k.at<double>(0, 0);
  263. double x1 = 0.5*x + 25*( x/(x*x + 1) ) + 8*cos( 1.2*(n-1) ) + q + u;
  264. x_kplus1.at<double>(0, 0) = x1;
  265. }
  266. void measurementFunction(const Mat& x_k, const Mat& n_k, Mat& z_k)
  267. {
  268. double x = x_k.at<double>(0, 0);
  269. double r = n_k.at<double>(0, 0);
  270. double y = x*x/20.0 + r;
  271. z_k.at<double>(0, 0) = y;
  272. }
  273. };
  274. TEST(UKF, DISABLED_ungm_mean_squared_error)
  275. {
  276. const double alpha = 1.5;
  277. const double beta = 2.0;
  278. const double kappa = 0.0;
  279. const double mse_treshold = 0.5;
  280. const int nIterations = 500; // number of observed iterations
  281. int MP = 1;
  282. int DP = 1;
  283. int CP = 0;
  284. int type = CV_64F;
  285. Ptr<UnivariateNonstationaryGrowthModel> model( new UnivariateNonstationaryGrowthModel() );
  286. UnscentedKalmanFilterParams params( DP, MP, CP, 0, 0, model );
  287. Mat processNoiseCov = Mat::zeros( DP, DP, type );
  288. processNoiseCov.at<double>(0, 0) = 1.0;
  289. Mat processNoiseCovSqrt = Mat::zeros( DP, DP, type );
  290. sqrt( processNoiseCov, processNoiseCovSqrt );
  291. Mat measurementNoiseCov = Mat::zeros( MP, MP, type );
  292. measurementNoiseCov.at<double>(0, 0) = 1.0;
  293. Mat measurementNoiseCovSqrt = Mat::zeros( MP, MP, type );
  294. sqrt( measurementNoiseCov, measurementNoiseCovSqrt );
  295. Mat P = Mat::eye( DP, DP, type );
  296. Mat state( DP, 1, type );
  297. state.at<double>(0, 0) = 0.1;
  298. Mat initState = state.clone();
  299. initState.at<double>(0, 0) = 0.0;
  300. params.errorCovInit = P;
  301. params.measurementNoiseCov = measurementNoiseCov;
  302. params.processNoiseCov = processNoiseCov;
  303. params.stateInit = initState.clone();
  304. params.alpha = alpha;
  305. params.beta = beta;
  306. params.k = kappa;
  307. Mat correctStateAUKF( DP, 1, type );
  308. Mat measurement( MP, 1, type );
  309. Mat exactMeasurement( MP, 1, type );
  310. Mat q( DP, 1, type );
  311. Mat r( MP, 1, type );
  312. Mat u( DP, 1, type );
  313. Mat zero = Mat::zeros( MP, 1, type );
  314. RNG rng( 216 );
  315. double average_error = 0.0;
  316. for (int j = 0; j<1000; j++)
  317. {
  318. cv::Ptr<UnscentedKalmanFilter> uncsentedKalmanFilter = createUnscentedKalmanFilter( params );
  319. state.at<double>(0, 0) = 0.1;
  320. double mse = 0.0;
  321. for (int i = 0; i<nIterations; i++)
  322. {
  323. rng.fill( q, RNG::NORMAL, Scalar::all(0), Scalar::all(1) );
  324. rng.fill( r, RNG::NORMAL, Scalar::all(0), Scalar::all(1) );
  325. q = processNoiseCovSqrt*q;
  326. r = measurementNoiseCovSqrt*r;
  327. u.at<double>(0, 0) = (double)i;
  328. model->stateConversionFunction(state, u, q, state);
  329. model->measurementFunction(state, zero, exactMeasurement);
  330. model->measurementFunction(state, r, measurement);
  331. uncsentedKalmanFilter->predict( u );
  332. correctStateAUKF = uncsentedKalmanFilter->correct( measurement );
  333. mse += pow( state.at<double>(0, 0) - correctStateAUKF.at<double>(0, 0), 2.0 );
  334. }
  335. mse /= nIterations;
  336. average_error += mse;
  337. }
  338. average_error /= 1000.0;
  339. ASSERT_GE( mse_treshold, average_error );
  340. }
  341. }} // namespace