Point Cloud Library (PCL) 1.12.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#pragma once
41
42#include <pcl/filters/median_filter.h>
43#include <pcl/common/point_tests.h> // for pcl::isFinite
44
45template <typename PointT> void
47{
48 if (!input_->isOrganized ())
49 {
50 PCL_ERROR ("[pcl::MedianFilter] Input cloud needs to be organized\n");
51 return;
52 }
53
54 // Copy everything from the input cloud to the output cloud (takes care of all the fields)
55 copyPointCloud (*input_, output);
56
57 int height = static_cast<int> (output.height);
58 int width = static_cast<int> (output.width);
59 for (int y = 0; y < height; ++y)
60 for (int x = 0; x < width; ++x)
61 if (pcl::isFinite ((*input_)(x, y)))
62 {
63 std::vector<float> vals;
64 vals.reserve (window_size_ * window_size_);
65 // Fill in the vector of values with the depths around the interest point
66 for (int y_dev = -window_size_/2; y_dev <= window_size_/2; ++y_dev)
67 for (int x_dev = -window_size_/2; x_dev <= window_size_/2; ++x_dev)
68 {
69 if (x + x_dev >= 0 && x + x_dev < width &&
70 y + y_dev >= 0 && y + y_dev < height &&
71 pcl::isFinite ((*input_)(x+x_dev, y+y_dev)))
72 vals.push_back ((*input_)(x+x_dev, y+y_dev).z);
73 }
74
75 if (vals.empty ())
76 continue;
77
78 // The output depth will be the median of all the depths in the window
79 auto middle_it = vals.begin () + vals.size () / 2;
80 std::nth_element (vals.begin (), middle_it, vals.end ());
81 float new_depth = *middle_it;
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
void applyFilter(PointCloud &output) override
Filter the input data and store the results into output.
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:398
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:400
void copyPointCloud(const pcl::PointCloud< PointInT > &cloud_in, pcl::PointCloud< PointOutT > &cloud_out)
Copy all the fields from a given point cloud into a new point cloud.
Definition: io.hpp:144
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:55