Visual Servoing Platform  version 3.6.1 under development (2025-01-11)
vpRBTracker.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  */
30 
35 #ifndef VP_RB_TRACKER_H
36 #define VP_RB_TRACKER_H
37 
38 #include <visp3/core/vpConfig.h>
39 
40 #if defined(VISP_HAVE_PANDA3D)
41 
42 #include <visp3/rbt/vpRBFeatureTracker.h>
43 #include <visp3/rbt/vpRBSilhouettePointsExtractionSettings.h>
44 #include <visp3/rbt/vpPanda3DDepthFilters.h>
45 #include <visp3/rbt/vpObjectCentricRenderer.h>
46 #include <visp3/rbt/vpRBTrackerLogger.h>
47 
48 #include <visp3/core/vpDisplay.h>
49 
50 #include <ostream>
51 
52 #if defined(VISP_HAVE_NLOHMANN_JSON)
53 #include VISP_NLOHMANN_JSON(json_fwd.hpp)
54 #endif
55 
56 BEGIN_VISP_NAMESPACE
57 
58 class vpObjectMask;
59 class vpRBDriftDetector;
60 class vpRBVisualOdometry;
61 
67 class VISP_EXPORT vpRBTracker
68 {
69 public:
70 
71  vpRBTracker();
72 
73  ~vpRBTracker() = default;
74 
79  void getPose(vpHomogeneousMatrix &cMo) const;
80  void setPose(const vpHomogeneousMatrix &cMo);
81  vpObjectCentricRenderer &getRenderer();
82  const vpRBFeatureTrackerInput &getMostRecentFrame() const { return m_currentFrame; }
83  const vpRBTrackerLogger &getLogger() const { return m_logger; }
84 
85  vpMatrix getCovariance() const;
86 
95  void addTracker(std::shared_ptr<vpRBFeatureTracker> tracker);
96  void setupRenderer(const std::string &file);
97  std::string getModelPath() const { return m_modelPath; }
98  void setModelPath(const std::string &path);
99 
100  std::vector<std::shared_ptr<vpRBFeatureTracker>> getFeatureTrackers() { return m_trackers; }
101 
102  vpCameraParameters getCameraParameters() const;
103  void setCameraParameters(const vpCameraParameters &cam, unsigned h, unsigned w);
104 
105  unsigned int getImageWidth() const { return m_imageWidth; }
106  unsigned int getImageHeight() const { return m_imageHeight; }
107 
109  {
110  return m_depthSilhouetteSettings;
111  }
112 
113  void setSilhouetteExtractionParameters(const vpSilhouettePointsExtractionSettings &settings);
114 
115  double getOptimizationGain() const { return m_lambda; }
116  void setOptimizationGain(double lambda)
117  {
118  if (lambda < 0.0) {
119  throw vpException(vpException::badValue, "Optimization gain should be greater to zero");
120  }
121  m_lambda = lambda;
122  }
123  unsigned int getMaxOptimizationIters() const { return m_vvsIterations; }
124  void setMaxOptimizationIters(unsigned int iters)
125  {
126  if (iters == 0) {
127  throw vpException(vpException::badValue, "Max number of iterations must be greater than zero");
128  }
129  m_vvsIterations = iters;
130  }
131 
132  double getOptimizationInitialMu() const { return m_muInit; }
133  void setOptimizationInitialMu(double mu)
134  {
135  if (mu < 0.0) {
136  throw vpException(vpException::badValue, "Optimization gain should be greater or equal to zero");
137  }
138  m_muInit = mu;
139  }
140 
141  double getOptimizationMuIterFactor() const { return m_muIterFactor; }
142  void setOptimizationMuIterFactor(double factor)
143  {
144  if (factor < 0.0) {
145  throw vpException(vpException::badValue, "Optimization gain should be greater or equal to zero");
146  }
147  m_muIterFactor = factor;
148  }
149 
150  std::shared_ptr<vpRBDriftDetector> getDriftDetector() const { return m_driftDetector; }
151  void setDriftDetector(const std::shared_ptr<vpRBDriftDetector> &detector)
152  {
153  m_driftDetector = detector;
154  }
155 
156  std::shared_ptr<vpObjectMask> getObjectSegmentationMethod() const { return m_mask; }
157  void setObjectSegmentationMethod(const std::shared_ptr<vpObjectMask> &mask)
158  {
159  m_mask = mask;
160  }
161 
162  std::shared_ptr<vpRBVisualOdometry> getOdometryMethod() const { return m_odometry; }
163  void setOdometryMethod(const std::shared_ptr<vpRBVisualOdometry> &odometry)
164  {
165  m_odometry = odometry;
166  }
167 
172  bool getVerbose()
173  {
174  return m_verbose;
175  }
176 
181  void setVerbose(bool verbose)
182  {
183  m_verbose = verbose;
184  }
185 
186 #if defined(VISP_HAVE_NLOHMANN_JSON)
187  void loadConfigurationFile(const std::string &filename);
188  void loadConfiguration(const nlohmann::json &j);
189 #endif
190 
195  void reset();
196 
201  void startTracking();
202  void track(const vpImage<unsigned char> &I);
203  void track(const vpImage<unsigned char> &I, const vpImage<vpRGBa> &IRGB);
204  void track(const vpImage<unsigned char> &I, const vpImage<vpRGBa> &IRGB, const vpImage<float> &depth);
213  void displayMask(vpImage<unsigned char> &Imask) const;
214  void display(const vpImage<unsigned char> &I, const vpImage<vpRGBa> &IRGB, const vpImage<unsigned char> &depth);
219 #ifdef VISP_HAVE_MODULE_GUI
220  void initClick(const vpImage<unsigned char> &I, const std::string &initFile, bool displayHelp);
221 #endif
222 
223 protected:
224 
225  void track(vpRBFeatureTrackerInput &input);
226  void updateRender(vpRBFeatureTrackerInput &frame);
227 
228  std::vector<vpRBSilhouettePoint> extractSilhouettePoints(
229  const vpImage<vpRGBf> &Inorm, const vpImage<float> &Idepth,
230  const vpImage<vpRGBf> &Ior, const vpImage<unsigned char> &Ivalid,
231  const vpCameraParameters &cam, const vpHomogeneousMatrix &cTcp);
232 
233  template<typename T>
234  void checkDimensionsOrThrow(const vpImage<T> &I, const std::string &imgType) const
235  {
236  if (I.getRows() != m_imageHeight || I.getCols() != m_imageWidth) {
237  std::stringstream ss;
238  ss << "vpRBTracker: dimension error: expected " << imgType;
239  ss << " image to have the following resolution " << m_imageWidth << " x " << m_imageHeight;
240  ss << ", but got " << I.getCols() << " x " << I.getRows();
241  throw vpException(vpException::dimensionError, ss.str());
242  }
243  }
244 
246 
247  std::vector<std::shared_ptr<vpRBFeatureTracker>> m_trackers;
248 
251 
252  std::string m_modelPath;
256 
257  double m_lambda;
258  unsigned m_vvsIterations;
259  double m_muInit;
260  double m_muIterFactor;
261 
265 
266  unsigned m_imageHeight, m_imageWidth;
267 
269  bool m_verbose;
270 
271  std::shared_ptr<vpObjectMask> m_mask;
272  std::shared_ptr<vpRBDriftDetector> m_driftDetector;
273  std::shared_ptr<vpRBVisualOdometry> m_odometry;
274 
275 };
276 
277 END_VISP_NAMESPACE
278 
279 
280 #endif
281 #endif
Generic class defining intrinsic camera parameters.
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:73
@ dimensionError
Bad dimension.
Definition: vpException.h:71
Implementation of an homogeneous matrix and operations on such kind of matrices.
unsigned int getCols() const
Definition: vpImage.h:171
unsigned int getRows() const
Definition: vpImage.h:212
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
Single object focused renderer.
Rendering parameters for a panda3D simulation.
Base interface for algorithms that should detect tracking drift for the render-based tracker.
All the data related to a single tracking frame. This contains both the input data (from a real camer...
const vpRBTrackerLogger & getLogger() const
Definition: vpRBTracker.h:83
std::shared_ptr< vpRBDriftDetector > m_driftDetector
Definition: vpRBTracker.h:272
std::shared_ptr< vpRBVisualOdometry > m_odometry
Definition: vpRBTracker.h:273
vpSilhouettePointsExtractionSettings m_depthSilhouetteSettings
Factor with which to multiply mu at every iteration during VVS.
Definition: vpRBTracker.h:262
~vpRBTracker()=default
vpCameraParameters m_cam
Definition: vpRBTracker.h:255
void checkDimensionsOrThrow(const vpImage< T > &I, const std::string &imgType) const
Definition: vpRBTracker.h:234
double getOptimizationInitialMu() const
Definition: vpRBTracker.h:132
bool m_verbose
Definition: vpRBTracker.h:269
vpObjectCentricRenderer m_renderer
Definition: vpRBTracker.h:264
unsigned int getImageWidth() const
Definition: vpRBTracker.h:105
std::shared_ptr< vpRBVisualOdometry > getOdometryMethod() const
Definition: vpRBTracker.h:162
unsigned m_vvsIterations
VVS gain.
Definition: vpRBTracker.h:258
vpRBTrackerLogger m_logger
Color and render image dimensions.
Definition: vpRBTracker.h:268
bool m_firstIteration
Definition: vpRBTracker.h:245
void setVerbose(bool verbose)
Definition: vpRBTracker.h:181
double m_muIterFactor
Initial mu value for Levenberg-Marquardt.
Definition: vpRBTracker.h:260
std::vector< std::shared_ptr< vpRBFeatureTracker > > m_trackers
Whether this is the first iteration.
Definition: vpRBTracker.h:247
std::string m_modelPath
Definition: vpRBTracker.h:252
vpPanda3DRenderParameters m_rendererSettings
Definition: vpRBTracker.h:263
double getOptimizationMuIterFactor() const
Definition: vpRBTracker.h:141
unsigned m_imageHeight
Definition: vpRBTracker.h:266
std::shared_ptr< vpObjectMask > getObjectSegmentationMethod() const
Definition: vpRBTracker.h:156
double m_lambda
Definition: vpRBTracker.h:257
std::shared_ptr< vpObjectMask > m_mask
Definition: vpRBTracker.h:271
vpHomogeneousMatrix m_cMoPrev
Definition: vpRBTracker.h:254
void setDriftDetector(const std::shared_ptr< vpRBDriftDetector > &detector)
Definition: vpRBTracker.h:151
vpRBFeatureTrackerInput m_currentFrame
List of trackers.
Definition: vpRBTracker.h:249
const vpRBFeatureTrackerInput & getMostRecentFrame() const
Definition: vpRBTracker.h:82
void setOdometryMethod(const std::shared_ptr< vpRBVisualOdometry > &odometry)
Definition: vpRBTracker.h:163
unsigned int getImageHeight() const
Definition: vpRBTracker.h:106
double getOptimizationGain() const
Definition: vpRBTracker.h:115
vpRBFeatureTrackerInput m_previousFrame
Definition: vpRBTracker.h:250
std::shared_ptr< vpRBDriftDetector > getDriftDetector() const
Definition: vpRBTracker.h:150
unsigned int getMaxOptimizationIters() const
Definition: vpRBTracker.h:123
std::vector< std::shared_ptr< vpRBFeatureTracker > > getFeatureTrackers()
Definition: vpRBTracker.h:100
void setOptimizationMuIterFactor(double factor)
Definition: vpRBTracker.h:142
void setOptimizationInitialMu(double mu)
Definition: vpRBTracker.h:133
void setOptimizationGain(double lambda)
Definition: vpRBTracker.h:116
double m_muInit
Max number of VVS iterations.
Definition: vpRBTracker.h:259
std::string getModelPath() const
Definition: vpRBTracker.h:97
vpSilhouettePointsExtractionSettings getSilhouetteExtractionParameters() const
Definition: vpRBTracker.h:108
void setObjectSegmentationMethod(const std::shared_ptr< vpObjectMask > &mask)
Definition: vpRBTracker.h:157
vpHomogeneousMatrix m_cMo
Definition: vpRBTracker.h:253
void setMaxOptimizationIters(unsigned int iters)
Definition: vpRBTracker.h:124
bool getVerbose()
Definition: vpRBTracker.h:172