效果:
代码:
#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/filter_indices.h> // for pcl::removeNaNFromPointCloud
#include <pcl/segmentation/region_growing_rgb.h>using namespace std::chrono_literals;int main()
{pcl::search::Search<pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGB>);pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);if( pcl::io::loadPCDFile<pcl::PointXYZRGB>("region_growing_rgb_tutorial.pcd", *cloud) == -1){std::cout << "cloud reading failed." << std::endl;return (-1);}pcl::IndicesPtr indices (new std::vector<int>);pcl::removeNaNFromPointCloud(*cloud, *indices); //从点云中移除包含NaN值的点,并返回非NaN点的索引pcl::RegionGrowingRGB<pcl::PointXYZRGB> reg;reg.setInputCloud(cloud);reg.setIndices(indices);reg.setSearchMethod(tree);reg.setDistanceThreshold(10);reg.setPointColorThreshold(6);reg.setRegionColorThreshold(5);reg.setMinClusterSize(600);std::vector<pcl::PointIndices> clusters;reg.extract(clusters);pcl::PointCloud <pcl::PointXYZRGB>::Ptr colored_cloud = reg.getColoredCloud();pcl::visualization::CloudViewer viewer("cluster viewer");viewer.showCloud(colored_cloud);while (!viewer.wasStopped()) {std::this_thread::sleep_for(100);}return(0);}