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