MATLAB点云处理:2点云法向量计算和最近点查询、通过索引提取点云

上一次记录了

MATLAB点云处理:1点云的读取、显示和保存

MATLAB点云处理1

这次继续写

MATLAB点云处理:2点云最近点查询和法向量计算、通过索引提取点云

这次主要是点云的法向量计算最近点查询

法向量计算

MATLAB提供了函数pcnormals用于计算点云的法向量,下面用经典的兔子展示一下

clc,clear
rabbit = pcread('rabbit.pcd')

通过上述代码,点开加载的兔子,可以看到里面是没有Normal属性的
在这里插入图片描述
然后通过

rabbit_normals = pcnormals(rabbit)

运行之后,就可以看到rabbit_normals的结果保存了计算出的法向量,这个结果不会保存到rabbit的Normal属性里,但是可以通过

rabbit.Normal = rabbit_normals

直接进行赋值;
为了让法向量也显示出来,参考官网提供的示例,可以通过quiver函数实现

rabbit = pcread('rabbit.pcd')
rabbit_normals = pcnormals(rabbit)
rabbit.Normal = rabbit_normals;
pcshow(rabbit)
hold on
x = rabbit.Location(:,1);
y = rabbit.Location(:,2);
z = rabbit.Location(:,3);
u = rabbit.Normal(:,1);
v = rabbit.Normal(:,2);
w = rabbit.Normal(:,3);
quiver3(x,y,z,u,v,w);

最终显示结果如下图所示
在这里插入图片描述
由于点云法向量的计算是通过找到临近点拟合平面计算法向量得来的,这就涉及到临近点数量的选取,pcnormals还提供了一个参数的设置,例如

rabbit_normals = pcnormals(rabbit,10)

这就是选取了当前点最近的10个点来计算法向量

最近点查询

刚才说到在计算法向量的时候,其实利用到最近点查询,那么就关注一下,MATLAB是怎么实现最近点查询的?
MATLAB也同样提供了两种查询方式,findNearestNeighbors和findNeighborsInRadius

[indices,dists] = findNearestNeighbors(ptCloud,point,K)
[indices,dists] = findNeighborsInRadius(ptCloud,point,radius)

这是这两种函数最普通也是最常用的调用方式
返回的indices是目标点云中的对应点的索引,dists是对应的距离,下面通过兔子展示一下:

clc,clear
rabbit = pcread('rabbit.pcd')
% rabbit_normals = pcnormals(rabbit,10)
% rabbit.Normal = rabbit_normals;
current_point_indic = unidrnd(rabbit.Count);
current_point = rabbit.Location(current_point_indic,:)
[indices, dists] = findNearestNeighbors(rabbit,current_point,50);
pcshow(rabbit)
hold on
plot3(current_point(1),current_point(2),current_point(3),'*r')
plot3(rabbit.Location(indices,1),rabbit.Location(indices,2),rabbit.Location(indices,3),'*')

最终的结果如下,可以看到兔子的身体黄了一坨
在这里插入图片描述
半径范围查询代码和结果如下

clc,clear
rabbit = pcread('rabbit.pcd')
% rabbit_normals = pcnormals(rabbit,10)
% rabbit.Normal = rabbit_normals;
current_point_indic = unidrnd(rabbit.Count);
current_point = rabbit.Location(current_point_indic,:)
[indices, dists] = findNeighborsInRadius(rabbit,current_point,0.02);
pcshow(rabbit)
hold on
plot3(current_point(1),current_point(2),current_point(3),'*r')
plot3(rabbit.Location(indices,1),rabbit.Location(indices,2),rabbit.Location(indices,3),'*')

在这里插入图片描述
这里可以看到,由于点的距离比较近,这次直接黄了一大块

通过索引提取点云

刚才通过最近点搜索返回的是点云里对应的索引,我们是通过复制的方式,比较笨的提取需要的点,其实MATLAB中提供了函数select函数

ptCloudOut = select(ptCloud,indices)

在这里插入图片描述

  • 7
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值