Visual Servoing Platform  version 3.6.1 under development (2025-02-11)
vpRBSilhouetteCCDTracker.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_SILHOUETTE_CCD_TRACKER_H
36 #define VP_SILHOUETTE_CCD_TRACKER_H
37 
38 #include <visp3/core/vpConfig.h>
39 #include <visp3/core/vpColVector.h>
40 #include <visp3/core/vpImage.h>
41 #include <visp3/core/vpCameraParameters.h>
42 #include <visp3/core/vpExponentialMap.h>
43 #include <visp3/core/vpPixelMeterConversion.h>
44 #include <visp3/core/vpMatrix.h>
45 #include <visp3/core/vpHomogeneousMatrix.h>
46 #include <visp3/core/vpRobust.h>
47 
48 // #if defined(VISP_HAVE_SIMDLIB)
49 // #include <Simd/SimdLib.h>
50 // #endif
51 #include <visp3/rbt/vpRBSilhouetteControlPoint.h>
52 #include <visp3/rbt/vpRBFeatureTracker.h>
53 
54 #include <vector>
55 #include <iostream>
56 #include <algorithm>
57 
58 #if defined(VISP_HAVE_NLOHMANN_JSON)
59 #include VISP_NLOHMANN_JSON(json.hpp)
60 #endif
61 
62 BEGIN_VISP_NAMESPACE
63 
64 
65 enum vpRBSilhouetteCCDDisplayType
66 {
67  SIMPLE = 0,
68  WEIGHT = 1,
69  ERROR = 2,
70  INVALID = 3
71 };
72 
73 class VISP_EXPORT vpCCDParameters
74 {
75 public:
76  vpCCDParameters() : gamma_1(0.5), gamma_2(4), gamma_3(4), gamma_4(3), alpha(1.3), beta(0.06), kappa(0.5), covarianceIterDecreaseFactor(0.25), h(40), delta_h(1), phi_dim(6)
77  { }
78 
79 
80  ~vpCCDParameters() = default;
85  double gamma_1;
90  double gamma_2;
95  double gamma_3;
100  double gamma_4;
101  double alpha;
102  double beta;
103 
111  double kappa;
128  int h;
133  int delta_h;
138  int phi_dim;
139 };
140 
141 #if defined(VISP_HAVE_NLOHMANN_JSON)
142 inline void from_json(const nlohmann::json &j, vpCCDParameters &ccdParameters)
143 {
144  ccdParameters.alpha = j.value("alpha", ccdParameters.alpha);
145  ccdParameters.beta = j.value("beta", ccdParameters.beta);
146  ccdParameters.kappa = j.value("kappa", ccdParameters.kappa);
147  ccdParameters.covarianceIterDecreaseFactor = j.value("covarianceIterDecreaseFactor",
148  ccdParameters.covarianceIterDecreaseFactor);
149  ccdParameters.h = j.value("h", ccdParameters.h);
150  ccdParameters.delta_h = j.value("delta_h", ccdParameters.delta_h);
151  ccdParameters.phi_dim = j.value("phi_dim", ccdParameters.phi_dim);
152  if (j.contains("gamma")) {
153  nlohmann::json gammaj = j["gamma"];
154  if (!gammaj.is_array() || gammaj.size() != 4) {
155  throw vpException(vpException::ioError, "CCD parameters: tried to read gamma values from something that is not a 4-sized float array");
156  }
157  ccdParameters.gamma_1 = gammaj[0];
158  ccdParameters.gamma_2 = gammaj[1];
159  ccdParameters.gamma_3 = gammaj[2];
160  ccdParameters.gamma_4 = gammaj[3];
161  }
162 }
163 #endif
164 
165 class VISP_EXPORT vpCCDStatistics
166 {
167 public:
174 
175  void reinit(int resolution, unsigned normalPointsNumber)
176  {
177  nv.resize(resolution, 2, false, false);
178  mean_vic.resize(resolution, 6, false, false);
179  cov_vic.resize(resolution, 18, false, false);
180  vic.resize(resolution, 20 * normalPointsNumber, false, false);
181  imgPoints.resize(resolution, 2 * 3 * normalPointsNumber, false, false);
182  weight.resize(resolution, 2 * normalPointsNumber, false, false);
183  }
184 };
185 
192 {
193 public:
194 
196  virtual ~vpRBSilhouetteCCDTracker() = default;
197 
198  bool requiresRGB() const VP_OVERRIDE { return true; }
199  bool requiresDepth() const VP_OVERRIDE { return false; }
200  bool requiresSilhouetteCandidates() const VP_OVERRIDE { return true; }
201 
206  void setCCDParameters(const vpCCDParameters &parameters) { m_ccdParameters = parameters; }
207  vpCCDParameters getCCDParameters() const { return m_ccdParameters; }
208  //void computeMask(const vpImage<vpRGBa> &render, vpCCDStatistics &stats);
209 
215  double getTemporalSmoothingFactor() const { return m_temporalSmoothingFac; }
223  void setTemporalSmoothingFactor(double factor)
224  {
225  if (factor < 0.0) {
226  throw vpException(vpException::badValue, "Temporal smoothing factor should be equal to or greater than 0");
227  }
228  m_temporalSmoothingFac = factor;
229 
230  }
231 
236  bool shouldUseMask() const { return m_useMask; }
237  void setShouldUseMask(bool useMask) { m_useMask = useMask; }
238 
244  float getMinimumMaskConfidence() const { return m_minMaskConfidence; }
245  void setMinimumMaskConfidence(float confidence)
246  {
247 
248  m_minMaskConfidence = confidence;
249  }
250 
251  void setDisplayType(vpRBSilhouetteCCDDisplayType type)
252  {
253  m_displayType = type;
254  }
255 
260  void onTrackingIterStart() VP_OVERRIDE { }
261  void onTrackingIterEnd() VP_OVERRIDE { }
262 
263  double getVVSTrackerWeight() const VP_OVERRIDE { return m_userVvsWeight / (10 * m_error.size()); }
264 
265  void extractFeatures(const vpRBFeatureTrackerInput &frame, const vpRBFeatureTrackerInput &previousFrame, const vpHomogeneousMatrix &cMo) VP_OVERRIDE;
266  void trackFeatures(const vpRBFeatureTrackerInput & /*frame*/, const vpRBFeatureTrackerInput & /*previousFrame*/, const vpHomogeneousMatrix & /*cMo*/) VP_OVERRIDE { }
267 
268  void initVVS(const vpRBFeatureTrackerInput &frame, const vpRBFeatureTrackerInput &previousFrame, const vpHomogeneousMatrix &cMo) VP_OVERRIDE;
269  void computeVVSIter(const vpRBFeatureTrackerInput &frame, const vpHomogeneousMatrix &cMo, unsigned int iteration) VP_OVERRIDE;
270  void updateCovariance(const double /*lambda*/) VP_OVERRIDE
271  {
272  m_cov = m_sigma;
273  }
274 
275  void display(const vpCameraParameters &cam, const vpImage<unsigned char> &I, const vpImage<vpRGBa> &IRGB, const vpImage<unsigned char> &depth) const VP_OVERRIDE;
276 
277 #if defined(VISP_HAVE_NLOHMANN_JSON)
278  virtual void loadJsonConfiguration(const nlohmann::json &j) VP_OVERRIDE
279  {
281 
282  m_vvsConvergenceThreshold = j.value("convergenceThreshold", m_vvsConvergenceThreshold);
283  setTemporalSmoothingFactor(j.value("temporalSmoothing", m_temporalSmoothingFac));
284  setShouldUseMask(j.value("useMask", m_useMask));
285  setMinimumMaskConfidence(j.value("minMaskConfidence", m_minMaskConfidence));
286 
287  m_ccdParameters = j.value("ccd", m_ccdParameters);
288  }
289 
290 #endif
291 
292 protected:
293  void updateCCDPoints(const vpHomogeneousMatrix &cMo);
294  void computeLocalStatistics(const vpImage<vpRGBa> &I, vpCCDStatistics &stats);
295  void computeErrorAndInteractionMatrix();
296  double computeMaskGradient(const vpImage<float> &mask, const vpRBSilhouetteControlPoint &pccd) const;
297 
298 
300 
301  std::vector<vpRBSilhouetteControlPoint> m_controlPoints;
303 
306 
308 
310  double tol;
311 
312  std::vector<vpColVector> m_gradients;
313  std::vector<vpMatrix> m_hessians;
317 
318  bool m_useMask;
320 
321  vpRBSilhouetteCCDDisplayType m_displayType;
322 };
323 
324 END_VISP_NAMESPACE
325 
326 #endif
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:442
double gamma_2
Curve uncertainty computation hyperparameter Recommended to leave fixed.
double gamma_3
Curve uncertainty computation hyperparameter Recommended to leave fixed.
double gamma_1
Curve uncertainty computation hyperparameter Recommended to leave fixed.
double covarianceIterDecreaseFactor
From the CCD paper: maximum decrease of the covariance within one iteration step. Between 0 and 1 If ...
int delta_h
Sample step when computing statistics and errors. Increase this value to decrease computation time,...
~vpCCDParameters()=default
int phi_dim
Number of parameters estimated by CCD. Either 6 or 8. Leave this fixed.
int h
Size of the vicinity that is used to compute statistics and error. Length of the line along the norma...
double gamma_4
Curve uncertainty computation hyperparameter Recommended to leave fixed.
double kappa
Bias to the diagonal of the covariance of the color statistics of a single pixel. Used to avoid singu...
vpMatrix imgPoints
Normal vector.
vpMatrix mean_vic
Vicinity data.
vpMatrix nv
Covariance.
void reinit(int resolution, unsigned normalPointsNumber)
Whether this pixel is the object.
vpMatrix weight
Img pixels.
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ ioError
I/O error.
Definition: vpException.h:67
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:73
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
All the data related to a single tracking frame. This contains both the input data (from a real camer...
A base class for all features that can be used and tracked in the vpRBTracker.
virtual void loadJsonConfiguration(const nlohmann::json &j)
Tracking based on the Contracting Curve Density algorithm.
void setTemporalSmoothingFactor(double factor)
Sets the temporal smoothing factor.
bool requiresSilhouetteCandidates() const VP_OVERRIDE
Whether this tracker requires Silhouette candidates.
vpRobust m_robust
Silhouette points where to compute CCD statistics.
void setCCDParameters(const vpCCDParameters &parameters)
double getVVSTrackerWeight() const VP_OVERRIDE
Get the importance of this tracker in the optimization step. The default computation is the following...
bool requiresDepth() const VP_OVERRIDE
Whether this tracker requires depth image to extract features.
bool requiresRGB() const VP_OVERRIDE
Whether this tracker requires RGB image to extract features.
double getTemporalSmoothingFactor() const
Returns the amount of temporal smoothing applied when computing the tracking error and its jacobian....
vpRBSilhouetteCCDDisplayType m_displayType
void trackFeatures(const vpRBFeatureTrackerInput &, const vpRBFeatureTrackerInput &, const vpHomogeneousMatrix &) VP_OVERRIDE
Track the features.
void setMinimumMaskConfidence(float confidence)
void onTrackingIterStart() VP_OVERRIDE
Method called when starting a tracking iteration.
virtual void loadJsonConfiguration(const nlohmann::json &j) VP_OVERRIDE
void setDisplayType(vpRBSilhouetteCCDDisplayType type)
void onTrackingIterEnd() VP_OVERRIDE
Method called after the tracking iteration has finished.
std::vector< vpColVector > m_gradients
void updateCovariance(const double) VP_OVERRIDE
Update the covariance matrix.
double m_temporalSmoothingFac
Sum of local hessians.
std::vector< vpRBSilhouetteControlPoint > m_controlPoints
bool shouldUseMask() const
Returns whether the tracking algorithm should filter out points that are unlikely to be on the object...
vpMatrix m_hessian
Sum of local gradients.
std::vector< vpMatrix > m_hessians
bool m_useMask
Smoothing factor used to integrate data from the previous frame.
virtual ~vpRBSilhouetteCCDTracker()=default
float getMinimumMaskConfidence() const
Returns the minimum mask gradient required for a silhouette point to be considered.
vpCCDParameters getCCDParameters() const
Trackable silhouette point representation.
Contains an M-estimator and various influence function.
Definition: vpRobust.h:84