PCL区域生长分割

直接看代码吧,基于PCL实现。库文件自己筛选以下吧

#include <iostream>
#include <pcl/visualization/pcl_visualizer.h>  
#include <pcl/common/common.h>
#include <pcl/point_types.h>
#include <fstream>  
#include <vector>
#include <pcl/features/normal_3d.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <sstream>
#include <unistd.h>
#include <pcl/search/impl/search.hpp>
#include <pcl/impl/instantiate.hpp>
#include <pcl/point_types.h>
#include <pcl/segmentation/region_growing.h>
#include <pcl/search/kdtree.h>
#include <pcl/search/search.h>
using namespace std;
void CreateCloudFromTxt(const std::string& file_path,pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
{
    std::ifstream file(file_path.c_str());
    std::string line;
    std::string no_use;
    pcl::PointXYZ point;
    while (getline(file, line)) {
        string::iterator it;
        for (it = line.begin(); it < line.end(); it++)
        {
          if (*it == ',')
          {
            line.erase(it);
            line.insert(it, ' ');
            it--;
          }
        }
        std::stringstream ss(line);
        ss >> point.x;
        ss >> point.y;
        ss >> point.z;
        cloud->push_back(point);
    }
    file.close();
}
void visualization(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
{
    boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("viewer"));
   
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 255, 0, 0);
    viewer->addPointCloud<pcl::PointXYZ>(cloud, single_color, "example");
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "example");

    while (!viewer->wasStopped()) {
        viewer->spinOnce(100);
        boost::this_thread::sleep(boost::posix_time::microseconds(100000));
    }
}
int main(int argc, char** argv)
{
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::PointCloud <pcl::Normal>::Ptr normals (new pcl::PointCloud <pcl::Normal>);
	CreateCloudFromTxt(argv[1],cloud);
	pcl::search::Search<pcl::PointXYZ>::Ptr tree = boost::shared_ptr<pcl::search::Search<pcl::PointXYZ> > (new pcl::search::KdTree<pcl::PointXYZ>);
	pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> normal_estimator;
 	normal_estimator.setSearchMethod (tree);
 	normal_estimator.setInputCloud (cloud);
 	normal_estimator.setKSearch (20);
 	normal_estimator.compute (*normals);

	pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> reg;
  	reg.setMinClusterSize (50);
 	reg.setMaxClusterSize (1000000);
  	reg.setSearchMethod (tree);
  	reg.setNumberOfNeighbours (30);
  	reg.setInputCloud (cloud);
  	//reg.setIndices (indices);
  	reg.setInputNormals (normals);
  	reg.setSmoothnessThreshold (3.0 / 180.0 * M_PI);
  	reg.setCurvatureThreshold (1.0);
	std::vector <pcl::PointIndices> cluster_indices;
        reg.extract (cluster_indices);

	std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr> Eucluextra; //用于储存欧式分割后的点云
    	for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it)
    	{
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster(new pcl::PointCloud<pcl::PointXYZ>);
       	for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); pit++)
       	cloud_cluster->points.push_back(cloud->points[*pit]);
        cloud_cluster->width = cloud_cluster->points.size ();
        cloud_cluster->height = 1;
        cloud_cluster->is_dense = true;
	Eucluextra.push_back(cloud_cluster);
 	 }

	for(int k=0;k<Eucluextra.size();k++)     
	{
	visualization(Eucluextra[k]);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值