JeVoisBase  1.20
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Kalman1D.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
19 #include <opencv2/video/tracking.hpp> // for kalman filter
20 
21 #pragma once
22 
23 namespace kalman1d
24 {
25  static jevois::ParameterCategory const ParamCateg("Kalman1D Options");
26 
27  //! Parameter \relates Kalman1D
28  JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(usevel, bool, "Use velocity tracking, in addition to position",
29  false, ParamCateg);
30 
31  //! Parameter \relates Kalman1D
32  JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(procnoise, float, "Process noise standard deviation",
33  0.003F, ParamCateg);
34 
35  //! Parameter \relates Kalman1D
36  JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(measnoise, float, "Measurement noise standard deviation",
37  0.05F, ParamCateg);
38 
39  //! Parameter \relates Kalman1D
40  JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(postnoise, float, "A posteriori error estimate standard deviation",
41  0.3F, ParamCateg);
42 }
43 
44 //! Simple component to track a moving 2D point over time using a Kalman filter
45 /*! A Kalman filter allows one to filter noisy data over time. Here, it is used to filter the possibly noisy results of
46  some machine vision algorith that is trying to detect a particular item in the camera's video stream. On occasion,
47  the detection might abruptly jump to an erroneous location. Such jumps are filtered out by the Kalman filter.
48 
49  Note that, unlike Kalman2D, this class does not perform any normalization or range transformation.
50 
51  This code loosely inspired by https://github.com/abreheret/kalman-mouse/blob/master/src/main.cpp
52 
53  \ingroup components */
54 class Kalman1D : public jevois::Component,
55  public jevois::Parameter<kalman1d::usevel, kalman1d::procnoise,
56  kalman1d::measnoise, kalman1d::postnoise>
57 {
58  public:
59  //! Constructor
60  Kalman1D(std::string const & instance);
61 
62  //! Destructor
63  virtual ~Kalman1D();
64 
65  //! Function to call each time you have a new measurement
66  void set(float x);
67 
68  //! Function to call each time you want to get the Kalman-filtered coordinates
69  /*! It is ok to call get() multiple times with no intervening set(), if you have no new measurements but still want
70  to use the filter output.
71 
72  eps is used for rounding of returned value, which is convenient to avoid sending very long floating point
73  values over serial port. */
74  float get(float const eps = 0.01F);
75 
76  protected:
77  void postInit() override;
78  void onParamChange(kalman1d::usevel const & param, bool const & newval) override;
79  void onParamChange(kalman1d::procnoise const & param, float const & newval) override;
80  void onParamChange(kalman1d::measnoise const & param, float const & newval) override;
81  void onParamChange(kalman1d::postnoise const & param, float const & newval) override;
82 
83  cv::KalmanFilter itsKF;
84  cv::Mat itsState; //!< (x, y, xdot, ydot)
85  cv::Mat itsProcessNoise;
86  cv::Mat itsMeasurement;
87  cv::Mat itsLatest;
88  bool itsFresh;
89 
90 }; // class Kalman1D
91 
Kalman1D::itsProcessNoise
cv::Mat itsProcessNoise
Definition: Kalman1D.H:85
Kalman1D::itsMeasurement
cv::Mat itsMeasurement
Definition: Kalman1D.H:86
kalman1d
Definition: Kalman1D.H:23
jevois::Component
Kalman1D::itsState
cv::Mat itsState
(x, y, xdot, ydot)
Definition: Kalman1D.H:84
Kalman1D::~Kalman1D
virtual ~Kalman1D()
Destructor.
Definition: Kalman1D.C:31
jevois::ParameterCategory
Kalman1D::Kalman1D
Kalman1D(std::string const &instance)
Constructor.
Definition: Kalman1D.C:23
Kalman1D::itsFresh
bool itsFresh
Definition: Kalman1D.H:88
F
float F
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(l2grad, bool, "Use more accurate L2 gradient norm if true, L1 if false", false, ParamCateg)
Component.H
Kalman1D::itsKF
cv::KalmanFilter itsKF
Definition: Kalman1D.H:83
Kalman1D::itsLatest
cv::Mat itsLatest
Definition: Kalman1D.H:87
Kalman1D::onParamChange
void onParamChange(kalman1d::usevel const &param, bool const &newval) override
Kalman1D::set
void set(float x)
Function to call each time you have a new measurement.
Definition: Kalman1D.C:70
Kalman1D::get
float get(float const eps=0.01F)
Function to call each time you want to get the Kalman-filtered coordinates.
Definition: Kalman1D.C:78
Kalman1D::postInit
void postInit() override
Definition: Kalman1D.C:35
Kalman1D
Simple component to track a moving 2D point over time using a Kalman filter.
Definition: Kalman1D.H:54