Region-Growing-RGB-PCL-Cpp (50%)

There are two main differences in the color-based algorithm.

The first one is that it uses color instead of normals.

The second is that it uses the merging algorithm for over- and under- segmentation control.

Letโ€™s take a look at how it is done.

  • After the segmentation, an attempt for merging clusters with close colors is made.

  • Two neighbouring clusters with a small difference between average color are merged together.

  • Then the second merging step takes place.

  • During this step every single cluster is verified by the number of points that it contains.

  • If this number is less than the user-defined value than current cluster is merged with the closest neighbouring cluster.

#include <iostream>
#include <thread>
#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/visualization/cloud_viewer.h>
#include <pcl/filters/passthrough.h>
#include <pcl/segmentation/region_growing_rgb.h>

// Color-based region growing segmentation
// http://pointclouds.org/documentation/tutorials/region_growing_rgb_segmentation.php#region-growing-rgb-segmentation

int
main (int argc, char** argv)
{
  // *.PCD ํŒŒ์ผ ์ฝ๊ธฐ 
  // https://github.com/adioshun/gitBook_Tutorial_PCL/blob/master/Intermediate/sample/region_growing_rgb_passthrough.pcd
  pcl::PointCloud <pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud <pcl::PointXYZRGB>);
  pcl::io::loadPCDFile <pcl::PointXYZRGB> ("region_growing_rgb_passthrough.pcd", *cloud);

  // ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ์‚ฌ์šฉํ•˜๋Š” Surface Normal ๊ณ„์‚ฐ  : Normal ์ •๋ณด ๋Œ€์‹  ์ƒ‰์ƒ ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ Normal ๊ณ„์‚ฐ ์ž‘์—…์ด ๋ถˆํ•„์š” ํ•จ 

  //์˜คํ”„์ ํŠธ ์ƒ์„ฑ 
  pcl::search::Search <pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGB>);
  pcl::RegionGrowingRGB<pcl::PointXYZRGB> reg;
  reg.setInputCloud (cloud);         // ์ž…๋ ฅ 
  reg.setSearchMethod (tree);        // ํƒ์ƒ‰ ๋ฐฉ๋ฒ• 
  reg.setDistanceThreshold (10);     // ์ด์›ƒ(Neighbor)์œผ๋กœ ์ง€์ •๋˜๋Š” ๊ฑฐ๋ฆฌ ์ •๋ณด 
  reg.setPointColorThreshold (6);    // ๋™์ผ Cluter์—ฌ๋ถ€๋ฅผ ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ (cf. Just as angle threshold is used for testing points normals )
  reg.setRegionColorThreshold (5);   // ๋™์ผ Cluter์—ฌ๋ถ€๋ฅผ ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ, ํ†ตํ•ฉ(merging)๋‹จ๊ณ„์—์„œ ์‚ฌ์šฉ๋จ 
  reg.setMinClusterSize (600);       // ์ตœ์†Œ ํฌ์ธํŠธ์ˆ˜, ์ง€์ • ๊ฐ’๋ณด๋‹ค ์ž‘์œผ๋ฉด ์ด์›ƒ ํฌ์ธํŠธ์™€ ํ†ตํ•ฉ ๋จ 

  std::vector <pcl::PointIndices> clusters;
  reg.extract (clusters);

  //ํŒŒ์ผ ์ €์žฅ 
  pcl::PointCloud <pcl::PointXYZRGB>::Ptr colored_cloud = reg.getColoredCloud ();
  pcl::io::savePCDFile<pcl::PointXYZRGB>("region_growing_rgb_result.pcd", *colored_cloud);

  return (0);
}

์›๋ณธ ์ฝ”๋“œ์— ์˜คํƒ€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. pass.setFilterLimits (0.0, 1.0); -> pass.setFilterLimits (0.0, 5.0);

Last updated