基于颜色的点云分割

news/2024/5/20 10:15:52 标签: 聚类

保留一下这道程序

// Color_based_region_growth_segmentation.cpp : 定义控制台应用程序的入口点。
//
// Color - based region growth segmentation.cpp : 定义控制台应用程序的入口点。
//
#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/visualization/cloud_viewer.h>
#include <pcl/filters/passthrough.h>
#include <pcl/segmentation/region_growing_rgb.h>
#include <pcl/console/print.h>
#include <pcl/console/parse.h>
#include <pcl/console/time.h>
#include <pcl/features/normal_3d.h>
using namespace pcl::console;
int main(int argc, char** argv)
{

	if (argc<2)
	{
		std::cout << ".exe xx.pcd -b_n 0 -kn 50 -st_n 30 -ct_n 0.05 -bc 0 -fc 10 -nc 0 -dt 10 -ct 6 -rt 5 -mt 600" << endl;

		return 0;
	}
	time_t start, end, diff[5], option;
	start = time(0);
	bool Bool_Cuting = false, b_n = false;
	int MinClusterSize = 600, KN_normal = 50;
	float far_cuting = 10, near_cuting = 0, DistanceThreshold = 10.0, ColorThreshold = 6, RegionColorThreshold = 5, SmoothnessThreshold = 30.0, CurvatureThreshold = 0.05;

	parse_argument(argc, argv, "-b_n", b_n);                 //是否使用法线信息辅助分割
	parse_argument(argc, argv, "-kn", KN_normal);            //搜索范围
	parse_argument(argc, argv, "-st_n", SmoothnessThreshold);//点与点归类时法线间的角度阈值
	parse_argument(argc, argv, "-ct_n", CurvatureThreshold); //点与点归类时曲率阈值

	parse_argument(argc, argv, "-bc", Bool_Cuting);          //是否进行范围滤波处理
	parse_argument(argc, argv, "-fc", far_cuting);           //滤波范围上限
	parse_argument(argc, argv, "-nc", near_cuting);          //滤波范围下限

	parse_argument(argc, argv, "-dt", DistanceThreshold);    //点与点归类时欧式距离阈值
	parse_argument(argc, argv, "-ct", ColorThreshold);       //点与点归类时颜色阈值
	parse_argument(argc, argv, "-rt", RegionColorThreshold); //聚类合并时颜色阈值
	parse_argument(argc, argv, "-mt", MinClusterSize);       //允许的最小聚类包含的点的个数,小于该值得聚类被合并到邻近聚类

															 //frist step load the point cloud
	pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
	if (pcl::io::loadPCDFile <pcl::PointXYZRGB>(argv[1], *cloud) == -1)
	{
		std::cout << "Cloud reading failed." << std::endl;
		return (-1);
	}
	end = time(0);
	diff[0] = difftime(end, start);
	PCL_INFO("\\Loading pcd file takes(seconds): %d\n", diff[0]);
	pcl::search::Search <pcl::PointXYZRGB>::Ptr tree = boost::shared_ptr<pcl::search::Search<pcl::PointXYZRGB> >(new pcl::search::KdTree<pcl::PointXYZRGB>);

	//Noraml estimation step(1 parameter)
	pcl::search::Search<pcl::PointXYZRGB>::Ptr tree1 = boost::shared_ptr<pcl::search::Search<pcl::PointXYZRGB> >(new pcl::search::KdTree<pcl::PointXYZRGB>);
	pcl::PointCloud <pcl::Normal>::Ptr normals(new pcl::PointCloud <pcl::Normal>);
	pcl::NormalEstimation<pcl::PointXYZRGB, pcl::Normal> normal_estimator;
	normal_estimator.setSearchMethod(tree);
	normal_estimator.setInputCloud(cloud);
	normal_estimator.setKSearch(KN_normal);
	normal_estimator.compute(*normals);
	end = time(0);
	diff[1] = difftime(end, start) - diff[0];
	PCL_INFO("\\Estimating normal takes(seconds): %d\n", diff[1]);
	//Optional step: cutting away the clutter scene too far away from camera
	pcl::IndicesPtr indices(new std::vector <int>);
	if (Bool_Cuting)//是否通过z轴范围对待处理数据进行裁剪
	{
		pcl::PassThrough<pcl::PointXYZRGB> pass;
		pass.setInputCloud(cloud);
		pass.setFilterFieldName("z");
		pass.setFilterLimits(near_cuting, far_cuting);
		pass.filter(*indices);
	}
	// Region growing RGB 
	pcl::RegionGrowingRGB<pcl::PointXYZRGB> reg;  //创建PointXYZRGB类型的区域生长分割对象
	reg.setInputCloud(cloud);                    //设置输入点云
	if (Bool_Cuting)reg.setIndices(indices);      //设置输入点云的索引
	reg.setSearchMethod(tree);                   //设置点云的搜索机制
	reg.setDistanceThreshold(DistanceThreshold); //设置距离阈值,用于判断两点是否是相邻点
	reg.setPointColorThreshold(ColorThreshold);  //设置两点颜色阈值,用于判断两点是否属于同一类
	reg.setRegionColorThreshold(RegionColorThreshold); //设置两类区域区域颜色阈值,用于判断两类区域是否聚类合并
	reg.setMinClusterSize(MinClusterSize);       //设置一个聚类的最少点数目为600
	if (b_n)
	{
		reg.setSmoothModeFlag(true);
		reg.setCurvatureTestFlag(true);
		reg.setInputNormals(normals);
		reg.setSmoothnessThreshold(SmoothnessThreshold / 180.0 * M_PI);
		reg.setCurvatureThreshold(CurvatureThreshold);
	}
	std::vector <pcl::PointIndices> clusters;
	reg.extract(clusters);
	end = time(0);
	diff[2] = difftime(end, start) - diff[0] - diff[1];
	PCL_INFO("\\RGB region growing takes(seconds): %d\n", diff[2]);
	pcl::PointCloud <pcl::PointXYZRGB>::Ptr colored_cloud = reg.getColoredCloud();
	pcl::visualization::CloudViewer viewer("基于颜色的区域生长算法实现点云分割");
	viewer.showCloud(colored_cloud);
	while (!viewer.wasStopped())
	{
		boost::this_thread::sleep(boost::posix_time::microseconds(100));
	}
	system("pause");
	return (0);
}


http://www.niftyadmin.cn/n/1472195.html

相关文章

ICP算法

自己做的一个ICP。但是效果不是特别好。。。暂时记录下来吧。 #define vtkRenderingCore_AUTOINIT 4(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingFreeType,vtkRenderingOpenGL) #define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL) #include "…

JS上传文件、导入文件

//开始导入function Import() {var filepath $(#txtUpload).val();//校验是否选择表格if (filepath ) {$(#showMsg).html(请选择表格);return;}var files document.getElementById("txtUpload").files;var fd new FormData();for (var i 0; i < files.length…

数独项目--指令判断:

对于指令的判断&#xff1a; 首先整个项目的指令集总共两个&#xff1a;sudoku.exe -c xxx Sudoku.exe -s xxx 那么判断指令是否正确也就变得简单了&#xff0c;首先argc一定为三&#xff0c;只要argc!3那么指令一定不符合要求&#xff0c;其次&#xff0c;-c代表得到要求个数的…

entityFramework 中decimal精度缺失问题

在entityFramework中&#xff0c;decimal精度默认为2位数&#xff0c;当要设置的精度大于2位并且数据库中设置的decimal精度大于2位时&#xff0c;则将数据保存在数据库中后两位的小数内容将强制为00 解决方案&#xff1a;在DbContext中加下如下代码 modelBuilder.Entity<t_…

随机采样一致性

一个基于RANSAC的去掉地面的程序。记录一下。 // RANSAC.cpp : 定义控制台应用程序的入口点。 //#include <iostream> #include <pcl/ModelCoefficients.h> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> #include <pcl/sample_consen…

logo的一般做法(小米为例)

<body><!-- h1里面嵌套a,并且有网站名,方便seo --><h1><a href"#">小米官网</a></h1> </body> <style>a {display: block;width: 55px;height: 55px;background: url(logo.png) no-repeat;/* a设置大小,背景图,首行…

PCL(1):点云数据文件格式

1、为什么要使用新的文件格式? PCD文件格式不是为了重新发明轮子&#xff0c;而是为了补充现有的文件格式&#xff0c;PCD不是第一个支持3D点云数据的文件类型,特别是计算机图形和计算几何社区&#xff0c;已经创建了许多格式来描述使用激光扫描仪获得的任意多边形和点云。其…

vue注册全局属性

例&#xff1a;统一引用getSpiderToken方法 main.js中相关代码 import { getSpiderToken } from ../static/js/storage Vue.prototype.getSpiderToken getSpiderToken 其它组件调用代码 this.getSpiderToken().nickname 属性跟方法一样&#xff0c;方法也可算做属性转载于:htt…