Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
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
44
46{
47 m_depthThreshold = 0.1;
48 m_thresholdIsRelative = false;
49 m_preferPreviousPoints = false;
50 m_sampleStep = 1;
51 m_maxNumPoints = 0;
52 m_border = 2;
53}
54
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
71std::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 std::vector<std::pair<unsigned int, unsigned int>> finalCandidates;
79 std::vector<std::pair<unsigned int, unsigned int>> candidates;
80 if (m_maxNumPoints) {
81 finalCandidates.reserve(m_maxNumPoints);
82 candidates.reserve(m_maxNumPoints);
83 }
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 if (m_maxNumPoints > 0) {
114 vpUniRand random(randomSeed);
115 std::vector<size_t> indices(m_maxNumPoints - finalCandidates.size());
116 sampleWithoutReplacement(static_cast<size_t>(m_maxNumPoints) - finalCandidates.size(), candidates.size(), indices, random);
117 for (unsigned int i = 0; i < indices.size(); ++i) {
118 finalCandidates.push_back(candidates[indices[i]]);
119 }
120 }
121 else {
122 for (unsigned int i = 0; i < candidates.size(); ++i) {
123 finalCandidates.push_back(candidates[i]);
124 }
125 }
126 return finalCandidates;
127}
128
129END_VISP_NAMESPACE
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Implementation of an homogeneous matrix and operations on such kind of matrices.
Definition of the vpImage class member functions.
Definition vpImage.h:131
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