Point Cloud Library (PCL)  1.9.0-dev
occlusion_reasoning.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #ifndef PCL_RECOGNITION_OCCLUSION_REASONING_H_
38 #define PCL_RECOGNITION_OCCLUSION_REASONING_H_
39 
40 #include <pcl/common/common.h>
41 #include <pcl/common/transforms.h>
42 #include <pcl/common/io.h>
43 
44 namespace pcl
45 {
46 
47  namespace occlusion_reasoning
48  {
49  /**
50  * \brief Class to reason about occlusions
51  * \author Aitor Aldoma
52  */
53 
54  template<typename ModelT, typename SceneT>
55  class ZBuffering
56  {
57  private:
58  float f_;
59  int cx_, cy_;
60  float * depth_;
61 
62  public:
63 
64  ZBuffering ();
65  ZBuffering (int resx, int resy, float f);
66  ~ZBuffering ();
67  void
68  computeDepthMap (typename pcl::PointCloud<SceneT>::ConstPtr & scene, bool compute_focal = false, bool smooth = false, int wsize = 3);
69  void
70  filter (typename pcl::PointCloud<ModelT>::ConstPtr & model, typename pcl::PointCloud<ModelT>::Ptr & filtered, float thres = 0.01);
71  void filter (typename pcl::PointCloud<ModelT>::ConstPtr & model, std::vector<int> & indices, float thres = 0.01);
72  };
73 
74  template<typename ModelT, typename SceneT> typename pcl::PointCloud<ModelT>::Ptr
75  filter (typename pcl::PointCloud<SceneT>::ConstPtr & organized_cloud, typename pcl::PointCloud<ModelT>::ConstPtr & to_be_filtered, float f,
76  float threshold)
77  {
78  float cx = (static_cast<float> (organized_cloud->width) / 2.f - 0.5f);
79  float cy = (static_cast<float> (organized_cloud->height) / 2.f - 0.5f);
80  typename pcl::PointCloud<ModelT>::Ptr filtered (new pcl::PointCloud<ModelT> ());
81 
82  std::vector<int> indices_to_keep;
83  indices_to_keep.resize (to_be_filtered->points.size ());
84 
85  int keep = 0;
86  for (size_t i = 0; i < to_be_filtered->points.size (); i++)
87  {
88  float x = to_be_filtered->points[i].x;
89  float y = to_be_filtered->points[i].y;
90  float z = to_be_filtered->points[i].z;
91  int u = static_cast<int> (f * x / z + cx);
92  int v = static_cast<int> (f * y / z + cy);
93 
94  //Not out of bounds
95  if ((u >= static_cast<int> (organized_cloud->width)) || (v >= static_cast<int> (organized_cloud->height)) || (u < 0) || (v < 0))
96  continue;
97 
98  //Check for invalid depth
99  if (!pcl_isfinite (organized_cloud->at (u, v).x) || !pcl_isfinite (organized_cloud->at (u, v).y)
100  || !pcl_isfinite (organized_cloud->at (u, v).z))
101  continue;
102 
103  float z_oc = organized_cloud->at (u, v).z;
104 
105  //Check if point depth (distance to camera) is greater than the (u,v)
106  if ((z - z_oc) > threshold)
107  continue;
108 
109  indices_to_keep[keep] = static_cast<int> (i);
110  keep++;
111  }
112 
113  indices_to_keep.resize (keep);
114  pcl::copyPointCloud (*to_be_filtered, indices_to_keep, *filtered);
115  return filtered;
116  }
117 
118  template<typename ModelT, typename SceneT> typename pcl::PointCloud<ModelT>::Ptr
119  filter (typename pcl::PointCloud<SceneT>::Ptr & organized_cloud, typename pcl::PointCloud<ModelT>::Ptr & to_be_filtered, float f,
120  float threshold, bool check_invalid_depth = true)
121  {
122  float cx = (static_cast<float> (organized_cloud->width) / 2.f - 0.5f);
123  float cy = (static_cast<float> (organized_cloud->height) / 2.f - 0.5f);
124  typename pcl::PointCloud<ModelT>::Ptr filtered (new pcl::PointCloud<ModelT> ());
125 
126  std::vector<int> indices_to_keep;
127  indices_to_keep.resize (to_be_filtered->points.size ());
128 
129  int keep = 0;
130  for (size_t i = 0; i < to_be_filtered->points.size (); i++)
131  {
132  float x = to_be_filtered->points[i].x;
133  float y = to_be_filtered->points[i].y;
134  float z = to_be_filtered->points[i].z;
135  int u = static_cast<int> (f * x / z + cx);
136  int v = static_cast<int> (f * y / z + cy);
137 
138  //Not out of bounds
139  if ((u >= static_cast<int> (organized_cloud->width)) || (v >= static_cast<int> (organized_cloud->height)) || (u < 0) || (v < 0))
140  continue;
141 
142  //Check for invalid depth
143  if (check_invalid_depth)
144  {
145  if (!pcl_isfinite (organized_cloud->at (u, v).x) || !pcl_isfinite (organized_cloud->at (u, v).y)
146  || !pcl_isfinite (organized_cloud->at (u, v).z))
147  continue;
148  }
149 
150  float z_oc = organized_cloud->at (u, v).z;
151 
152  //Check if point depth (distance to camera) is greater than the (u,v)
153  if ((z - z_oc) > threshold)
154  continue;
155 
156  indices_to_keep[keep] = static_cast<int> (i);
157  keep++;
158  }
159 
160  indices_to_keep.resize (keep);
161  pcl::copyPointCloud (*to_be_filtered, indices_to_keep, *filtered);
162  return filtered;
163  }
164 
165  template<typename ModelT, typename SceneT> typename pcl::PointCloud<ModelT>::Ptr
166  getOccludedCloud (typename pcl::PointCloud<SceneT>::Ptr & organized_cloud, typename pcl::PointCloud<ModelT>::Ptr & to_be_filtered, float f,
167  float threshold, bool check_invalid_depth = true)
168  {
169  float cx = (static_cast<float> (organized_cloud->width) / 2.f - 0.5f);
170  float cy = (static_cast<float> (organized_cloud->height) / 2.f - 0.5f);
171  typename pcl::PointCloud<ModelT>::Ptr filtered (new pcl::PointCloud<ModelT> ());
172 
173  std::vector<int> indices_to_keep;
174  indices_to_keep.resize (to_be_filtered->points.size ());
175 
176  int keep = 0;
177  for (size_t i = 0; i < to_be_filtered->points.size (); i++)
178  {
179  float x = to_be_filtered->points[i].x;
180  float y = to_be_filtered->points[i].y;
181  float z = to_be_filtered->points[i].z;
182  int u = static_cast<int> (f * x / z + cx);
183  int v = static_cast<int> (f * y / z + cy);
184 
185  //Out of bounds
186  if ((u >= static_cast<int> (organized_cloud->width)) || (v >= static_cast<int> (organized_cloud->height)) || (u < 0) || (v < 0))
187  continue;
188 
189  //Check for invalid depth
190  if (check_invalid_depth)
191  {
192  if (!pcl_isfinite (organized_cloud->at (u, v).x) || !pcl_isfinite (organized_cloud->at (u, v).y)
193  || !pcl_isfinite (organized_cloud->at (u, v).z))
194  continue;
195  }
196 
197  float z_oc = organized_cloud->at (u, v).z;
198 
199  //Check if point depth (distance to camera) is greater than the (u,v)
200  if ((z - z_oc) > threshold)
201  {
202  indices_to_keep[keep] = static_cast<int> (i);
203  keep++;
204  }
205  }
206 
207  indices_to_keep.resize (keep);
208  pcl::copyPointCloud (*to_be_filtered, indices_to_keep, *filtered);
209  return filtered;
210  }
211  }
212 }
213 
214 #ifdef PCL_NO_PRECOMPILE
215 #include <pcl/recognition/impl/hv/occlusion_reasoning.hpp>
216 #endif
217 
218 #endif /* PCL_RECOGNITION_OCCLUSION_REASONING_H_ */
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:410
pcl::PointCloud< ModelT >::Ptr getOccludedCloud(typename pcl::PointCloud< SceneT >::Ptr &organized_cloud, typename pcl::PointCloud< ModelT >::Ptr &to_be_filtered, float f, float threshold, bool check_invalid_depth=true)
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
Class to reason about occlusions.
PCL_EXPORTS void copyPointCloud(const pcl::PCLPointCloud2 &cloud_in, const std::vector< int > &indices, pcl::PCLPointCloud2 &cloud_out)
Extract the indices of a given point cloud as a new point cloud.
void computeDepthMap(typename pcl::PointCloud< SceneT >::ConstPtr &scene, bool compute_focal=false, bool smooth=false, int wsize=3)
uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:415
void filter(typename pcl::PointCloud< ModelT >::ConstPtr &model, typename pcl::PointCloud< ModelT >::Ptr &filtered, float thres=0.01)
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:428
uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:413
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:429
const PointT & at(int column, int row) const
Obtain the point given by the (column, row) coordinates.
Definition: point_cloud.h:283