Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
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#include <visp3/core/vpUniRand.h>
48
49// #if defined(VISP_HAVE_SIMDLIB)
50// #include <Simd/SimdLib.h>
51// #endif
52#include <visp3/rbt/vpRBSilhouetteControlPoint.h>
53#include <visp3/rbt/vpRBFeatureTracker.h>
54
55#include <vector>
56#include <iostream>
57#include <algorithm>
58
59#if defined(VISP_HAVE_NLOHMANN_JSON)
60#include VISP_NLOHMANN_JSON(json.hpp)
61#endif
62
64
65class VISP_EXPORT vpCCDParameters
66{
67public:
74
75 ~vpCCDParameters() = default;
80 double gamma_1;
85 double gamma_2;
90 double gamma_3;
95 double gamma_4;
96 double alpha;
97 double beta;
98
105 double kappa;
122 int h;
128 int min_h;
138};
139
140#if defined(VISP_HAVE_NLOHMANN_JSON)
141inline void from_json(const nlohmann::json &j, vpCCDParameters &ccdParameters)
142{
143 ccdParameters.alpha = j.value("alpha", ccdParameters.alpha);
144 ccdParameters.beta = j.value("beta", ccdParameters.beta);
145 ccdParameters.kappa = j.value("kappa", ccdParameters.kappa);
146 ccdParameters.covarianceIterDecreaseFactor = j.value("covarianceIterDecreaseFactor",
147 ccdParameters.covarianceIterDecreaseFactor);
148 ccdParameters.h = j.value("h", ccdParameters.h);
149 ccdParameters.start_h = ccdParameters.h;
150 ccdParameters.delta_h = j.value("delta_h", ccdParameters.delta_h);
151 ccdParameters.start_delta_h = ccdParameters.delta_h;
152 if (j.contains("min_h")) {
153 ccdParameters.min_h = j.value("min_h", ccdParameters.min_h);
154 }
155 else {
156 ccdParameters.min_h = ccdParameters.h;
157 }
158
159 ccdParameters.phi_dim = j.value("phi_dim", ccdParameters.phi_dim);
160 if (j.contains("gamma")) {
161 nlohmann::json gammaj = j["gamma"];
162 if (!gammaj.is_array() || gammaj.size() != 4) {
163 throw vpException(vpException::ioError, "CCD parameters: tried to read gamma values from something that is not a 4-sized float array");
164 }
165 ccdParameters.gamma_1 = gammaj[0];
166 ccdParameters.gamma_2 = gammaj[1];
167 ccdParameters.gamma_3 = gammaj[2];
168 ccdParameters.gamma_4 = gammaj[3];
169 }
170}
171#endif
172
173class VISP_EXPORT vpCCDStatistics
174{
175public:
181
182 void reinit(int resolution, unsigned normalPointsNumber)
183 {
184 nv.resize(resolution, 2, false, false);
185 mean_vic.resize(resolution, 6, false, false);
186 cov_vic.resize(resolution, 18, false, false);
187 vic.resize(resolution, 20 * normalPointsNumber, false, false);
188 imgPoints.resize(resolution, 2 * 3 * normalPointsNumber, false, false);
189 }
190 void zero()
191 {
192 nv = 0.0;
193 mean_vic = 0.0;
194 cov_vic = 0.0;
195 vic = 0.0;
196 imgPoints = 0.0;
197 }
198};
199
213{
214public:
215
224
226 virtual ~vpRBSilhouetteCCDTracker() = default;
227
228 bool requiresRGB() const VP_OVERRIDE { return true; }
229 bool requiresDepth() const VP_OVERRIDE { return false; }
230 bool requiresSilhouetteCandidates() const VP_OVERRIDE { return true; }
231
236 void setCCDParameters(const vpCCDParameters &parameters) { m_ccdParameters = parameters; }
238 //void computeMask(const vpImage<vpRGBa> &render, vpCCDStatistics &stats);
239
247
255 void setTemporalSmoothingFactor(double factor)
256 {
257 if (factor < 0.0) {
258 throw vpException(vpException::badValue, "Temporal smoothing factor should be equal to or greater than 0");
259 }
260 m_temporalSmoothingFac = factor;
261 }
262
268 bool shouldUseMask() const { return m_useMask; }
269 void setShouldUseMask(bool useMask) { m_useMask = useMask; }
270
277 void setMinimumMaskConfidence(double confidence)
278 {
279
280 m_minMaskConfidence = confidence;
281 }
282
287 unsigned int getMaxNumPoints() const { return m_maxPoints; }
288 void setMaxNumPoints(unsigned int maxPoints) { m_maxPoints = maxPoints; }
289
291 {
292 if (type == DT_INVALID) {
293 throw vpException(vpException::badValue, "CCD tracker display type is invalid");
294 }
295 m_displayType = type;
296 }
297
301
302 void onTrackingIterStart(const vpRBFeatureTrackerInput & /*frame*/, const vpHomogeneousMatrix & /*cMo*/) VP_OVERRIDE;
303 void onTrackingIterEnd(const vpHomogeneousMatrix & /*cMo*/) VP_OVERRIDE { }
304 void reset() VP_OVERRIDE
305 {
306 m_stats.zero();
307 m_prevStats.zero();
308 m_previousFrame = nullptr;
309 m_controlPoints.clear();
310 m_gradientData.clear();
311 m_hessianData.clear();
312 m_hessian.clear();
313 m_gradients.clear();
314 m_error = 0.0;
316 m_vvsConverged = false;
317 }
318
319 double getVVSTrackerWeight(double optimizationProgress) const VP_OVERRIDE
320 {
321 return m_weighting->weight(optimizationProgress) / (100 * m_error.size());
322 }
323
324 void extractFeatures(const vpRBFeatureTrackerInput &frame, const vpRBFeatureTrackerInput &previousFrame, const vpHomogeneousMatrix &cMo) VP_OVERRIDE;
325 void trackFeatures(const vpRBFeatureTrackerInput & /*frame*/, const vpRBFeatureTrackerInput & /*previousFrame*/, const vpHomogeneousMatrix & /*cMo*/) VP_OVERRIDE { }
326
327 void initVVS(const vpRBFeatureTrackerInput &frame, const vpRBFeatureTrackerInput &previousFrame, const vpHomogeneousMatrix &cMo) VP_OVERRIDE;
328 void computeVVSIter(const vpRBFeatureTrackerInput &frame, const vpHomogeneousMatrix &cMo, unsigned int iteration) VP_OVERRIDE;
329 void updateCovariance(const double /*lambda*/) VP_OVERRIDE
330 {
331 m_cov = m_sigma;
332 }
333
338 void buildGradientAndHessianStorageViews(unsigned int normalsPerPoint, bool clear);
342 void changeScale();
343
344 void display(const vpCameraParameters &cam, const vpImage<unsigned char> &I, const vpImage<vpRGBa> &IRGB, const vpImage<unsigned char> &depth) const VP_OVERRIDE;
345
346#if defined(VISP_HAVE_NLOHMANN_JSON)
347
348#if defined(__clang__)
349// Mute warning : declaration requires an exit-time destructor [-Wexit-time-destructors]
350// message : expanded from macro 'NLOHMANN_JSON_SERIALIZE_ENUM'
351# pragma clang diagnostic push
352# pragma clang diagnostic ignored "-Wexit-time-destructors"
353#endif
354
361 });
362 virtual void loadJsonConfiguration(const nlohmann::json &j) VP_OVERRIDE
363 {
365
366 m_vvsConvergenceThreshold = j.value("convergenceThreshold", m_vvsConvergenceThreshold);
367 setTemporalSmoothingFactor(j.value("temporalSmoothing", m_temporalSmoothingFac));
368 setShouldUseMask(j.value("useMask", m_useMask));
369 setMinimumMaskConfidence(j.value("minMaskConfidence", m_minMaskConfidence));
370 setMaxNumPoints(j.value("maxNumPoints", m_maxPoints));
371
372 setDisplayType(j.value("displayType", m_displayType));
373 m_ccdParameters = j.value("ccd", m_ccdParameters);
374 }
375
376#if defined(__clang__)
377# pragma clang diagnostic pop
378#endif
379
380#endif
381
382protected:
383 void updateCCDPoints(const vpHomogeneousMatrix &cMo);
384 void computeLocalStatistics(const vpImage<vpRGBa> &I, vpCCDStatistics &stats);
385 template<bool hasTemporalSmoothing>
386 void computeErrorAndInteractionMatrix(const vpHomogeneousMatrix &cMo);
387
389
390 std::vector<vpRBSilhouetteControlPoint> m_controlPoints;
392
395
397
399 double tol;
400
401 std::vector<double> m_gradientData;
402 std::vector<double> m_hessianData;
403
404 std::vector<vpColVector> m_gradients;
405 std::vector<vpMatrix> m_hessians;
409
412
413 unsigned int m_maxPoints;
414 static const unsigned int BASE_SEED;
416
419};
420
421END_VISP_NAMESPACE
422
423#endif
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)
Img pixels.
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
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.
Definition of the vpImage class member functions.
Definition vpImage.h:131
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
All the data related to a single tracking frame. This contains both the input data (from a real camer...
std::shared_ptr< vpTemporalWeighting > m_weighting
Number of considered features.
virtual void loadJsonConfiguration(const nlohmann::json &j)
bool m_vvsConverged
User-defined weight for this specific type of feature.
vpMatrix m_cov
Right side of the Gauss Newton minimization.
Tracking based on the Contracting Curve Density algorithm.
void setTemporalSmoothingFactor(double factor)
Sets the temporal smoothing factor.
const vpRBFeatureTrackerInput * m_previousFrame
unsigned int getMaxNumPoints() const
Get the maximum number of silhouette control points that will be used by the tracker at a given itera...
void onTrackingIterEnd(const vpHomogeneousMatrix &) VP_OVERRIDE
Method called after the tracking iteration has finished.
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)
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.
NLOHMANN_JSON_SERIALIZE_ENUM(vpRBSilhouetteCCDTracker::vpDisplayType, { {vpRBSilhouetteCCDTracker::vpDisplayType::DT_INVALID, nullptr}, {vpRBSilhouetteCCDTracker::vpDisplayType::DT_SIMPLE, "simple"}, {vpRBSilhouetteCCDTracker::vpDisplayType::DT_WEIGHT, "weight"}, {vpRBSilhouetteCCDTracker::vpDisplayType::DT_ERROR, "error"}, {vpRBSilhouetteCCDTracker::vpDisplayType::DT_WEIGHT_AND_ERROR, "weightAndError"} })
double getTemporalSmoothingFactor() const
Returns the amount of temporal smoothing applied when computing the tracking error and its jacobian....
void trackFeatures(const vpRBFeatureTrackerInput &, const vpRBFeatureTrackerInput &, const vpHomogeneousMatrix &) VP_OVERRIDE
Track the features.
void setDisplayType(vpDisplayType type)
double getMinimumMaskConfidence() const
Returns the minimum mask gradient required for a silhouette point to be considered.
virtual void loadJsonConfiguration(const nlohmann::json &j) VP_OVERRIDE
std::vector< double > m_hessianData
double getVVSTrackerWeight(double optimizationProgress) const VP_OVERRIDE
Get the importance of this tracker in the optimization step. The default computation is the following...
std::vector< vpColVector > m_gradients
std::vector< double > m_gradientData
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...
void setMaxNumPoints(unsigned int maxPoints)
static const unsigned int BASE_SEED
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
void setMinimumMaskConfidence(double confidence)
void reset() VP_OVERRIDE
Resets feature state. Can be called when the object changes or is lost, Ensuring that prior data does...
vpCCDParameters getCCDParameters() const
Contains an M-estimator and various influence function.
Definition vpRobust.h:84
Class for generating random numbers with uniform probability density.
Definition vpUniRand.h:127