【编程实践】利用Alpha Shape凹包提取三维点云某个投影下的边界

1 程序执行结果

实现效果:白色的为提取的点云边界(体素下采样叶节点参数setLeafSize(0.1f,0.1f,0.1f),投影至Z=0平面,concave hull参数为0.3)
在这里插入图片描述
在这里插入图片描述
实现效果:白色的为提取的点云边界(体素下采样叶节点参数setLeafSize(0.1f,0.1f,0.1f),投影至Z=-1.0平面,concave hull参数为0.7)
在这里插入图片描述
在这里插入图片描述

2 代码实现

// alpha-concave hull
#include <pcl/io/pcd_io.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/surface/concave_hull.h>
#include <pcl/console/time.h>
using namespace std;

typedef pcl::PointXYZ PointT; //C++类型定义,用于简化类型名称
typedef pcl::PointCloud<PointT> PointCloudT;

int main()
{
	cout << "Start..." << endl;
	PointCloudT::Ptr cloud(new PointCloudT);
	string path = ".\\input\\Kuangshan_long_R.pcd";

	if (pcl::io::loadPCDFile(path, *cloud) < 0)
	{
		PCL_ERROR("The point cloud file does not exist.\n");
		return -1;
	}
	cout << "There are " << cloud->points.size() << " points of data." << endl;
	//--------------------------点云数据下采样 --------------------------
	PointCloudT::Ptr cloud_sub(new PointCloudT);	//下采样点云
	pcl::VoxelGrid<PointT> vg;			//创建体素下采样对象
	vg.setInputCloud(cloud);			//设置下采样输入点云
	vg.setLeafSize(0.1f, 0.1f, 0.1f);//设置体素栅格边长
	vg.filter(*cloud_sub);			//执行体素下采样
	//利用遍历修改某维度值的方法进行投影
	for (size_t i = 0; i < cloud_sub->size(); ++i)
	{
		cloud_sub->points[i].z = 0.0f;//投影至Z=0平面
	}
	//-------------------- AlphaShapes提取投影边界 凹包算法(ConcaveHull)--------------------
	pcl::console::TicToc time;
	time.tic();

	PointCloudT::Ptr cloud_boundary(new PointCloudT);
	pcl::ConcaveHull<PointT> ch;
	ch.setInputCloud(cloud_sub);
	ch.setAlpha(0.3);
	ch.reconstruct(*cloud_boundary);
	cout << "This operation takes :" << time.toc() / 1000 << " s(second)" << endl;
	//---------------------------- 保存边界点云 --------------------------
	if (!cloud_boundary->empty())
	{
		pcl::io::savePCDFileBinary(".\\output\\0911\\boundary_points.pcd", *cloud_boundary);
		cout << "There are a total of " << cloud_boundary->points.size() << " points in the boundary point cloud." << endl;
	}
	else
	{
		PCL_ERROR("The boundary point cloud is empty. \a\n");
		system("pause");
		return -1;
	}
	cout << "Done!" << endl;
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用Python实现alpha shape算法对二维点进行边界提取的代码: ```python from scipy.spatial import Delaunay import numpy as np from matplotlib import pyplot as plt def alpha_shape(points, alpha): """ Compute the alpha shape (concave hull) of a set of points. """ # Delaunay triangulation tri = Delaunay(points) # Filter triangles by alpha value edges = set() edge_points = [] for j, tri in enumerate(tri.simplices): # Calculate circumcenter and radius of circumcircle x, y = np.mean(points[tri], axis=0) r = np.sqrt((points[tri[0]][0] - x)**2 + (points[tri[0]][1] - y)**2) # Check if triangle is within alpha value if r < alpha: for i in range(3): edge = (tri[i], tri[(i+1)%3]) if edge not in edges and (edge[1], edge[0]) not in edges: edges.add(edge) edge_points.append(points[tri[i]]) edge_points.append(points[tri[(i+1)%3]]) # Compute boundary vertices and edges boundary_vertices = set() boundary_edges = [] for i, point in enumerate(points): for edge in edges: if i in edge: boundary_vertices.add(i) boundary_edges.append(edge) # Plot alpha shape plt.triplot(points[:,0], points[:,1], tri.simplices.copy()) plt.plot(edge_points[:,0], edge_points[:,1], 'r.') plt.plot(points[list(boundary_vertices),0], points[list(boundary_vertices),1], 'b.') plt.show() return boundary_vertices, boundary_edges ``` 在这个代码中,我们使用了`scipy.spatial`库中的`Delaunay`函数来进行Delaunay三角剖分,然后根据alpha值筛选出在范围内的三角形,并提取边界。 下面是一个使用示例: ```python # Generate random points points = np.random.rand(30, 2) # Compute alpha shape alpha = 0.3 boundary_vertices, boundary_edges = alpha_shape(points, alpha) # Print boundary vertices and edges print("Boundary vertices:", boundary_vertices) print("Boundary edges:", boundary_edges) ``` 这个例子中,我们先生成了30个随机点,然后使用`alpha_shape`函数计算alpha shape,并输出边界点和边。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值