DenseCRFSegmentationProcessor.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package ROBDEKON::ArmarXObjects::DenseCRFSegmentationProcessor
17  * @author Christoph Pohl ( christoph dot pohl at kit dot edu )
18  * @date 2019
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #ifndef _ARMARX_COMPONENT_ROBDEKON_DenseCRFSegmentationProcessor_H
24 #define _ARMARX_COMPONENT_ROBDEKON_DenseCRFSegmentationProcessor_H
25 
26 
31 
32 #include <ArmarXCore/interface/observers/ObserverInterface.h>
33 #include <RobotAPI/interface/visualization/DebugDrawerInterface.h>
34 
35 // RemoteGUI
37 #include <ArmarXGui/interface/RemoteGuiInterface.h>
41 
42 #include <VisionX/interface/components/DenseCRFSegmentationProcessorInterface.h>
48 
49 #include <pcl/point_types.h>
50 
51 #include <boost/graph/filtered_graph.hpp>
53 
54 #include "DenseGraphCRF.hpp"
55 #include "Common.h"
56 
57 ARMARX_CONFIG_STRUCT_DEFINE_ADAPT_CONFIGURE(armarx::densecrf::config, general,
58  (int, num_classes, AX_DESCRIPTION("Number of maximal object classes in an image"),
59  AX_DEFAULT(20), AX_MIN(1), AX_MAX(255), AX_NO_REMOTE_GUI()),
60  (float, ground_truth_prob, AX_DESCRIPTION(
61  "Number of maximal object classes in an image"), AX_DEFAULT(
62  0.5), AX_MIN(0), AX_MAX(1), AX_DECIMALS(2), AX_STEPS(100)),
63  (bool, use_vertex_only_graph, AX_DESCRIPTION(
64  "If true, the graph is not computed via a voxel grid and"
65  " therefore has no edges between the vertices. This also means the"
66  " curvature is not signed in this case."), AX_DEFAULT(false)),
67  (bool, use_approximate_voxels, AX_DESCRIPTION(
68  "If true & UseVertexOnlyGraph is true, the downsampling"
69  " uses the ApproximateVoxelGrid for downsampling instead of the normal"
70  "VoxelGrid"), AX_DEFAULT(false)),
71  (float, voxel_resolution, AX_DESCRIPTION(
72  "Resolution of the Voxels used as Vertices of the Graph"), AX_DEFAULT(
73  0.05), AX_MIN(0.01), AX_MAX(.02), AX_DECIMALS(3), AX_STEPS(100)),
74  (int, map_iterations, AX_DESCRIPTION(
75  "Number of iterations used for the map"), AX_DEFAULT(
76  5), AX_MIN(1), AX_MAX(100))
77 );
78 
79 ARMARX_CONFIG_STRUCT_DEFINE_ADAPT_CONFIGURE(armarx::densecrf::config, output,
80  (bool, provide_graph_pclouds, AX_DESCRIPTION(
81  "If true a segmentation for each timestep"
82  "is provided as result cloud"), AX_DEFAULT(false),
83  AX_NO_REMOTE_GUI()),
84  (bool, provide_confidence_pclouds, AX_DESCRIPTION(
85  "If true a confidence for each timestep"
86  "is provided as result cloud"), AX_DEFAULT(false),
87  AX_NO_REMOTE_GUI()),
88  (bool, colorize_confidence_pclouds, AX_DESCRIPTION(
89  "If true the confidence pclouds are colorized"), AX_DEFAULT(false),
90  AX_NO_REMOTE_GUI()));
91 
92 ARMARX_CONFIG_STRUCT_DEFINE_ADAPT_CONFIGURE(armarx::densecrf::config, edge_comp,
93  (bool, use_xyz,
94  AX_DESCRIPTION("If true xyz values are used for edge computation"),
95  AX_DEFAULT(true)),
96  (float, xyz_influence,
97  AX_DESCRIPTION("Weight for the influence of position in the edge computation"),
98  AX_DEFAULT(0), AX_MIN(-2), AX_MAX(2), AX_DECIMALS(1), AX_STEPS(100)),
99  (bool, use_normals,
100  AX_DESCRIPTION("If true normal values are used for edge computation"),
101  AX_DEFAULT(true)),
102  (float, normals_influence,
103  AX_DESCRIPTION("Weight for the influence of normals in the edge computation"),
104  AX_DEFAULT(0), AX_MIN(-2), AX_MAX(2), AX_DECIMALS(1), AX_STEPS(100)),
105  (bool, use_rgb,
106  AX_DESCRIPTION("If true RGB values are used for edge computation"),
107  AX_DEFAULT(true)),
108  (float, rgb_influence,
109  AX_DESCRIPTION("Weight for the influence of color in the edge computation"),
110  AX_DEFAULT(0), AX_MIN(-2), AX_MAX(2), AX_DECIMALS(1), AX_STEPS(100)),
111  (bool, use_curvature,
112  AX_DESCRIPTION("If true curvature values are used for edge computation"),
113  AX_DEFAULT(true)),
114  (float, curvature_influence,
115  AX_DESCRIPTION("Weight for the influence of curvature in the edge computation"),
116  AX_DEFAULT(0), AX_MIN(-2), AX_MAX(2), AX_DECIMALS(1), AX_STEPS(100)),
117  (bool, use_time,
118  AX_DESCRIPTION("If true time values are used for edge computation"),
119  AX_DEFAULT(true)),
120  (float, time_influence,
121  AX_DESCRIPTION("Weight for the influence of time in the edge computation"),
122  AX_DEFAULT(0), AX_MIN(-2), AX_MAX(2), AX_DECIMALS(1), AX_STEPS(100)),
123  (bool, use_combined,
124  AX_DESCRIPTION("If true the influence values are combined before they are given to DenseCRF"),
125  AX_DEFAULT(true)),
126  (float, potts_compatibilty,
127  AX_DESCRIPTION("Weight factor for the Potts Compatibility"),
128  AX_DEFAULT(10), AX_MIN(0.5), AX_MAX(50), AX_DECIMALS(1), AX_STEPS(200))
129 );
130 
131 ARMARX_CONFIG_STRUCT_DEFINE_ADAPT_CONFIGURE(armarx::densecrf::config, data,
132  (armarx::densecrf::config::general, general),
133  (armarx::densecrf::config::output, out),
134  (armarx::densecrf::config::edge_comp, edge)
135  );
136 namespace armarx
137 {
138 
139  /**
140  * @class DenseCRFSegmentationProcessorPropertyDefinitions
141  * @brief
142  */
144  {
145  public:
148  {
149  }
150  };
151 
152  /**
153  * @defgroup Component-DenseCRFSegmentationProcessor DenseCRFSegmentationProcessor
154  * @ingroup VisionX-Components
155  * A description of the component DenseCRFSegmentationProcessor.
156  *
157  * @class DenseCRFSegmentationProcessor
158  * @ingroup Component-DenseCRFSegmentationProcessor
159  * @brief Brief description of class DenseCRFSegmentationProcessor.
160  *
161  * Detailed description of class DenseCRFSegmentationProcessor.
162  */
163 
164  class DenseCRFSegmentationProcessor : virtual public DenseCRFSegmentationProcessorInterface,
165  virtual public visionx::PointCloudProcessor,
167  {
168  public:
169  typedef boost::graph_traits<Graph>::vertex_iterator VertexIterator;
170 
171  /**
172  * @see armarx::ManagedIceObject::getDefaultName()
173  */
174  std::string getDefaultName() const override
175  {
176  return "DenseCRFSegmentationProcessor";
177  }
178 
179  Vector5f getCurrentEdgeWeights(const Ice::Current& current) override;
180 
181  void setEdgeWeights(float xyz, float rgb, float normals, float curvature, float time, const Ice::Current& current) override;
182 
183  protected:
184  /**
185  * @see visionx::PointCloudProcessor::onInitPointCloudProcessor()
186  */
187  void onInitPointCloudProcessor() override;
188 
189  /**
190  * @see visionx::PointCloudProcessor::onConnectPointCloudProcessor()
191  */
192  void onConnectPointCloudProcessor() override;
193 
194  /**
195  * @see visionx::PointCloudProcessor::onDisconnectPointCloudProcessor()
196  */
197  void onDisconnectPointCloudProcessor() override;
198 
199  /**
200  * @see visionx::PointCloudProcessor::onExitPointCloudProcessor()
201  */
202  void onExitPointCloudProcessor() override;
203 
204  /**
205  * @see visionx::PointCloudProcessor::process()
206  */
207  void process() override;
208 
209  /**
210  * @see PropertyUser::createPropertyDefinitions()
211  */
213 
214 
215  private:
216  template <typename TimestampMap>
217  struct vertex_timestamp_unequal_filter
218  {
219  vertex_timestamp_unequal_filter() = default;
220 
221  vertex_timestamp_unequal_filter(TimestampMap tsm, double ts) : m_tsm(tsm), m_ts(ts)
222  {
223  }
224 
225  template <typename Vertex>
226  bool operator()(const Vertex& v) const
227  {
228  // keep all verticers that are not the one supplied to the constructor
229  double current_ts = get(m_tsm, v);
230  return abs(m_ts - current_ts) > 0.00001;
231  }
232 
233  TimestampMap m_tsm;
234  double m_ts{};
235  };
236 
237  template <typename TimestampMap>
238  struct vertex_timestamp_equal_filter
239  {
240  vertex_timestamp_equal_filter() = default;
241 
242  vertex_timestamp_equal_filter(TimestampMap tsm, double ts) : m_tsm(tsm), m_ts(ts)
243  {
244  }
245 
246  template <typename Vertex>
247  bool operator()(const Vertex& v) const
248  {
249  // keep all verticers that are not the one supplied to the constructor
250  double current_ts = get(m_tsm, v);
251  return abs(m_ts - current_ts) < 0.00001;
252  }
253 
254  TimestampMap m_tsm;
255  double m_ts;
256  };
257 
258 
259  double initial_time_;
260  GraphPtr current_graph_ptr_;
261  GraphWithTimestampPtr persistent_graph_ptr_;
262 
263  std::deque<Graph> graph_queue_;
264  std::deque<double> timestamp_queue_;
265 
266  void computeGraphUsingVoxelGrid(const PointCloudT::Ptr inputCloudPtr);
267 
268  void computeVertexOnlyGraph(pcl::PointCloud<PointT>::Ptr input_cloud_ptr);
269 
270  void generateRandomClassLabels();
271 
272  void relabelPointCloud(pcl::PointCloud<PointT>::Ptr input_cloud_ptr);
273 
274  // void computeRandomWalkerSegmentation();
275 
276  // void computeEdgeWeights();
277 
278  void updatePersistentGraph();
279 
280  void addGraphToPersistentGraph(Graph& graph);
281 
282  void addCurrentGraphToPersistentGraph();
283 
284  void removeGraphFromPersistentGraph(Graph& graph);
285 
286  void removeCurrentGraphFromPersistentGraph();
287 
288  void removeTimestampFromPersistentGraph(double ts);
289 
290  static void copyRGBLToRGBLNormal(PointCloudT& input_cloud, PointCloudWithNormalT& output_cloud);
291 
292  static void copyRGBLNormalToRGBL(PointCloudWithNormalT& input_cloud, PointCloudT& output_cloud);
293 
294  GraphWithTimestamp retrieveGraphFromPersistentGraph(double ts);
295 
296  GraphWithTimestamp retrieveCurrentGraphFromPersistentGraph();
297 
298  template <typename FilterT>
299  GraphWithTimestamp filterAndCopyPersistentGraph(TimestampMap tsm, FilterT filter)
300  {
301  ARMARX_TRACE;
302  typedef boost::filtered_graph<GraphWithTimestamp, boost::keep_all, FilterT> FilteredGraph;
303  typedef typename boost::graph_traits<FilteredGraph>::vertex_iterator FilteredVertexIterator;
304  FilteredGraph filtered_graph = boost::make_filtered_graph(*persistent_graph_ptr_, boost::keep_all(),
305  filter);
306  FilteredVertexIterator vi, v_end;
307  GraphWithTimestamp temp_graph;
308  TimestampMap temp_tsm = boost::get(boost::vertex_timestamp_t(), temp_graph.m_graph);
309  ConfidenceMap cm = boost::get(boost::vertex_confidence_t(), persistent_graph_ptr_->m_graph);
310  ConfidenceMap temp_cm = boost::get(boost::vertex_confidence_t(), temp_graph.m_graph);
311  int num_vertices = 0;
312  for (boost::tie(vi, v_end) = boost::vertices(filtered_graph); vi != v_end; ++vi)
313  {
314  num_vertices++;
315  VertexWTsId new_vertex = boost::add_vertex(temp_graph);
316  boost::put(temp_tsm, new_vertex, get(tsm, *vi));
317  boost::put(temp_cm, new_vertex, get(cm, *vi));
318  PointWithNormalT point = filtered_graph.m_g.m_graph.m_point_cloud->points[*vi];
319  temp_graph[new_vertex] = point;
320  }
321  // ARMARX_INFO << "Filtered Graph has " << num_vertices << " num_vertices and "
322  // << static_cast<int>(boost::num_vertices(temp_graph)) << " Vertices";
323  return temp_graph;
324  }
325 
326  pcl::PointCloud<pcl::PointXYZL>::ConstPtr selectRandomSeeds();
327 
328  Eigen::MatrixXf computeRandomUnaryEnergy(int num_points);
329 
330  void provideAllGraphs();
331  void provideAllConfidences();
332 
333  WriteBufferedTripleBuffer<armarx::densecrf::config::data> config_;
334  std::mutex write_mutex_;
335  };
336 }
337 
338 #endif
armarx::PointCloudWithNormalT
pcl::PointCloud< PointWithNormalT > PointCloudWithNormalT
Definition: Common.h:31
armarx::VertexWTsId
boost::graph_traits< GraphWithTimestamp >::vertex_descriptor VertexWTsId
Definition: Common.h:65
armarx::DenseCRFSegmentationProcessor::getCurrentEdgeWeights
Vector5f getCurrentEdgeWeights(const Ice::Current &current) override
Definition: DenseCRFSegmentationProcessor.cpp:742
boost::vertex_timestamp_t
Definition: Common.h:15
armarx::DenseCRFSegmentationProcessor::onInitPointCloudProcessor
void onInitPointCloudProcessor() override
Definition: DenseCRFSegmentationProcessor.cpp:60
armarx::DenseCRFSegmentationProcessor
Brief description of class DenseCRFSegmentationProcessor.
Definition: DenseCRFSegmentationProcessor.h:164
armarx::DenseCRFSegmentationProcessorPropertyDefinitions::DenseCRFSegmentationProcessorPropertyDefinitions
DenseCRFSegmentationProcessorPropertyDefinitions(std::string prefix)
Definition: DenseCRFSegmentationProcessor.h:146
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
armarx::DenseCRFSegmentationProcessor::getDefaultName
std::string getDefaultName() const override
Definition: DenseCRFSegmentationProcessor.h:174
armarx::ConfidenceMap
boost::property_map< CloudGraphWithTimestamp, boost::vertex_confidence_t >::type ConfidenceMap
Definition: Common.h:71
armarx::DenseCRFSegmentationProcessor::onExitPointCloudProcessor
void onExitPointCloudProcessor() override
Definition: DenseCRFSegmentationProcessor.cpp:126
Common.h
WidgetBuilder.h
DenseGraphCRF.hpp
trace.h
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:333
armarx::DenseCRFSegmentationProcessor::onConnectPointCloudProcessor
void onConnectPointCloudProcessor() override
Definition: DenseCRFSegmentationProcessor.cpp:71
armarx::DenseCRFSegmentationProcessorPropertyDefinitions
Definition: DenseCRFSegmentationProcessor.h:143
visionx::PointCloudProcessorPropertyDefinitions::PointCloudProcessorPropertyDefinitions
PointCloudProcessorPropertyDefinitions(std::string prefix)
Definition: PointCloudProcessor.cpp:111
armarx::DenseCRFSegmentationProcessor::setEdgeWeights
void setEdgeWeights(float xyz, float rgb, float normals, float curvature, float time, const Ice::Current &current) override
Definition: DenseCRFSegmentationProcessor.cpp:750
armarx::GraphPtr
Agraph_t * GraphPtr
Definition: Layout.h:41
boost::vertex_confidence_t
Definition: Common.h:19
armarx::DenseCRFSegmentationProcessor::VertexIterator
boost::graph_traits< Graph >::vertex_iterator VertexIterator
Definition: DenseCRFSegmentationProcessor.h:169
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
voxel_grid_graph_builder.h
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:253
ARMARX_CONFIG_STRUCT_DEFINE_ADAPT_CONFIGURE
ARMARX_CONFIG_STRUCT_DEFINE_ADAPT_CONFIGURE(armarx::densecrf::config, general,(int, num_classes, AX_DESCRIPTION("Number of maximal object classes in an image"), AX_DEFAULT(20), AX_MIN(1), AX_MAX(255), AX_NO_REMOTE_GUI()),(float, ground_truth_prob, AX_DESCRIPTION("Number of maximal object classes in an image"), AX_DEFAULT(0.5), AX_MIN(0), AX_MAX(1), AX_DECIMALS(2), AX_STEPS(100)),(bool, use_vertex_only_graph, AX_DESCRIPTION("If true, the graph is not computed via a voxel grid and" " therefore has no edges between the vertices. This also means the" " curvature is not signed in this case."), AX_DEFAULT(false)),(bool, use_approximate_voxels, AX_DESCRIPTION("If true & UseVertexOnlyGraph is true, the downsampling" " uses the ApproximateVoxelGrid for downsampling instead of the normal" "VoxelGrid"), AX_DEFAULT(false)),(float, voxel_resolution, AX_DESCRIPTION("Resolution of the Voxels used as Vertices of the Graph"), AX_DEFAULT(0.05), AX_MIN(0.01), AX_MAX(.02), AX_DECIMALS(3), AX_STEPS(100)),(int, map_iterations, AX_DESCRIPTION("Number of iterations used for the map"), AX_DEFAULT(5), AX_MIN(1), AX_MAX(100)))
visionx::PointCloudProcessor
The PointCloudProcessor class provides an interface for access to PointCloudProviders via Ice and sha...
Definition: PointCloudProcessor.h:186
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
common.h
armarx::Graph
boost::subgraph< CloudGraph > Graph
Definition: Common.h:54
WidgetProxy.h
armarx::TimestampMap
boost::property_map< CloudGraphWithTimestamp, boost::vertex_timestamp_t >::type TimestampMap
Definition: Common.h:70
armarx::PointCloudT
pcl::PointCloud< PointT > PointCloudT
Definition: Common.h:30
visionx::PointCloudProcessorPropertyDefinitions
Properties of PointCloudProcessor.
Definition: PointCloudProcessor.h:173
PointCloudProcessor.h
TaskUtil.h
PluginAll.h
armarx::RemoteGuiComponentPluginUser
Definition: RemoteGuiComponentPlugin.h:240
Component.h
armarx::DenseCRFSegmentationProcessor::onDisconnectPointCloudProcessor
void onDisconnectPointCloudProcessor() override
Definition: DenseCRFSegmentationProcessor.cpp:122
TripleBuffer.h
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
IceUtil::Handle< class PropertyDefinitionContainer >
create_macro.h
armarx::DenseCRFSegmentationProcessor::process
void process() override
Definition: DenseCRFSegmentationProcessor.cpp:130
armarx::PointWithNormalT
pcl::PointXYZRGBLNormal PointWithNormalT
Definition: Common.h:29
PointXYZRGBLNormal.h
point_cloud_graph.h
RemoteGuiComponentPlugin.h
armarx::DenseCRFSegmentationProcessor::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: DenseCRFSegmentationProcessor.cpp:52
armarx::GraphWithTimestamp
boost::subgraph< CloudGraphWithTimestamp > GraphWithTimestamp
Definition: Common.h:55
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::GraphWithTimestampPtr
boost::shared_ptr< GraphWithTimestamp > GraphWithTimestampPtr
Definition: Common.h:60