Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bilateral_upsampling.hpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009-2012, 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  * $Id$
37  *
38  */
39 
40 
41 #ifndef PCL_SURFACE_IMPL_BILATERAL_UPSAMPLING_H_
42 #define PCL_SURFACE_IMPL_BILATERAL_UPSAMPLING_H_
43 
45 #include <algorithm>
46 
48 template <typename PointInT, typename PointOutT> void
50 {
51  // Copy the header
52  output.header = input_->header;
53 
54  if (!initCompute ())
55  {
56  output.width = output.height = 0;
57  output.points.clear ();
58  return;
59  }
60 
61  if (input_->isOrganized () == false)
62  {
63  PCL_ERROR ("Input cloud is not organized.\n");
64  return;
65  }
66 
67  // Invert projection matrix
68  unprojection_matrix_ = projection_matrix_.inverse ();
69 
70  for (int i = 0; i < 3; ++i)
71  {
72  for (int j = 0; j < 3; ++j)
73  printf ("%f ", unprojection_matrix_(i, j));
74 
75  printf ("\n");
76  }
77 
78 
79  // Perform the actual surface reconstruction
80  performProcessing (output);
81 
82  deinitCompute ();
83 }
84 
86 template <typename PointInT, typename PointOutT> void
88 {
89  output.resize (input_->size ());
90  float nan = std::numeric_limits<float>::quiet_NaN ();
91 
92 
93  for (int x = 0; x < static_cast<int> (input_->width); ++x)
94  for (int y = 0; y < static_cast<int> (input_->height); ++y)
95  {
96  int start_window_x = std::max (x - window_size_, 0),
97  start_window_y = std::max (y - window_size_, 0),
98  end_window_x = std::min (x + window_size_, static_cast<int> (input_->width)),
99  end_window_y = std::min (y + window_size_, static_cast<int> (input_->height));
100 
101  float sum = 0.0f,
102  norm_sum = 0.0f;
103 
104  for (int x_w = start_window_x; x_w < end_window_x; ++ x_w)
105  for (int y_w = start_window_y; y_w < end_window_y; ++ y_w)
106  {
107  float dx = float (x - x_w),
108  dy = float (y - y_w);
109 
110  float val_exp_depth = expf (- (dx*dx + dy*dy) / (2.0f * static_cast<float> (sigma_depth_ * sigma_depth_)));
111 
112  float d_color = static_cast<float> (
113  abs (input_->points[y_w * input_->width + x_w].r - input_->points[y * input_->width + x].r) +
114  abs (input_->points[y_w * input_->width + x_w].g - input_->points[y * input_->width + x].g) +
115  abs (input_->points[y_w * input_->width + x_w].b - input_->points[y * input_->width + x].b));
116  float val_exp_rgb = expf (- d_color * d_color / (2.0f * sigma_color_ * sigma_color_));
117 
118  if (pcl_isfinite (input_->points[y_w*input_->width + x_w].z))
119  {
120  sum += val_exp_depth * val_exp_rgb * input_->points[y_w*input_->width + x_w].z;
121  norm_sum += val_exp_depth * val_exp_rgb;
122  }
123  }
124 
125  output.points[y*input_->width + x].r = input_->points[y*input_->width + x].r;
126  output.points[y*input_->width + x].g = input_->points[y*input_->width + x].g;
127  output.points[y*input_->width + x].b = input_->points[y*input_->width + x].b;
128 
129  if (norm_sum != 0.0f)
130  {
131  float depth = sum / norm_sum;
132  Eigen::Vector3f pc (static_cast<float> (x) * depth, static_cast<float> (y) * depth, depth);
133  Eigen::Vector3f pw (unprojection_matrix_ * pc);
134  output.points[y*input_->width + x].x = pw[0];
135  output.points[y*input_->width + x].y = pw[1];
136  output.points[y*input_->width + x].z = pw[2];
137  }
138  else
139  {
140  output.points[y*input_->width + x].x = nan;
141  output.points[y*input_->width + x].y = nan;
142  output.points[y*input_->width + x].z = nan;
143  }
144  }
145 
146  output.header = input_->header;
147  output.width = input_->width;
148  output.height = input_->height;
149 }
150 
151 
152 
153 #define PCL_INSTANTIATE_BilateralUpsampling(T,OutT) template class PCL_EXPORTS pcl::BilateralUpsampling<T,OutT>;
154 
155 
156 #endif /* PCL_SURFACE_IMPL_BILATERAL_UPSAMPLING_H_ */