Point Cloud Library (PCL)  1.9.1-dev
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  int height = static_cast<int> (output.height);
59  int width = static_cast<int> (output.width);
60  for (int y = 0; y < height; ++y)
61  for (int x = 0; x < width; ++x)
62  if (pcl::isFinite ((*input_)(x, y)))
63  {
64  std::vector<float> vals;
65  vals.reserve (window_size_ * window_size_);
66  // Fill in the vector of values with the depths around the interest point
67  for (int y_dev = -window_size_/2; y_dev <= window_size_/2; ++y_dev)
68  for (int x_dev = -window_size_/2; x_dev <= window_size_/2; ++x_dev)
69  {
70  if (x + x_dev >= 0 && x + x_dev < width &&
71  y + y_dev >= 0 && y + y_dev < height &&
72  pcl::isFinite ((*input_)(x+x_dev, y+y_dev)))
73  vals.push_back ((*input_)(x+x_dev, y+y_dev).z);
74  }
75 
76  if (vals.empty ())
77  continue;
78 
79  // The output depth will be the median of all the depths in the window
80  partial_sort (vals.begin (), vals.begin () + vals.size () / 2 + 1, vals.end ());
81  float new_depth = vals[vals.size () / 2];
82  // Do not allow points to move more than the set max_allowed_movement_
83  if (std::abs (new_depth - (*input_)(x, y).z) < max_allowed_movement_)
84  output (x, y).z = new_depth;
85  else
86  output (x, y).z = (*input_)(x, y).z +
87  max_allowed_movement_ * (new_depth - (*input_)(x, y).z) / std::abs (new_depth - (*input_)(x, y).z);
88  }
89 }
90 
91 
92 #endif /* PCL_FILTERS_IMPL_MEDIAN_FILTER_HPP_ */
bool isFinite(const PointT &pt)
Tests if the 3D components of a point are all finite param[in] pt point to be tested return true if f...
Definition: point_tests.h:53
void applyFilter(PointCloud &output) override
Filter the input data and store the results into output.
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.
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
PointCloud represents the base class in PCL for storing collections of 3D points. ...