Point Cloud Library (PCL)  1.7.0
region_growing_rgb.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of the copyright holder(s) nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Author : Sergey Ushakov
36  * Email : mine_all_mine@bk.ru
37  *
38  */
39 
40 #ifndef PCL_REGION_GROWING_RGB_H_
41 #define PCL_REGION_GROWING_RGB_H_
42 
43 #include <pcl/segmentation/region_growing.h>
44 
45 namespace pcl
46 {
47  /** \brief
48  * Implements the well known Region Growing algorithm used for segmentation based on color of points.
49  * Description can be found in the article
50  * "Color-based segmentation of point clouds"
51  * by Qingming Zhan, Yubin Liang, Yinghui Xiao
52  */
53  template <typename PointT, typename NormalT = pcl::Normal>
54  class PCL_EXPORTS RegionGrowingRGB : public RegionGrowing<PointT, NormalT>
55  {
56  public:
57 
81 
82  public:
83 
84  /** \brief Constructor that sets default values for member variables. */
86 
87  /** \brief Destructor that frees memory. */
88  virtual
89  ~RegionGrowingRGB ();
90 
91  /** \brief Returns the color threshold value used for testing if points belong to the same region. */
92  float
93  getPointColorThreshold () const;
94 
95  /** \brief This method specifies the threshold value for color test between the points.
96  * This kind of testing is made at the first stage of the algorithm(region growing).
97  * If the difference between points color is less than threshold value, then they are considered
98  * to be in the same region.
99  * \param[in] thresh new threshold value for color test
100  */
101  void
102  setPointColorThreshold (float thresh);
103 
104  /** \brief Returns the color threshold value used for testing if regions can be merged. */
105  float
106  getRegionColorThreshold () const;
107 
108  /** \brief This method specifies the threshold value for color test between the regions.
109  * This kind of testing is made at the second stage of the algorithm(region merging).
110  * If the difference between segments color is less than threshold value, then they are merged together.
111  * \param[in] thresh new threshold value for color test
112  */
113  void
114  setRegionColorThreshold (float thresh);
115 
116  /** \brief Returns the distance threshold. If the distance between two points is less or equal to
117  * distance threshold value, then those points assumed to be neighbouring points.
118  */
119  float
120  getDistanceThreshold () const;
121 
122  /** \brief Allows to set distance threshold.
123  * \param[in] thresh new threshold value for neighbour test
124  */
125  void
126  setDistanceThreshold (float thresh);
127 
128  /** \brief Returns the number of nearest neighbours used for searching K nearest segments.
129  * Note that here it refers to the segments(not the points).
130  */
131  unsigned int
132  getNumberOfRegionNeighbours () const;
133 
134  /** \brief This method allows to set the number of neighbours that is used for finding
135  * neighbouring segments. Neighbouring segments are needed for the merging process.
136  * \param[in] nghbr_number the number of neighbouring segments to find
137  */
138  void
139  setNumberOfRegionNeighbours (unsigned int nghbr_number);
140 
141  /** \brief Returns the flag that signalize if the smoothness test is turned on/off. */
142  bool
143  getNormalTestFlag () const;
144 
145  /** \brief
146  * Allows to turn on/off the smoothness test.
147  * \param[in] value new value for normal/smoothness test. If set to true then the test will be turned on
148  */
149  void
150  setNormalTestFlag (bool value);
151 
152  /** \brief Allows to turn on/off the curvature test.
153  * \param[in] value new value for curvature test. If set to true then the test will be turned on
154  */
155  virtual void
156  setCurvatureTestFlag (bool value);
157 
158  /** \brief
159  * Allows to turn on/off the residual test.
160  * \param[in] value new value for residual test. If set to true then the test will be turned on
161  */
162  virtual void
163  setResidualTestFlag (bool value);
164 
165  /** \brief This method launches the segmentation algorithm and returns the clusters that were
166  * obtained during the segmentation.
167  * \param[out] clusters clusters that were obtained. Each cluster is an array of point indices.
168  */
169  virtual void
170  extract (std::vector <pcl::PointIndices>& clusters);
171 
172  /** \brief For a given point this function builds a segment to which it belongs and returns this segment.
173  * \param[in] index index of the initial point which will be the seed for growing a segment.
174  */
175  virtual void
176  getSegmentFromPoint (int index, pcl::PointIndices& cluster);
177 
178  protected:
179 
180  /** \brief This method simply checks if it is possible to execute the segmentation algorithm with
181  * the current settings. If it is possible then it returns true.
182  */
183  virtual bool
184  prepareForSegmentation ();
185 
186  /** \brief This method finds KNN for each point and saves them to the array
187  * because the algorithm needs to find KNN a few times.
188  */
189  virtual void
190  findPointNeighbours ();
191 
192  /** \brief This method simply calls the findRegionsKNN for each segment and
193  * saves the results for later use.
194  */
195  void
196  findSegmentNeighbours ();
197 
198  /** \brief This method finds K nearest neighbours of the given segment.
199  * \param[in] index index of the segment for which neighbours will be found
200  * \param[in] nghbr_number the number of neighbours to find
201  * \param[out] nghbrs the array of indices of the neighbours that were found
202  * \param[out] dist the array of distances to the corresponding neighbours
203  */
204  void
205  findRegionsKNN (int index, int nghbr_number, std::vector<int>& nghbrs, std::vector<float>& dist);
206 
207  /** \brief This function implements the merging algorithm described in the article
208  * "Color-based segmentation of point clouds"
209  * by Qingming Zhan, Yubin Liang, Yinghui Xiao
210  */
211  void
212  applyRegionMergingAlgorithm ();
213 
214  /** \brief This method calculates the colorimetrical difference between two points.
215  * In this case it simply returns the euclidean distance between two colors.
216  * \param[in] first_color the color of the first point
217  * \param[in] second_color the color of the second point
218  */
219  float
220  calculateColorimetricalDifference (std::vector<unsigned int>& first_color, std::vector<unsigned int>& second_color) const;
221 
222  /** \brief This method assembles the array containing neighbours of each homogeneous region.
223  * Homogeneous region is the union of some segments. This array is used when the regions
224  * with a few points need to be merged with the neighbouring region.
225  * \param[out] neighbours_out vector of lists of neighbours for every homogeneous region
226  * \param[in] regions_in vector of lists, each list contains indices of segments that belong
227  * to the corresponding homogeneous region.
228  */
229  void
230  findRegionNeighbours (std::vector< std::vector< std::pair<float, int> > >& neighbours_out, std::vector< std::vector<int> >& regions_in);
231 
232  /** \brief This function simply assembles the regions from list of point labels.
233  * \param[in] num_pts_in_region for each final region it stores the corresponding number of points in it
234  * \param[in] num_regions number of regions to assemble
235  */
236  void
237  assembleRegions (std::vector<unsigned int>& num_pts_in_region, int num_regions);
238 
239  /** \brief This function is checking if the point with index 'nghbr' belongs to the segment.
240  * If so, then it returns true. It also checks if this point can serve as the seed.
241  * \param[in] initial_seed index of the initial point that was passed to the growRegion() function
242  * \param[in] point index of the current seed point
243  * \param[in] nghbr index of the point that is neighbour of the current seed
244  * \param[out] is_a_seed this value is set to true if the point with index 'nghbr' can serve as the seed
245  */
246  virtual bool
247  validatePoint (int initial_seed, int point, int nghbr, bool& is_a_seed) const;
248 
249  protected:
250 
251  /** \brief Thershold used in color test for points. */
253 
254  /** \brief Thershold used in color test for regions. */
256 
257  /** \brief Threshold that tells which points we need to assume neighbouring. */
259 
260  /** \brief Number of neighbouring segments to find. */
262 
263  /** \brief Stores distances for the point neighbours from point_neighbours_ */
264  std::vector< std::vector<float> > point_distances_;
265 
266  /** \brief Stores the neighboures for the corresponding segments. */
267  std::vector< std::vector<int> > segment_neighbours_;
268 
269  /** \brief Stores distances for the segment neighbours from segment_neighbours_ */
270  std::vector< std::vector<float> > segment_distances_;
271 
272  /** \brief Stores new indices for segments that were obtained at the region growing stage. */
273  std::vector<int> segment_labels_;
274 
275  public:
276  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
277  };
278 }
279 
280 #ifdef PCL_NO_PRECOMPILE
281 #include <pcl/segmentation/impl/region_growing_rgb.hpp>
282 #endif
283 
284 #endif