40#ifndef PCL_SEGMENTATION_REGION_GROWING_RGB_HPP_
41#define PCL_SEGMENTATION_REGION_GROWING_RGB_HPP_
43#include <pcl/console/print.h>
44#include <pcl/segmentation/region_growing_rgb.h>
45#include <pcl/search/search.h>
46#include <pcl/search/kdtree.h>
51template <
typename Po
intT,
typename NormalT>
53 color_p2p_threshold_ (1225.0f),
54 color_r2r_threshold_ (10.0f),
55 distance_threshold_ (0.05f),
56 region_neighbour_number_ (100),
58 segment_neighbours_ (0),
59 segment_distances_ (0),
69template <
typename Po
intT,
typename NormalT>
72 point_distances_.clear ();
73 segment_neighbours_.clear ();
74 segment_distances_.clear ();
75 segment_labels_.clear ();
79template <
typename Po
intT,
typename NormalT>
float
82 return (
powf (color_p2p_threshold_, 0.5f));
86template <
typename Po
intT,
typename NormalT>
void
89 color_p2p_threshold_ = thresh * thresh;
93template <
typename Po
intT,
typename NormalT>
float
96 return (
powf (color_r2r_threshold_, 0.5f));
100template <
typename Po
intT,
typename NormalT>
void
103 color_r2r_threshold_ = thresh * thresh;
107template <
typename Po
intT,
typename NormalT>
float
110 return (
powf (distance_threshold_, 0.5f));
114template <
typename Po
intT,
typename NormalT>
void
117 distance_threshold_ = thresh * thresh;
121template <
typename Po
intT,
typename NormalT>
unsigned int
124 return (region_neighbour_number_);
128template <
typename Po
intT,
typename NormalT>
void
135template <
typename Po
intT,
typename NormalT>
bool
138 return (normal_flag_);
142template <
typename Po
intT,
typename NormalT>
void
145 normal_flag_ = value;
149template <
typename Po
intT,
typename NormalT>
void
152 curvature_flag_ = value;
156template <
typename Po
intT,
typename NormalT>
void
159 residual_flag_ = value;
163template <
typename Po
intT,
typename NormalT>
void
168 point_neighbours_.clear ();
169 point_labels_.clear ();
170 num_pts_in_segment_.clear ();
171 point_distances_.clear ();
172 segment_neighbours_.clear ();
173 segment_distances_.clear ();
174 segment_labels_.clear ();
175 number_of_segments_ = 0;
191 findPointNeighbours ();
192 applySmoothRegionGrowingAlgorithm ();
195 findSegmentNeighbours ();
196 applyRegionMergingAlgorithm ();
198 std::vector<pcl::PointIndices>::iterator
cluster_iter = clusters_.begin ();
210 clusters.reserve (clusters_.size ());
211 std::copy (clusters_.begin (), clusters_.end (), std::back_inserter (
clusters));
217template <
typename Po
intT,
typename NormalT>
bool
221 if ( input_->points.empty () )
229 if ( !normals_ || input_->size () != normals_->size () )
236 if (residual_threshold_ <= 0.0f)
248 if ( region_neighbour_number_ == 0 || color_p2p_threshold_ < 0.0f || color_r2r_threshold_ < 0.0f || distance_threshold_ < 0.0f )
252 if (neighbour_number_ == 0)
261 if (indices_->empty ())
262 PCL_ERROR (
"[pcl::RegionGrowingRGB::prepareForSegmentation] Empty given indices!\n");
263 search_->setInputCloud (input_, indices_);
266 search_->setInputCloud (input_);
272template <
typename Po
intT,
typename NormalT>
void
275 int point_number =
static_cast<int> (indices_->size ());
277 std::vector<float> distances;
279 point_neighbours_.resize (input_->size (),
neighbours);
280 point_distances_.resize (input_->size (), distances);
287 search_->nearestKSearch (
i_point, region_neighbour_number_,
neighbours, distances);
294template <
typename Po
intT,
typename NormalT>
void
298 std::vector<float> distances;
299 segment_neighbours_.resize (number_of_segments_,
neighbours);
300 segment_distances_.resize (number_of_segments_, distances);
305 std::vector<float>
dist;
313template <
typename Po
intT,
typename NormalT>
void
316 std::vector<float> distances;
317 float max_dist = std::numeric_limits<float>::max ();
318 distances.resize (clusters_.size (),
max_dist);
355 dist.resize (size, 0);
367template <
typename Po
intT,
typename NormalT>
void
372 std::vector<unsigned int>
color;
395 segment_labels_.resize (number_of_segments_, -1);
402 if (segment_labels_[
i_seg] == -1)
414 while (
i_nghbr < region_neighbour_number_ &&
i_nghbr < segment_neighbours_[
i_seg].size () )
422 if ( segment_labels_[index] == -1 )
440 std::vector<int> region;
451 int index = segment_labels_[
i_seg];
488 nghbr.first = std::numeric_limits<float>::max ();
510template <
typename Po
intT,
typename NormalT>
float
521template <
typename Po
intT,
typename NormalT>
void
534 std::pair<float, pcl::index_t> pair;
553template <
typename Po
intT,
typename NormalT>
void
569 index = segment_labels_[index];
575 if (clusters_.empty ())
578 std::vector<pcl::PointIndices>::iterator
itr1,
itr2;
579 itr1 = clusters_.begin ();
580 itr2 = clusters_.end () - 1;
593 if (
itr2->indices.empty ())
594 clusters_.erase (
itr2, clusters_.end ());
598template <
typename Po
intT,
typename NormalT>
bool
624 data[0] = (*input_)[point].data[0];
625 data[1] = (*input_)[point].data[1];
626 data[2] = (*input_)[point].data[2];
627 data[3] = (*input_)[point].data[3];
629 Eigen::Map<Eigen::Vector3f>
initial_point (
static_cast<float*
> (data));
630 Eigen::Map<Eigen::Vector3f>
initial_normal (
static_cast<float*
> ((*normals_)[point].normal));
631 if (smooth_mode_flag_ ==
true)
633 Eigen::Map<Eigen::Vector3f>
nghbr_normal (
static_cast<float*
> ((*normals_)[
nghbr].normal));
640 Eigen::Map<Eigen::Vector3f>
nghbr_normal (
static_cast<float*
> ((*normals_)[
nghbr].normal));
649 if (curvature_flag_ && (*normals_)[
nghbr].curvature > curvature_threshold_)
656 data_p[0] = (*input_)[point].data[0];
657 data_p[1] = (*input_)[point].data[1];
658 data_p[2] = (*input_)[point].data[2];
659 data_p[3] = (*input_)[point].data[3];
667 Eigen::Map<Eigen::Vector3f>
initial_normal (
static_cast<float*
> ((*normals_)[point].normal));
677template <
typename Po
intT,
typename NormalT>
void
691 for (
const auto& point : (*indices_))
700 if (clusters_.empty ())
703 point_neighbours_.clear ();
704 point_labels_.clear ();
705 num_pts_in_segment_.clear ();
706 point_distances_.clear ();
707 segment_neighbours_.clear ();
708 segment_distances_.clear ();
709 segment_labels_.clear ();
710 number_of_segments_ = 0;
719 findPointNeighbours ();
720 applySmoothRegionGrowingAlgorithm ();
723 findSegmentNeighbours ();
724 applyRegionMergingAlgorithm ();
Iterator class for point clouds with or without given indices.
std::size_t size() const
Size of the range the iterator is going through.
bool curvature_flag_
If set to true then curvature test will be done during segmentation.
bool normal_flag_
If set to true then normal/smoothness test will be done during segmentation.
bool residual_flag_
If set to true then residual test will be done during segmentation.
pcl::uindex_t min_pts_per_cluster_
Stores the minimum number of points that a cluster needs to contain in order to be considered valid.
void assembleRegions()
This function simply assembles the regions from list of point labels.
void getSegmentFromPoint(index_t index, pcl::PointIndices &cluster) override
For a given point this function builds a segment to which it belongs and returns this segment.
void setDistanceThreshold(float thresh)
Allows to set distance threshold.
void setRegionColorThreshold(float thresh)
This method specifies the threshold value for color test between the regions.
float getPointColorThreshold() const
Returns the color threshold value used for testing if points belong to the same region.
bool prepareForSegmentation() override
This method simply checks if it is possible to execute the segmentation algorithm with the current se...
void findRegionNeighbours(std::vector< std::vector< std::pair< float, pcl::index_t > > > &neighbours_out, std::vector< std::vector< int > > ®ions_in)
This method assembles the array containing neighbours of each homogeneous region.
void applyRegionMergingAlgorithm()
This function implements the merging algorithm described in the article "Color-based segmentation of ...
unsigned int getNumberOfRegionNeighbours() const
Returns the number of nearest neighbours used for searching K nearest segments.
void setCurvatureTestFlag(bool value) override
Allows to turn on/off the curvature test.
void extract(std::vector< pcl::PointIndices > &clusters) override
This method launches the segmentation algorithm and returns the clusters that were obtained during th...
void setNormalTestFlag(bool value)
Allows to turn on/off the smoothness test.
void setNumberOfRegionNeighbours(unsigned int nghbr_number)
This method allows to set the number of neighbours that is used for finding neighbouring segments.
float calculateColorimetricalDifference(std::vector< unsigned int > &first_color, std::vector< unsigned int > &second_color) const
This method calculates the colorimetrical difference between two points.
float getDistanceThreshold() const
Returns the distance threshold.
~RegionGrowingRGB()
Destructor that frees memory.
bool getNormalTestFlag() const
Returns the flag that signalize if the smoothness test is turned on/off.
void findRegionsKNN(pcl::index_t index, pcl::uindex_t nghbr_number, Indices &nghbrs, std::vector< float > &dist)
This method finds K nearest neighbours of the given segment.
RegionGrowingRGB()
Constructor that sets default values for member variables.
void setResidualTestFlag(bool value) override
Allows to turn on/off the residual test.
void findPointNeighbours() override
This method finds KNN for each point and saves them to the array because the algorithm needs to find ...
float getRegionColorThreshold() const
Returns the color threshold value used for testing if regions can be merged.
void findSegmentNeighbours()
This method simply calls the findRegionsKNN for each segment and saves the results for later use.
void setPointColorThreshold(float thresh)
This method specifies the threshold value for color test between the points.
bool validatePoint(index_t initial_seed, index_t point, index_t nghbr, bool &is_a_seed) const override
This function is checking if the point with index 'nghbr' belongs to the segment.
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
bool comparePair(std::pair< float, int > i, std::pair< float, int > j)
This function is used as a comparator for sorting.
detail::int_type_t< detail::index_type_size, detail::index_type_signed > index_t
Type used for an index in PCL.
IndicesAllocator<> Indices
Type used for indices in PCL.
detail::int_type_t< detail::index_type_size, false > uindex_t
Type used for an unsigned index in PCL.