Visual Servoing Platform  version 3.6.1 under development (2025-03-14)
vpRBDenseDepthTracker.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 
36 #ifndef VP_RB_DENSE_DEPTH_TRACKER_H
37 #define VP_RB_DENSE_DEPTH_TRACKER_H
38 
39 #include <visp3/core/vpConfig.h>
40 #include <visp3/core/vpImage.h>
41 #include <visp3/core/vpCameraParameters.h>
42 #include <visp3/core/vpPixelMeterConversion.h>
43 #include <visp3/core/vpRobust.h>
44 #include <visp3/core/vpPoint.h>
45 #include <visp3/core/vpHomogeneousMatrix.h>
46 
47 // #if defined(VISP_HAVE_SIMDLIB)
48 // #include <Simd/SimdLib.h>
49 // #endif
50 #include <visp3/rbt/vpRBFeatureTracker.h>
51 
52 #include <vector>
53 #include <iostream>
54 #include <algorithm>
55 
56 #if defined(VISP_HAVE_NLOHMANN_JSON)
57 #include VISP_NLOHMANN_JSON(json.hpp)
58 #endif
59 
60 BEGIN_VISP_NAMESPACE
67 class VISP_EXPORT vpRBDenseDepthTracker : public vpRBFeatureTracker
68 {
69 public:
70 
71  vpRBDenseDepthTracker() : vpRBFeatureTracker(), m_step(2), m_useMask(false), m_minMaskConfidence(0.f) { }
72 
73  virtual ~vpRBDenseDepthTracker() = default;
74 
75  bool requiresRGB() const VP_OVERRIDE { return false; }
76  bool requiresDepth() const VP_OVERRIDE { return true; }
77  bool requiresSilhouetteCandidates() const VP_OVERRIDE { return false; }
78 
83  unsigned int getStep() const { return m_step; }
84  void setStep(unsigned int step)
85  {
86  if (step == 0) {
87  throw vpException(vpException::badValue, "Step should be greater than 0");
88  }
89  m_step = step;
90  }
91 
96  bool shouldUseMask() const { return m_useMask; }
97  void setShouldUseMask(bool useMask) { m_useMask = useMask; }
98 
104  float getMinimumMaskConfidence() const { return m_minMaskConfidence; }
105  void setMinimumMaskConfidence(float confidence)
106  {
107  if (confidence > 1.f || confidence < 0.f) {
108  throw vpException(vpException::badValue, "Mask confidence should be between 0 and 1");
109  }
110  m_minMaskConfidence = confidence;
111  }
112 
121  void onTrackingIterStart() VP_OVERRIDE { }
122  void onTrackingIterEnd() VP_OVERRIDE { }
123 
124  double getVVSTrackerWeight() const VP_OVERRIDE { return m_userVvsWeight / (m_error.size()); }
125 
126  void extractFeatures(const vpRBFeatureTrackerInput &frame, const vpRBFeatureTrackerInput &previousFrame, const vpHomogeneousMatrix &cMo) VP_OVERRIDE;
127  void trackFeatures(const vpRBFeatureTrackerInput &/*frame*/, const vpRBFeatureTrackerInput &/*previousFrame*/, const vpHomogeneousMatrix &/*cMo*/) VP_OVERRIDE { }
128  void initVVS(const vpRBFeatureTrackerInput &/*frame*/, const vpRBFeatureTrackerInput &/*previousFrame*/, const vpHomogeneousMatrix &/*cMo*/) VP_OVERRIDE { }
129  void computeVVSIter(const vpRBFeatureTrackerInput &frame, const vpHomogeneousMatrix &cMo, unsigned int iteration) VP_OVERRIDE;
130 
131  void display(const vpCameraParameters &cam, const vpImage<unsigned char> &I, const vpImage<vpRGBa> &IRGB, const vpImage<unsigned char> &depth) const VP_OVERRIDE;
132 
134  {
135  vpDepthPoint() : currentPoint(3), cameraNormal(3), objectNormal(3)
136  { }
137 
138  inline void update(const vpHomogeneousMatrix &cMo, const vpRotationMatrix &cRo)
139  {
140  oP.changeFrame(cMo);
141  oP.projection();
142  cameraNormal = cRo * objectNormal;
143  // f.buildFrom(oP.get_x(), oP.get_y(), oP.get_Z(), log(oP.get_Z() / currentPoint.get_Z()));
144  }
145 
146  inline void error(vpColVector &e, unsigned i) const
147  {
148  double D = -((cameraNormal[0] * oP.get_X()) + (cameraNormal[1] * oP.get_Y()) + (cameraNormal[2] * oP.get_Z()));
149  double projNormal = cameraNormal[0] * currentPoint[0] + cameraNormal[1] * currentPoint[1] + cameraNormal[2] * currentPoint[2];
150 
151  e[i] = D + projNormal;
152  //e[i] = f.get_LogZoverZstar();
153  }
154 
155  inline void interaction(vpMatrix &L, unsigned i)
156  {
157  const double X = currentPoint[0], Y = currentPoint[1], Z = currentPoint[2];
158  const double nx = cameraNormal[0], ny = cameraNormal[1], nz = cameraNormal[2];
159  L[i][0] = nx;
160  L[i][1] = ny;
161  L[i][2] = nz;
162  L[i][3] = nz * Y - ny * Z;
163  L[i][4] = nx * Z - nz * X;
164  L[i][5] = ny * X - nx * Y;
165  // vpMatrix LL = f.interaction();
166  // for (unsigned int j = 0; j < 6; ++j) {
167  // L[i][j] = LL[0][j];
168  // }
169  }
170 
171  public:
177  //vpFeatureDepth f;
178  };
179 
180 #if defined(VISP_HAVE_NLOHMANN_JSON)
181  virtual void loadJsonConfiguration(const nlohmann::json &j) VP_OVERRIDE
182  {
184  setStep(j.value("step", m_step));
185  setShouldUseMask(j.value("useMask", m_useMask));
186  setMinimumMaskConfidence(j.value("minMaskConfidence", m_minMaskConfidence));
187  }
188 
189 #endif
190 
191 protected:
192 
193  std::vector<vpDepthPoint> m_depthPoints;
195  unsigned int m_step;
196  bool m_useMask;
198 };
199 
200 END_VISP_NAMESPACE
201 
202 #endif
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
@ 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.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
A tracker based on dense depth point-plane alignment.
bool requiresSilhouetteCandidates() const VP_OVERRIDE
Whether this tracker requires Silhouette candidates.
void setMinimumMaskConfidence(float confidence)
void initVVS(const vpRBFeatureTrackerInput &, const vpRBFeatureTrackerInput &, const vpHomogeneousMatrix &) VP_OVERRIDE
void trackFeatures(const vpRBFeatureTrackerInput &, const vpRBFeatureTrackerInput &, const vpHomogeneousMatrix &) VP_OVERRIDE
Track the features.
bool requiresRGB() const VP_OVERRIDE
Whether this tracker requires RGB image to extract features.
virtual void loadJsonConfiguration(const nlohmann::json &j) VP_OVERRIDE
unsigned int getStep() const
void setStep(unsigned int step)
double getVVSTrackerWeight() const VP_OVERRIDE
Get the importance of this tracker in the optimization step. The default computation is the following...
bool shouldUseMask() const
Returns whether the tracking algorithm should filter out points that are unlikely to be on the object...
bool requiresDepth() const VP_OVERRIDE
Whether this tracker requires depth image to extract features.
std::vector< vpDepthPoint > m_depthPoints
void onTrackingIterEnd() VP_OVERRIDE
Method called after the tracking iteration has finished.
float getMinimumMaskConfidence() const
Returns the minimum mask confidence that a pixel linked to depth point should have if it should be ke...
void setShouldUseMask(bool useMask)
void onTrackingIterStart() VP_OVERRIDE
Method called when starting a tracking iteration.
virtual ~vpRBDenseDepthTracker()=default
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 extractFeatures(const vpRBFeatureTrackerInput &frame, const vpRBFeatureTrackerInput &previousFrame, const vpHomogeneousMatrix &cMo)=0
Extract features from the frame data and the current pose estimate.
virtual void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpImage< vpRGBa > &IRGB, const vpImage< unsigned char > &depth) const =0
virtual void loadJsonConfiguration(const nlohmann::json &j)
virtual void computeVVSIter(const vpRBFeatureTrackerInput &frame, const vpHomogeneousMatrix &cMo, unsigned int iteration)=0
Contains an M-estimator and various influence function.
Definition: vpRobust.h:84
Implementation of a rotation matrix and operations on such kind of matrices.
void update(const vpHomogeneousMatrix &cMo, const vpRotationMatrix &cRo)
void error(vpColVector &e, unsigned i) const
void interaction(vpMatrix &L, unsigned i)