CGAL 基于曲面拟合的点云平滑

该文介绍了如何利用CGAL库中的jet_smooth_point_set函数,基于射流拟合方法对点云数据进行平滑处理。通过加载PCL格式的点云文件,转换为CGAL支持的格式,然后设置邻域点数进行平滑,最后将平滑后的点云数据保存回文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、算法原理

  基于曲面拟合的方法,实现对点云的平滑处理。

1、主要函数

头文件

#include <CGAL/jet_smooth_point_set.h>

函数

void CGAL::jet_smooth_point_set  ( PointRange &  points,  
  unsigned int  k,  
  const NamedParameters &  np = parameters::default_values()  
 )  

  使用射流拟合在最近的邻居和重新投影到射流上的点的范围平滑。由于此方法会改变点的位置,因此不适用于进行过排序处理的点云。

  • points:点云
  • k: 隐式曲面拟合的邻域点数。值越大,结果越平滑。
  • np:下面列出的命名参数中的一个可选序列。

在这里插入图片描述

二、代码实现

#include <vector>
#include <iostream>    // io
#include <pcl/io/pcd_io.h>          // PCL读取PCD
#include <pcl/point_types.h>        // PCL点类型
#include <CGAL/Point_set_3/IO/XYZ.h>// CGAL保存点为.xyz

#include <CGAL/jet_smooth_point_set.h>
#include <CGAL/Simple_cartesian.h>

typedef CGAL::Simple_cartesian<float> Kernel;

int main()
{
	//-------------------------加载PCD点云数据------------------------------
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);

	if (pcl::io::loadPCDFile<pcl::PointXYZ>("cgal//cloud.pcd", *cloud) == -1)
	{
		PCL_ERROR("Could not read file\n");
		return -1;
	}
	// ----------------------转为CGAL支持的格式------------------------------
	std::vector<Kernel::Point_3> points;
	for (size_t i = 0; i < cloud->size(); ++i)
	{
		float px = cloud->points[i].x;
		float py = cloud->points[i].y;
		float pz = cloud->points[i].z;
		points.push_back(Kernel::Point_3(px, py, pz));
	}

	// ---------------------------Smoothing----------------------------------
	const unsigned int nb_neighbors = 24; // 邻域点的个数
	CGAL::jet_smooth_point_set<CGAL::Parallel_if_available_tag>(points, nb_neighbors);
	//CGAL::IO::write_XYZ("cgal//smooth.xyz", points);

	pcl::PointCloud<pcl::PointXYZ>::Ptr smoothed(new pcl::PointCloud<pcl::PointXYZ>);
	smoothed->resize(points.size());
	smoothed->reserve(points.size());

	for (size_t i = 0; i < points.size(); ++i)
	{
		smoothed->points[i].x = points[i].hx();
		smoothed->points[i].y = points[i].hy();
		smoothed->points[i].z = points[i].hz();
	}
	pcl::io::savePCDFileBinary("cgal//jet_smooth.pcd", *smoothed); 
	
	return 0;
}

三、结果展示

在这里插入图片描述

### 曲面拟合的代码示例 以下是几种常见的曲面拟合方法及其对应的代码示例: #### 基于CGAL库的点云平滑算法 利用CGAL库可以高效地完成点云数据的曲面拟合平滑操作。下面是一个简单的C++代码示例,展示如何使用CGAL进行点云曲面拟合[^1]。 ```cpp #include <CGAL/Simple_cartesian.h> #include <CGAL/surface_neighbor_coordinates_3.h> #include <vector> typedef CGAL::Simple_cartesian<double> K; typedef K::Point_3 Point_3; typedef K::Vector_3 Vector_3; int main() { std::vector<Point_3> points = { /* 插入点云数据 */ }; Point_3 query_point(0, 0, 0); // 计算查询点周围的表面邻居坐标 std::vector<std::pair<int, double>> neighbors; bool is_in_convex_hull = CGAL::surface_neighbor_coordinates_3( points.begin(), points.end(), query_point, std::back_inserter(neighbors)); if (!is_in_convex_hull) { // 查询点不在凸包内部时处理逻辑 } return 0; } ``` 此代码片段展示了如何计算某个查询点周围点云的表面邻居坐标,这是实现曲面拟合的重要一步。 --- #### 遗传算法用于多项式曲面拟合 遗传算法是一种优化技术,适用于复杂的数据拟合场景。以下MATLAB代码演示了如何用遗传算法拟合一组二维数据到三次多项式曲面上[^2]。 ```matlab function [p_fit] = genetic_algorithm_fitting(x, y) % 定义适应度函数 fitnessfcn = @(p) sum((polyval(p, x) - y).^2); % 设置遗传算法参数 options = gaoptimset('Display', 'iter'); nVars = 4; % 对应三次多项式的系数数量 % 运行遗传算法 p_fit = ga(fitnessfcn, nVars, [], [], [], [], [], [], [], options); end % 数据准备 x = linspace(0, 2 * pi, 50)'; y = sin(x) + randn(size(x)) * 0.1; % 调用遗传算法 [p_fit] = genetic_algorithm_fitting(x, y); % 可视化结果 figure; plot(x, y, 'bo'); hold on; x_fit = linspace(0, 2 * pi, 1000); y_fit = polyval(p_fit, x_fit); plot(x_fit, y_fit, 'r-'); title('Sin function fitting using genetic algorithm'); legend('Data points', 'Fitted curve'); ``` 这段代码通过遗传算法找到最佳的多项式系数来逼近给定数据集。 --- #### PCL库中的B-Spline曲面拟合 PCL(Point Cloud Library)提供了一种强大的工具来进行点云的B-Spline曲面拟合。下面是Python版本的一个简单例子[^3]。 ```python import pcl from pcl import registration def b_spline_surface_fitting(input_cloud): cloud_filtered = input_cloud.make_voxel_grid_filter() cloud_filtered.set_leaf_size(0.01, 0.01, 0.01) sor = cloud_filtered.filter() # 创建B-Spline模型估计器 bspline_model = pcl.SACMODEL_BSPLINE ransac = sor.make_segmenter_normals(k_search=50) ransac.set_optimize_coefficients(True) ransac.set_model_type(bspline_model) ransac.set_method_type(pcl.SAC_RANSAC) ransac.set_distance_threshold(0.01) indices, model = ransac.segment() return indices, model # 加载点云文件 cloud = pcl.load_XYZRGB("input.pcd") # 将点云转换为PointXYZ格式 if hasattr(cloud, 'points'): point_cloud_xyz = pcl.PointCloud() point_cloud_xyz.from_array(numpy.array([[pt.x, pt.y, pt.z] for pt in cloud])) indices, model = b_spline_surface_fitting(point_cloud_xyz) print(model) ``` 注意,在实际应用中可能需要调整`voxel_grid_filter`的叶子大小以及RANSAC的距离阈值以获得更好的拟合效果。 --- ### 注意事项 当遇到显示窗口无响应的情况时,可以在可视化部分增加时间延迟参数,例如将`viewer.spinOnce()`替换为`viewer.spinOnce(3000)`,从而确保程序正常运行[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

点云侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值