Region Growing Segmentation

14
Region growing segmentation In this tutorial we will learn how to use the region growing algorithm implemented in the pcl::RegionGrowing  class. The purpose of the said algorithm is to merge the points that are close enough in terms of the smoothness constraint. Thereby, the output of this algorithm is the set of clusters, were each cluster is a set of points that are considered to be a part of the same smooth surface. The work of this algorithm is based on the comparison of the angles between the points normals. Theoretical Primer Let’s take a look on how the algorithm works. First of all it sorts the points by their curvature value. It needs to be done because the region begins its growth from the point that has the minimum curvature value. The reason for this is that the point with the minimum curvature is located in the flat area (growth from the flattest area allows to reduce the total number of segments. !o we have the sorted cloud. "ntil there are unlabeled points in the cloud, algorithm picks up the point with minimum curvature value and starts the growth of the region. This process occurs as follows# The picked point is added to the set called seeds. For every seed point algorithm fnds neighbouring points. o $very neighbour is tested for the angle between its normal and normal of the current seed point. If the angle is less than threshold value then current point is added to the current region. o  %fter that every nei ghbour is tested f or the curvature v alue. If the curvat ure is less than t hreshold value t hen this point is added to the seeds. o &urrent seed is removed from the seeds.

description

Region Growing Segmentation

Transcript of Region Growing Segmentation

Page 1: Region Growing Segmentation

7/17/2019 Region Growing Segmentation

http://slidepdf.com/reader/full/region-growing-segmentation 1/14

Region growing segmentation

In this tutorial we will learn how to use the region growing algorithm implemented in the pcl::RegionGrowing  class. The purpose of the said

algorithm is to merge the points that are close enough in terms of the smoothness constraint. Thereby, the output of this algorithm is the set of

clusters, were each cluster is a set of points that are considered to be a part of the same smooth surface. The work of this algorithm is based on

the comparison of the angles between the points normals.

Theoretical Primer 

Let’s take a look on how the algorithm works.

First of all it sorts the points by their curvature value. It needs to be done because the region begins its growth from the point that has the minimum

curvature value. The reason for this is that the point with the minimum curvature is located in the flat area (growth from the flattest area allows to

reduce the total number of segments.

!o we have the sorted cloud. "ntil there are unlabeled points in the cloud, algorithm picks up the point with minimum curvature value and starts

the growth of the region. This process occurs as follows#

• The picked point is added to the set called seeds.

• For every seed point algorithm fnds neighbouring points.o $very neighbour is tested for the angle between its normal and normal of the current seed point. If the angle is

less than threshold value then current point is added to the current region.

o  %fter that every neighbour is tested for the curvature value. If the curvature is less than threshold value then this

point is added to the seeds.

o &urrent seed is removed from the seeds.

Page 2: Region Growing Segmentation

7/17/2019 Region Growing Segmentation

http://slidepdf.com/reader/full/region-growing-segmentation 2/14

If the seeds set becomes empty this means that the algorithm has grown the region and the process is repeated from the beginning. 'ou can find

the pseudocode for the said algorithm below.

Inputs#

• Point cloud  

• Point normals 

• Points curvatures 

• Neighbour finding function 

• Curvature threshold  

•  Angle threshold  

Initiali)e#

• Region list  

•  Available points list  

 %lgorithm#

•  While  is not empty  do

o Current region 

o Current seeds 

o  Point with minimum curvature in 

o

o

Page 3: Region Growing Segmentation

7/17/2019 Region Growing Segmentation

http://slidepdf.com/reader/full/region-growing-segmentation 3/14

o

o for   to size ( ) do

 Find nearest neighbours of current seed

point 

for   to size ( ) do

Current neighbour point 

If   contains  an

d  then

If   then

end if 

end if 

end for 

o end for 

Page 4: Region Growing Segmentation

7/17/2019 Region Growing Segmentation

http://slidepdf.com/reader/full/region-growing-segmentation 4/14

o  Add current region to global segment list 

• end while

• Return 

The code

First of all you will need the point cloud for this tutorial. This is a good one for the purposes of the algorithm. *e+t what you need to do is to create

a fileregion_growing_segmentation.cpp in any editor you prefer and copy the following code inside of it#

 1 2 3 4 5 6 7 8 910111

213141516

#include <iostream>#include <vector>#include <pcl/point_types.h>

#include <pcl/io/pcd_io.h>#include <pcl/search/search.h>#include <pcl/search/kdtree.h>#include <pcl/features/normal_3d.h>#include <pcl/visualization/cloud_viewer.h>#include <pcl/lters/passthrough.h>#include <pcl/segmentation/region_growing.h>

intmain int argc! char"" argv$  pcl%%&oint'loud<pcl%%&oint()*>%%&tr cloud new pcl%%&oint'loud<pcl%%&oint()*>+  if   pcl%%io%%load&',-ile <pcl%%&oint()*> region_growing_tutorial.pcd! "cloud  01  $  std%%cout << 'loud reading failed. << std%%endl+  return 01+  2

  pcl%%search%%earch<pcl%%&oint()*>%%&tr tree  

Page 5: Region Growing Segmentation

7/17/2019 Region Growing Segmentation

http://slidepdf.com/reader/full/region-growing-segmentation 5/14

171819202122232

4252627282

930313233

4oost%%shared_ptr<pcl%%search%%earch<pcl%%&oint()*> > new pcl%%search%%5d6ree<pcl%%&oint()*>+  pcl%%&oint'loud <pcl%%7ormal>%%&tr normals new pcl%%&oint'loud <pcl%%7ormal>+  pcl%%7ormal8stimation<pcl%%&oint()*! pcl%%7ormal> normal_estimator+

  normal_estimator.setearch9ethod tree+  normal_estimator.set:nput'loud cloud+  normal_estimator.set5earch ;+  normal_estimator.compute "normals+

  pcl%%:ndices&tr indices new std%%vector <int>+  pcl%%&ass6hrough<pcl%%&oint()*> pass+  pass.set:nput'loud cloud+  pass.set-ilter-ield7ame z+  pass.set-ilter=imits .! 1.+

  pass.lter "indices+

  pcl%%egion?rowing<pcl%%&oint()*! pcl%%7ormal> reg+  reg.set9in'lusterize ;+  reg.set9a@'lusterize 1+  reg.setearch9ethod tree+  reg.set7um4erAf7eigh4ours 3+  reg.set:nput'loud cloud+  //reg.setIndices (indices);  reg.set:nput7ormals normals+  reg.setmoothness6hreshold 3. / 1B. " 9_&:+

  reg.set'urvature6hreshold 1.+

  std%%vector <pcl%%&oint:ndices> clusters+  reg.e@tract clusters+

  std%%cout << 7um4er of clusters is eCual to  << clusters.size << std%%endl+  std%%cout << -irst cluster has  << clustersDE.indices.size <<  points. << endl+  std%%cout << 6hese are the indices of the points of the initial <<

Page 6: Region Growing Segmentation

7/17/2019 Region Growing Segmentation

http://slidepdf.com/reader/full/region-growing-segmentation 6/14

343536373839404

1424344454

647484950

  std%%endl << cloud that 4elong to the rst cluster% << std%%endl+  int counter  +  while counter < clustersDE.indices.size   $

  std%%cout << clustersDE.indicesDcounterE << ! +  counterFF+  if  counter G 1    std%%cout << std%%endl+  2  std%%cout << std%%endl+

  pcl%%&oint'loud <pcl%%&oint()*?H>%%&tr colored_cloud  reg.get'olored'loud +  pcl%%visualization%%'loudIiewer viewer 'luster viewer+  viewer.show'loudcolored_cloud+

  while Jviewer.wastopped   $  2

  return +2

Page 7: Region Growing Segmentation

7/17/2019 Region Growing Segmentation

http://slidepdf.com/reader/full/region-growing-segmentation 7/14

515253545556575

8596061626

364656667

Page 8: Region Growing Segmentation

7/17/2019 Region Growing Segmentation

http://slidepdf.com/reader/full/region-growing-segmentation 8/14

686970717273

The explanation

*ow let’s study out what is the purpose of this code. First few lines will be omitted, because they are obvious.

First lines that are of interest are these#

  pcl%%&oint'loud<pcl%%&oint()*>%%&tr cloud new pcl%%&oint'loud<pcl%%&oint()*>+  if   pcl%%io%%load&',-ile <pcl%%&oint()*> region_growing_tutorial.pcd! "cloud  01  $  std%%cout << 'loud reading failed. << std%%endl+  return 01+  2

They are simply loading the cloud from the .pcd file. *o doubt that you saw how it is done hundreds of times, so let’s move on.

  pcl%%search%%earch<pcl%%&oint()*>%%&tr tree  4oost%%shared_ptr<pcl%%search%%earch<pcl%%&oint()*> > new pcl%%search%%5d6ree<pcl%%&oint()*>+  pcl%%&oint'loud <pcl%%7ormal>%%&tr normals new pcl%%&oint'loud <pcl%%7ormal>+  pcl%%7ormal8stimation<pcl%%&oint()*! pcl%%7ormal> normal_estimator+  normal_estimator.setearch9ethod tree+  normal_estimator.set:nput'loud cloud+  normal_estimator.set5earch ;+  normal_estimator.compute "normals+

Page 9: Region Growing Segmentation

7/17/2019 Region Growing Segmentation

http://slidepdf.com/reader/full/region-growing-segmentation 9/14

 %s mentioned before, the algorithm reuires normals. -ere the pcl::Normalstimation class is used to compute them. To learn more about how it

is done you should take a look at the$stimating !urface *ormals in a oint&loud tutorial in the Features section.

  pcl%%:ndices&tr indices new std%%vector <int>+  pcl%%&ass6hrough<pcl%%&oint()*> pass+  pass.set:nput'loud cloud+  pass.set-ilter-ield7ame z+  pass.set-ilter=imits .! 1.+  pass.lter "indices+

These lines are given only for e+ample. 'ou can safely comment this part. Insofar as pcl::RegionGrowing  is derived from pcl::!"#$ase, it can

work with indices. It means you can point that you need to segment only those points that are listed in the indices array instead of the whole point

cloud.

  pcl%%egion?rowing<pcl%%&oint()*! pcl%%7ormal> reg+  reg.set9in'lusterize ;+  reg.set9a@'lusterize 1+

'ou have finally reached the part where pcl::RegionGrowing  is instantiated. It is a template class that have two parameters#

• ointT / type of points to use(in the given e+ample it is pcl::!oint%&'

• *ormalT / type of normals to use(in the given e+ample it is pcl::Normal

 %fter that minimum and ma+imum cluster si)es are set. It means that after the segmentation is done all clusters that have less points then was set

as minimum(or have more than ma+imum will be discarded. The default values for minimum and ma+imum are 0 and 1as much as possible’

respectively.

  reg.setearch9ethod tree+  reg.set7um4erAf7eigh4ours 3+  reg.set:nput'loud cloud+  //reg.setIndices (indices);  reg.set:nput7ormals normals+

The algorithm needs 2 nearest search in its internal structure, so here is the place where a search method is provided and number of neighbours

is set. %fter that it receives the cloud that must be segmented, point indices and normals.

Page 10: Region Growing Segmentation

7/17/2019 Region Growing Segmentation

http://slidepdf.com/reader/full/region-growing-segmentation 10/14

  reg.setmoothness6hreshold 3. / 1B. " 9_&:+  reg.set'urvature6hreshold 1.+

This two lines are most important part in the algorithm initiali)ation, because they are responsible for the mentioned smoothness constraint. First

method sets the angle in radians that will be used as the allowable range for the normals deviation. If the deviation between points normals is less

than smoothness threshold then they are suggested to be in the same cluster (new point / the tested one / will be added to the cluster. The

second one is responsible for curvature threshold. If two points have a small normals deviation then the disparity between their curvatures is

tested. %nd if this value is less than curvature threshold then the algorithm will continue the growth of the cluster using new added point.

  std%%vector <pcl%%&oint:ndices> clusters+  reg.e@tract clusters+

This method simply launches the segmentation algorithm. %fter its work it will return clusters array.

  std%%cout << 7um4er of clusters is eCual to  << clusters.size << std%%endl+  std%%cout << -irst cluster has  << clustersDE.indices.size <<  points. << endl+  std%%cout << 6hese are the indices of the points of the initial <<  std%%endl << cloud that 4elong to the rst cluster% << std%%endl+  int counter  +  while counter < clustersDE.indices.size   $  std%%cout << clustersDE.indicesDcounterE << ! +  counterFF+  if  counter G 1    std%%cout << std%%endl+  2  std%%cout << std%%endl+

These lines are simple enough, so they won’t be commented. They are intended for those who are not familiar with how to work

with pcl::!oint(n)ices and how to access its elements.

  pcl%%&oint'loud <pcl%%&oint()*?H>%%&tr colored_cloud  reg.get'olored'loud +  pcl%%visualization%%'loudIiewer viewer 'luster viewer+  viewer.show'loudcolored_cloud+  while Jviewer.wastopped   $

Page 11: Region Growing Segmentation

7/17/2019 Region Growing Segmentation

http://slidepdf.com/reader/full/region-growing-segmentation 11/14

  2

  return +2

The pcl::RegionGrowing  class provides a method that returns the colored cloud where each cluster has its own color. !o in this part of code

the pcl::*is+ali,ation::"lo+)-iewer is instanciated for viewing the result of the segmentation / the same colored cloud. 'ou can learn more

about cloud visuali)ation in the The &loud3iewer  tutorial.

Compiling and running the program

 %dd the following lines to your &4akeLists.t+t file#

 1 2 3 4 5 6 7 8 910

1112

cmake_minimum_reCuired I8:A7 K.B -L6L=_8A

proMectregion_growing_segmentation

nd_package&'= 1.; 8NO:8,

include_directoriesP$&'=_:7'=O,8_,:2link_directoriesP$&'=_=:HL)_,:2add_denitionsP$&'=_,8-:7:6:A72

add_e@ecuta4le region_growing_segmentation region_growing_segmentation.cpptarget_link_li4raries region_growing_segmentation P$&'=_=:HL:82

 %fter you have made the e+ecutable, you can run it. !imply do#

P ./region_growing_segmentation

 %fter the segmentation the cloud viewer window will be opened and you will see something similar to those images#

Page 12: Region Growing Segmentation

7/17/2019 Region Growing Segmentation

http://slidepdf.com/reader/full/region-growing-segmentation 12/14

Page 13: Region Growing Segmentation

7/17/2019 Region Growing Segmentation

http://slidepdf.com/reader/full/region-growing-segmentation 13/14

5n the last image you can see that the colored cloud has many red points. This means that these points belong to the clusters that were re6ected,

because they had too much7little points.

Page 14: Region Growing Segmentation

7/17/2019 Region Growing Segmentation

http://slidepdf.com/reader/full/region-growing-segmentation 14/14