Visual Servoing Platform  version 3.6.1 under development (2025-03-17)
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
vpRBSilhouettePointsExtractionSettings.cpp
1 
2 /*
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  */
31 
32 #include <stdlib.h>
33 
34 #include <visp3/core/vpColVector.h>
35 #include <visp3/core/vpMath.h>
36 #include <visp3/core/vpCameraParameters.h>
37 #include <visp3/core/vpPixelMeterConversion.h>
38 #include <visp3/core/vpMeterPixelConversion.h>
39 
40 #include <visp3/rbt/vpRBSilhouettePointsExtractionSettings.h>
41 #include <visp3/rbt/vpRBSilhouettePoint.h>
42 
43 BEGIN_VISP_NAMESPACE
44 
46 {
47  m_depthThreshold = 0.1;
48  m_thresholdIsRelative = false;
49  m_preferPreviousPoints = false;
50  m_sampleStep = 5;
51  m_maxNumPoints = 0;
52  m_border = 10;
53 }
54 
56 {
57  *this = rend;
58 }
59 
61 {
62  m_depthThreshold = rend.m_depthThreshold;
63  m_thresholdIsRelative = rend.m_thresholdIsRelative;
64  m_sampleStep = rend.m_sampleStep;
65  m_maxNumPoints = rend.m_maxNumPoints;
66  m_preferPreviousPoints = rend.m_preferPreviousPoints;
67  m_border = rend.m_border;
68  return *this;
69 }
70 
71 std::vector<std::pair<unsigned int, unsigned int>> vpSilhouettePointsExtractionSettings::getSilhouetteCandidates(
72  const vpImage<unsigned char> &validSilhouette, const vpImage<float> &renderDepth,
73  const vpCameraParameters &cam, const vpHomogeneousMatrix &cTcp,
74  const std::vector<vpRBSilhouettePoint> &previousPoints, long randomSeed) const
75 {
76  const unsigned int rows = validSilhouette.getHeight();
77  const unsigned int cols = validSilhouette.getWidth();
78 
79  std::vector<std::pair<unsigned int, unsigned int>> finalCandidates;
80  std::vector<std::pair<unsigned int, unsigned int>> candidates;
81  if (m_maxNumPoints) {
82  finalCandidates.reserve(m_maxNumPoints);
83  candidates.reserve(m_maxNumPoints);
84  }
85  if (m_preferPreviousPoints) {
86  for (const vpRBSilhouettePoint &p: previousPoints) {
87  double x = 0.0, y = 0.0;
88  vpPixelMeterConversion::convertPoint(cam, p.j, p.i, x, y);
89  vpColVector cpX({ x * p.Z, y * p.Z, p.Z, 1.0 });
90  vpColVector cX = cTcp * cpX;
91  cX /= cX[3];
92  vpMeterPixelConversion::convertPoint(cam, cX[0] / cX[2], cX[1] / cX[2], x, y);
93 
94  unsigned nu = static_cast<unsigned int>(round(x)), nv = static_cast<unsigned int>(round(y));
95  if (nu > 0 && nv > 0 && nv < rows && nu < cols) {
96  if (validSilhouette[nv][nu] > 0 && fabs((renderDepth[nv][nu] / p.Z) - 1.0) < 0.01) {
97  finalCandidates.push_back(std::make_pair(nv, nu));
98  }
99  }
100  }
101  }
102  if (m_maxNumPoints > 0 && finalCandidates.size() >= static_cast<unsigned int>(m_maxNumPoints)) {
103  return finalCandidates;
104  }
105 
106  for (unsigned int n = m_border; n < rows - m_border; n += m_sampleStep) {
107  for (unsigned int m = m_border; m < cols - m_border; m += m_sampleStep) {
108  if (validSilhouette[n][m] > 0) {
109  candidates.push_back(std::make_pair(n, m));
110  }
111  }
112  }
113 
114  if (m_maxNumPoints > 0) {
115  vpUniRand random(randomSeed);
116  std::vector<size_t> indices(m_maxNumPoints - finalCandidates.size());
117  sampleWithoutReplacement(m_maxNumPoints - finalCandidates.size(), candidates.size(), indices, random);
118  for (unsigned int i = 0; i < indices.size(); ++i) {
119  finalCandidates.push_back(candidates[indices[i]]);
120  }
121  }
122  else {
123  for (unsigned int i = 0; i < candidates.size(); ++i) {
124  finalCandidates.push_back(candidates[i]);
125  }
126  }
127  return finalCandidates;
128 }
129 
130 END_VISP_NAMESPACE
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
Implementation of an homogeneous matrix and operations on such kind of matrices.
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Silhouette point simple candidate representation.
std::vector< std::pair< unsigned int, unsigned int > > getSilhouetteCandidates(const vpImage< unsigned char > &validSilhouette, const vpImage< float > &renderDepth, const vpCameraParameters &cam, const vpHomogeneousMatrix &cTcp, const std::vector< vpRBSilhouettePoint > &previousPoints, long randomSeed=41) const
const vpSilhouettePointsExtractionSettings & operator=(const vpSilhouettePointsExtractionSettings &rend)
Class for generating random numbers with uniform probability density.
Definition: vpUniRand.h:127