Point Cloud Library (PCL)  1.9.1-dev
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  Eigen::MatrixXf val_exp_depth_matrix;
92  Eigen::VectorXf val_exp_rgb_vector;
93  computeDistances (val_exp_depth_matrix, val_exp_rgb_vector);
94 
95  for (int x = 0; x < static_cast<int> (input_->width); ++x)
96  for (int y = 0; y < static_cast<int> (input_->height); ++y)
97  {
98  int start_window_x = std::max (x - window_size_, 0),
99  start_window_y = std::max (y - window_size_, 0),
100  end_window_x = std::min (x + window_size_, static_cast<int> (input_->width)),
101  end_window_y = std::min (y + window_size_, static_cast<int> (input_->height));
102 
103  float sum = 0.0f,
104  norm_sum = 0.0f;
105 
106  for (int x_w = start_window_x; x_w < end_window_x; ++ x_w)
107  for (int y_w = start_window_y; y_w < end_window_y; ++ y_w)
108  {
109  float val_exp_depth = val_exp_depth_matrix (static_cast<Eigen::MatrixXf::Index> (x - x_w + window_size_),
110  static_cast<Eigen::MatrixXf::Index> (y - y_w + window_size_));
111 
112  Eigen::VectorXf::Index d_color = static_cast<Eigen::VectorXf::Index> (
113  std::abs (input_->points[y_w * input_->width + x_w].r - input_->points[y * input_->width + x].r) +
114  std::abs (input_->points[y_w * input_->width + x_w].g - input_->points[y * input_->width + x].g) +
115  std::abs (input_->points[y_w * input_->width + x_w].b - input_->points[y * input_->width + x].b));
116 
117  float val_exp_rgb = val_exp_rgb_vector (d_color);
118 
119  if (std::isfinite (input_->points[y_w*input_->width + x_w].z))
120  {
121  sum += val_exp_depth * val_exp_rgb * input_->points[y_w*input_->width + x_w].z;
122  norm_sum += val_exp_depth * val_exp_rgb;
123  }
124  }
125 
126  output.points[y*input_->width + x].r = input_->points[y*input_->width + x].r;
127  output.points[y*input_->width + x].g = input_->points[y*input_->width + x].g;
128  output.points[y*input_->width + x].b = input_->points[y*input_->width + x].b;
129 
130  if (norm_sum != 0.0f)
131  {
132  float depth = sum / norm_sum;
133  Eigen::Vector3f pc (static_cast<float> (x) * depth, static_cast<float> (y) * depth, depth);
134  Eigen::Vector3f pw (unprojection_matrix_ * pc);
135  output.points[y*input_->width + x].x = pw[0];
136  output.points[y*input_->width + x].y = pw[1];
137  output.points[y*input_->width + x].z = pw[2];
138  }
139  else
140  {
141  output.points[y*input_->width + x].x = nan;
142  output.points[y*input_->width + x].y = nan;
143  output.points[y*input_->width + x].z = nan;
144  }
145  }
146 
147  output.header = input_->header;
148  output.width = input_->width;
149  output.height = input_->height;
150 }
151 
152 
153 template <typename PointInT, typename PointOutT> void
154 pcl::BilateralUpsampling<PointInT, PointOutT>::computeDistances (Eigen::MatrixXf &val_exp_depth, Eigen::VectorXf &val_exp_rgb)
155 {
156  val_exp_depth.resize (2*window_size_+1,2*window_size_+1);
157  val_exp_rgb.resize (3*255+1);
158 
159  int j = 0;
160  for (int dx = -window_size_; dx < window_size_+1; ++dx)
161  {
162  int i = 0;
163  for (int dy = -window_size_; dy < window_size_+1; ++dy)
164  {
165  float val_exp = std::exp (- (dx*dx + dy*dy) / (2.0f * static_cast<float> (sigma_depth_ * sigma_depth_)));
166  val_exp_depth(i,j) = val_exp;
167  i++;
168  }
169  j++;
170  }
171 
172  for (int d_color = 0; d_color < 3*255+1; d_color++)
173  {
174  float val_exp = std::exp (- d_color * d_color / (2.0f * sigma_color_ * sigma_color_));
175  val_exp_rgb(d_color) = val_exp;
176  }
177 }
178 
179 
180 #define PCL_INSTANTIATE_BilateralUpsampling(T,OutT) template class PCL_EXPORTS pcl::BilateralUpsampling<T,OutT>;
181 
182 
183 #endif /* PCL_SURFACE_IMPL_BILATERAL_UPSAMPLING_H_ */
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:423
void process(pcl::PointCloud< PointOutT > &output) override
Method that does the actual processing on the input cloud.
void performProcessing(pcl::PointCloud< PointOutT > &output) override
Abstract cloud processing method.
uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:428
uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:426
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:420
void resize(size_t n)
Resize the cloud.
Definition: point_cloud.h:468
void computeDistances(Eigen::MatrixXf &val_exp_depth, Eigen::VectorXf &val_exp_rgb)
Computes the distance for depth and RGB.