JeVoisBase  1.20
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Kalman2D.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 kalman2d
24 {
25  static jevois::ParameterCategory const ParamCateg("Kalman2D Options");
26 
27  //! Parameter \relates Kalman2D
28  JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(usevel, bool, "Use velocity tracking, in addition to position",
29  false, ParamCateg);
30 
31  //! Parameter \relates Kalman2D
32  JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(procnoise, float, "Process noise standard deviation",
33  0.003F, ParamCateg);
34 
35  //! Parameter \relates Kalman2D
36  JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(measnoise, float, "Measurement noise standard deviation",
37  0.05F, ParamCateg);
38 
39  //! Parameter \relates Kalman2D
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  See for example DemoSaliency to see the Kalman2D filtering over time the location of the most salient object in the
50  video stream.
51 
52  To allow using this component in many different modules and to support various image resolutions, we here internally
53  normalize all measurements and filtered outputs to between [-1000.0 ... 1000.0] horizontally and [-750 ... 750
54  vertically, where (-1000.0, -750.0) is the top-left of the image, (0.0, 0.0) the center, and (1000.0, 750.0) the
55  bottom-right corner.
56 
57  Helper functions are used to do this conversion, see \ref coordhelpers.
58 
59  The range [-1000.0 ... 1000.0] is fixed and is not exposed as a parameter so that users of the Kalman2D class will
60  not be tempted to use various ranges, in an effort to standardize the interface between vision modules (which may
61  track object locations using Kalman2D) and motor control (e.g., an Arduino controlling servos on a pan/tilt camera
62  head). The writers of motor control code can reliably assume that object coordinates sent to them will be in the
63  [-1000.0 ... 1000.0] horizontal and [-750 ... 750] vertical range with 0,0 at dead center, and control their motors
64  accordingly.
65 
66  This code loosely inspired by https://github.com/abreheret/kalman-mouse/blob/master/src/main.cpp
67 
68  \ingroup components */
69 class Kalman2D : public jevois::Component,
70  public jevois::Parameter<kalman2d::usevel, kalman2d::procnoise,
71  kalman2d::measnoise, kalman2d::postnoise>
72 {
73  public:
74  //! Constructor
75  Kalman2D(std::string const & instance);
76 
77  //! Destructor
78  virtual ~Kalman2D();
79 
80  //! Function to call each time you have a new measurement (output from a vision algorithm)
81  /*! This variant does not normalize the coordinates, they should typically be in [-1000.0 .. 1000.0] */
82  void set(float x, float y);
83 
84  //! Function to call each time you have a new measurement (output from a vision algorithm)
85  /*! This variant normalizes the image coordinates to the range [-1000.0 .. 1000.0] horizontal and [-750 ... 750]
86  vertical. */
87  void set(float x, float y, unsigned int imgwidth, unsigned int imgheight);
88 
89  //! Function to call each time you want to get the Kalman-filtered coordinates
90  /*! It is ok to call get() multiple times with no intervening set(), if you have no new measurements but still want
91  to use the filter output. This variant does not normalize the coordinates, they should typically be in [-1000.0
92  .. 1000.0] range.
93 
94  eps is used for rounding of returned coordinates, which is convenient to avoid sending very long floating point
95  values over serial port. */
96  void get(float & x, float & y, float const eps = 0.01F);
97 
98  //! Function to call each time you want to get the Kalman-filtered coordinates
99  /*! It is ok to call get() multiple times with no intervening set(), if you have no new measurements but still want
100  to use the filter output. This variant normalizes the coordinates back to image coordinates in [0 .. imgwidth[ x
101  [0 .. imgheight[ */
102  void get(float & x, float & y, unsigned int imgwidth, unsigned int imgheight, float const eps = 0.01F);
103 
104  //! Function to call each time you want to get the Kalman-filtered coordinates
105  /*! It is ok to call get() multiple times with no intervening set(), if you have no new measurements but still want
106  to use the filter output. This variant returns both the coordinates in [0 .. 1000] range (typically, so send to
107  an Arduino for servo control, or other motor plant), and the image coordinates in [0 .. imgwidth[ x [0
108  .. imgheight[ (typically, to draw on a debug/demo image). */
109  void get(float & rawx, float & rawy, float & imgx, float & imgy, unsigned int imgwidth, unsigned int imgheight,
110  float const raweps = 0.01F, float const imgeps = 0.01F);
111 
112  protected:
113  void postInit() override;
114  void onParamChange(kalman2d::usevel const & param, bool const & newval) override;
115  void onParamChange(kalman2d::procnoise const & param, float const & newval) override;
116  void onParamChange(kalman2d::measnoise const & param, float const & newval) override;
117  void onParamChange(kalman2d::postnoise const & param, float const & newval) override;
118 
119  cv::KalmanFilter itsKF;
120  cv::Mat itsState; //!< (x, y, xdot, ydot)
122  cv::Mat itsMeasurement;
123  cv::Mat itsLatest;
124  bool itsFresh;
125 
126 }; // class Kalman2D
127 
Kalman2D::itsProcessNoise
cv::Mat itsProcessNoise
Definition: Kalman2D.H:121
Kalman2D::postInit
void postInit() override
Definition: Kalman2D.C:35
Kalman2D::itsMeasurement
cv::Mat itsMeasurement
Definition: Kalman2D.H:122
jevois::Component
Kalman2D::itsKF
cv::KalmanFilter itsKF
Definition: Kalman2D.H:119
Kalman2D
Simple component to track a moving 2D point over time using a Kalman filter.
Definition: Kalman2D.H:69
Kalman2D::itsState
cv::Mat itsState
(x, y, xdot, ydot)
Definition: Kalman2D.H:120
jevois::ParameterCategory
Kalman2D::Kalman2D
Kalman2D(std::string const &instance)
Constructor.
Definition: Kalman2D.C:23
Kalman2D::~Kalman2D
virtual ~Kalman2D()
Destructor.
Definition: Kalman2D.C:31
kalman2d
Definition: Kalman2D.H:23
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
Kalman2D::itsLatest
cv::Mat itsLatest
Definition: Kalman2D.H:123
Kalman2D::itsFresh
bool itsFresh
Definition: Kalman2D.H:124
Kalman2D::onParamChange
void onParamChange(kalman2d::usevel const &param, bool const &newval) override
Kalman2D::set
void set(float x, float y)
Function to call each time you have a new measurement (output from a vision algorithm)
Definition: Kalman2D.C:76
Kalman2D::get
void get(float &x, float &y, float const eps=0.01F)
Function to call each time you want to get the Kalman-filtered coordinates.
Definition: Kalman2D.C:91