Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpDepthMask.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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
31#include <visp3/rbt/vpDepthMask.h>
32
33#include <visp3/rbt/vpRBFeatureTracker.h>
34
35#if defined(VISP_HAVE_NLOHMANN_JSON)
36#include VISP_NLOHMANN_JSON(json.hpp)
37#endif
38
40
42 const vpRBFeatureTrackerInput &/* previousFrame */,
43 vpImage<float> &mask)
44{
45 if (!frame.hasDepth()) {
46 throw vpException(vpException::badValue, "This segmentation method requires a depth image");
47 }
48
49
50 // const float radius = frame.renders.objectDiameter / 2.f;
51
52 const double radius = std::max(m_minRadiusFactor * (frame.renders.objectDiameter / 2.f), (frame.renders.zFar - frame.renders.zNear) / 2.f);
53 const double Zo = (frame.renders.cMo)[2][3];
54 const float Zmi = std::min(Zo - radius, frame.renders.zNear);
55 const float Zma = std::max(Zo + radius, frame.renders.zFar);
56
57 const float tol = radius * m_falloffSmoothingFactor;
58 const float fac = 1.f / (tol * sqrtf(2.f));
59
60
61 const auto getProba = [Zmi, Zma, fac](double Z) -> float {
62 // Missing depth value => Consider likely
63 if (Z == 0.f) {
64 return 1.f;
65 }
66 else {
67 // Depth values that are close enough to the object's center depth value are correct
68 if (Z > Zmi && Z < Zma) {
69 return 1.f;
70 }
71 // Otherwise, use a gradual fallof based on a Gaussian distribution with sigma based on tol
72 else {
73 if (Z <= Zmi) {
74 return erfc((Zmi - Z) * fac);
75 }
76 else {
77 return erfc((Z - Zma) * fac);
78 }
79 }
80 }};
81
82 if (!m_computeOnBBOnly) {
83 mask.resize(frame.depth.getHeight(), frame.depth.getWidth());
84#if defined(VISP_HAVE_OPENMP)
85#pragma omp parallel for
86#endif
87 for (int i = 0; i < static_cast<int>(frame.depth.getSize()); ++i) {
88 const float Z = frame.depth.bitmap[i];
89 mask.bitmap[i] = getProba(Z);
90 }
91 }
92 else {
93 mask.resize(frame.depth.getHeight(), frame.depth.getWidth(), 0.f);
94 const vpRect renderBB = frame.renders.boundingBox;
95 const int top = static_cast<int>(renderBB.getTop());
96 const int left = static_cast<int>(renderBB.getLeft());
97 const int bottom = std::min(static_cast<int>(frame.depth.getHeight()) - 1, static_cast<int>(renderBB.getBottom()));
98 const int right = std::min(static_cast<int>(frame.depth.getWidth()) - 1, static_cast<int>(renderBB.getRight()));
99#ifdef VISP_HAVE_OPENMP
100#pragma omp parallel for
101#endif
102 for (int i = top; i <= bottom; ++i) {
103 float *const maskRow = mask[i];
104 const float *const depthRow = frame.depth[i];
105 for (unsigned int j = left; j <= static_cast<unsigned int>(right); ++j) {
106 maskRow[j] = getProba(depthRow[j]);
107 }
108 }
109 }
110}
111
112#if defined(VISP_HAVE_NLOHMANN_JSON)
113void vpDepthMask::loadJsonConfiguration(const nlohmann::json &json)
114{
115 m_minRadiusFactor = json.value("minRadiusFactor", m_minRadiusFactor);
116 m_falloffSmoothingFactor = json.value("falloffRadiusFactor", m_falloffSmoothingFactor);
117 m_computeOnBBOnly = json.value("computeOnlyOnBoundingBox", m_computeOnBBOnly);
118}
119#endif
120
121END_VISP_NAMESPACE
void loadJsonConfiguration(const nlohmann::json &json) VP_OVERRIDE
void updateMask(const vpRBFeatureTrackerInput &frame, const vpRBFeatureTrackerInput &previousFrame, vpImage< float > &mask) VP_OVERRIDE
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
Definition of the vpImage class member functions.
Definition vpImage.h:131
unsigned int getWidth() const
Definition vpImage.h:242
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition vpImage.h:544
unsigned int getSize() const
Definition vpImage.h:221
Type * bitmap
points toward the bitmap
Definition vpImage.h:135
unsigned int getHeight() const
Definition vpImage.h:181
All the data related to a single tracking frame. This contains both the input data (from a real camer...
vpImage< float > depth
RGB image, 0 sized if RGB is not available.
vpRBRenderData renders
camera parameters
Defines a rectangle in the plane.
Definition vpRect.h:79
double getLeft() const
Definition vpRect.h:173
double getRight() const
Definition vpRect.h:179
double getBottom() const
Definition vpRect.h:97
double getTop() const
Definition vpRect.h:192
double zNear
Binary image indicating whether a given pixel is part of the silhouette.
vpHomogeneousMatrix cMo