Point Cloud Library (PCL)  1.7.1
median_filter.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, 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 the copyright holder(s) 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 #ifndef PCL_FILTERS_IMPL_MEDIAN_FILTER_HPP_
41 #define PCL_FILTERS_IMPL_MEDIAN_FILTER_HPP_
42 
43 #include <pcl/filters/median_filter.h>
44 #include <pcl/common/io.h>
45 
46 template <typename PointT> void
48 {
49  if (!input_->isOrganized ())
50  {
51  PCL_ERROR ("[pcl::MedianFilter] Input cloud needs to be organized\n");
52  return;
53  }
54 
55  // Copy everything from the input cloud to the output cloud (takes care of all the fields)
56  copyPointCloud (*input_, output);
57 
58  for (int y = 0; y < output.height; ++y)
59  for (int x = 0; x < output.width; ++x)
60  if (pcl::isFinite ((*input_)(x, y)))
61  {
62  std::vector<float> vals;
63  vals.reserve (window_size_ * window_size_);
64  // Fill in the vector of values with the depths around the interest point
65  for (int y_dev = -window_size_/2; y_dev <= window_size_/2; ++y_dev)
66  for (int x_dev = -window_size_/2; x_dev <= window_size_/2; ++x_dev)
67  {
68  if (x + x_dev >= 0 && x + x_dev < output.width &&
69  y + y_dev >= 0 && y + y_dev < output.height &&
70  pcl::isFinite ((*input_)(x+x_dev, y+y_dev)))
71  vals.push_back ((*input_)(x+x_dev, y+y_dev).z);
72  }
73 
74  if (vals.size () == 0)
75  continue;
76 
77  // The output depth will be the median of all the depths in the window
78  partial_sort (vals.begin (), vals.begin () + vals.size () / 2 + 1, vals.end ());
79  float new_depth = vals[vals.size () / 2];
80  // Do not allow points to move more than the set max_allowed_movement_
81  if (fabs (new_depth - (*input_)(x, y).z) < max_allowed_movement_)
82  output (x, y).z = new_depth;
83  else
84  output (x, y).z = (*input_)(x, y).z +
85  max_allowed_movement_ * (new_depth - (*input_)(x, y).z) / fabsf (new_depth - (*input_)(x, y).z);
86  }
87 }
88 
89 
90 #endif /* PCL_FILTERS_IMPL_MEDIAN_FILTER_HPP_ */