Point Cloud Library (PCL)  1.9.1-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 #pragma once
38 
39 #include <pcl/common/common.h>
40 #include <pcl/common/transforms.h>
41 #include <pcl/common/io.h>
42 
43 namespace pcl
44 {
45 
46  namespace occlusion_reasoning
47  {
48  /**
49  * \brief Class to reason about occlusions
50  * \author Aitor Aldoma
51  */
52 
53  template<typename ModelT, typename SceneT>
54  class ZBuffering
55  {
56  private:
57  float f_;
58  int cx_, cy_;
59  float * depth_;
60 
61  public:
62 
63  ZBuffering ();
64  ZBuffering (int resx, int resy, float f);
65  ~ZBuffering ();
66  void
67  computeDepthMap (typename pcl::PointCloud<SceneT>::ConstPtr & scene, bool compute_focal = false, bool smooth = false, int wsize = 3);
68  void
69  filter (typename pcl::PointCloud<ModelT>::ConstPtr & model, typename pcl::PointCloud<ModelT>::Ptr & filtered, float thres = 0.01);
70  void filter (typename pcl::PointCloud<ModelT>::ConstPtr & model, std::vector<int> & indices, float thres = 0.01);
71  };
72 
73  template<typename ModelT, typename SceneT> typename pcl::PointCloud<ModelT>::Ptr
74  filter (typename pcl::PointCloud<SceneT>::ConstPtr & organized_cloud, typename pcl::PointCloud<ModelT>::ConstPtr & to_be_filtered, float f,
75  float threshold)
76  {
77  float cx = (static_cast<float> (organized_cloud->width) / 2.f - 0.5f);
78  float cy = (static_cast<float> (organized_cloud->height) / 2.f - 0.5f);
79  typename pcl::PointCloud<ModelT>::Ptr filtered (new pcl::PointCloud<ModelT> ());
80 
81  std::vector<int> indices_to_keep;
82  indices_to_keep.resize (to_be_filtered->points.size ());
83 
84  int keep = 0;
85  for (size_t i = 0; i < to_be_filtered->points.size (); i++)
86  {
87  float x = to_be_filtered->points[i].x;
88  float y = to_be_filtered->points[i].y;
89  float z = to_be_filtered->points[i].z;
90  int u = static_cast<int> (f * x / z + cx);
91  int v = static_cast<int> (f * y / z + cy);
92 
93  //Not out of bounds
94  if ((u >= static_cast<int> (organized_cloud->width)) || (v >= static_cast<int> (organized_cloud->height)) || (u < 0) || (v < 0))
95  continue;
96 
97  //Check for invalid depth
98  if (!std::isfinite (organized_cloud->at (u, v).x) || !std::isfinite (organized_cloud->at (u, v).y)
99  || !std::isfinite (organized_cloud->at (u, v).z))
100  continue;
101 
102  float z_oc = organized_cloud->at (u, v).z;
103 
104  //Check if point depth (distance to camera) is greater than the (u,v)
105  if ((z - z_oc) > threshold)
106  continue;
107 
108  indices_to_keep[keep] = static_cast<int> (i);
109  keep++;
110  }
111 
112  indices_to_keep.resize (keep);
113  pcl::copyPointCloud (*to_be_filtered, indices_to_keep, *filtered);
114  return filtered;
115  }
116 
117  template<typename ModelT, typename SceneT> typename pcl::PointCloud<ModelT>::Ptr
118  filter (typename pcl::PointCloud<SceneT>::Ptr & organized_cloud, typename pcl::PointCloud<ModelT>::Ptr & to_be_filtered, float f,
119  float threshold, bool check_invalid_depth = true)
120  {
121  float cx = (static_cast<float> (organized_cloud->width) / 2.f - 0.5f);
122  float cy = (static_cast<float> (organized_cloud->height) / 2.f - 0.5f);
123  typename pcl::PointCloud<ModelT>::Ptr filtered (new pcl::PointCloud<ModelT> ());
124 
125  std::vector<int> indices_to_keep;
126  indices_to_keep.resize (to_be_filtered->points.size ());
127 
128  int keep = 0;
129  for (size_t i = 0; i < to_be_filtered->points.size (); i++)
130  {
131  float x = to_be_filtered->points[i].x;
132  float y = to_be_filtered->points[i].y;
133  float z = to_be_filtered->points[i].z;
134  int u = static_cast<int> (f * x / z + cx);
135  int v = static_cast<int> (f * y / z + cy);
136 
137  //Not out of bounds
138  if ((u >= static_cast<int> (organized_cloud->width)) || (v >= static_cast<int> (organized_cloud->height)) || (u < 0) || (v < 0))
139  continue;
140 
141  //Check for invalid depth
142  if (check_invalid_depth)
143  {
144  if (!std::isfinite (organized_cloud->at (u, v).x) || !std::isfinite (organized_cloud->at (u, v).y)
145  || !std::isfinite (organized_cloud->at (u, v).z))
146  continue;
147  }
148 
149  float z_oc = organized_cloud->at (u, v).z;
150 
151  //Check if point depth (distance to camera) is greater than the (u,v)
152  if ((z - z_oc) > threshold)
153  continue;
154 
155  indices_to_keep[keep] = static_cast<int> (i);
156  keep++;
157  }
158 
159  indices_to_keep.resize (keep);
160  pcl::copyPointCloud (*to_be_filtered, indices_to_keep, *filtered);
161  return filtered;
162  }
163 
164  template<typename ModelT, typename SceneT> typename pcl::PointCloud<ModelT>::Ptr
165  getOccludedCloud (typename pcl::PointCloud<SceneT>::Ptr & organized_cloud, typename pcl::PointCloud<ModelT>::Ptr & to_be_filtered, float f,
166  float threshold, bool check_invalid_depth = true)
167  {
168  float cx = (static_cast<float> (organized_cloud->width) / 2.f - 0.5f);
169  float cy = (static_cast<float> (organized_cloud->height) / 2.f - 0.5f);
170  typename pcl::PointCloud<ModelT>::Ptr filtered (new pcl::PointCloud<ModelT> ());
171 
172  std::vector<int> indices_to_keep;
173  indices_to_keep.resize (to_be_filtered->points.size ());
174 
175  int keep = 0;
176  for (size_t i = 0; i < to_be_filtered->points.size (); i++)
177  {
178  float x = to_be_filtered->points[i].x;
179  float y = to_be_filtered->points[i].y;
180  float z = to_be_filtered->points[i].z;
181  int u = static_cast<int> (f * x / z + cx);
182  int v = static_cast<int> (f * y / z + cy);
183 
184  //Out of bounds
185  if ((u >= static_cast<int> (organized_cloud->width)) || (v >= static_cast<int> (organized_cloud->height)) || (u < 0) || (v < 0))
186  continue;
187 
188  //Check for invalid depth
189  if (check_invalid_depth)
190  {
191  if (!std::isfinite (organized_cloud->at (u, v).x) || !std::isfinite (organized_cloud->at (u, v).y)
192  || !std::isfinite (organized_cloud->at (u, v).z))
193  continue;
194  }
195 
196  float z_oc = organized_cloud->at (u, v).z;
197 
198  //Check if point depth (distance to camera) is greater than the (u,v)
199  if ((z - z_oc) > threshold)
200  {
201  indices_to_keep[keep] = static_cast<int> (i);
202  keep++;
203  }
204  }
205 
206  indices_to_keep.resize (keep);
207  pcl::copyPointCloud (*to_be_filtered, indices_to_keep, *filtered);
208  return filtered;
209  }
210  }
211 }
212 
213 #ifdef PCL_NO_PRECOMPILE
214 #include <pcl/recognition/impl/hv/occlusion_reasoning.hpp>
215 #endif
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:409
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:44
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:414
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:427
uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:412
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:428
const PointT & at(int column, int row) const
Obtain the point given by the (column, row) coordinates.
Definition: point_cloud.h:282