Matlab计算并绘制点云的法向量和曲率

1、计算绘制法向量

clear
% 加载茶壶的点云
ptCloud = pcread('teapot.ply');
% 计算法向量,8个邻近点
normals = pcnormals(ptCloud);
% 读取x
x = ptCloud.Location(1:5:end,1);
% 读取y
y = ptCloud.Location(1:5:end,2);
% 读取z
z = ptCloud.Location(1:5:end,3);
% uvw为法向量的三列
u = normals(1:5:end,1);
v = normals(1:5:end,2);
w = normals(1:5:end,3);
pcshow(ptCloud)
hold on
% 显示法向量
quiver3(x,y,z,u,v,w);
hold off

Matlab计算并绘制点云的法向量和曲率_第1张图片
2、绘制不同颜色/不用粗细的向量

x0 = 0
y0 = 0
z0 = 0
x11 = 0
x12 = 0
x13 = 2
quiver3(x0,y0,z0,x11,x12,x13,'r','linewidth',3)
hold on
x21 = 0
x22 = 2
x23 = 0
quiver3(x0,y0,z0,x21,x22,x23,'g','linewidth',3)
hold on
x31 = 2
x32 = 0
x33 = 0
quiver3(x0,y0,z0,x31,x32,x33,'b','linewidth',3)

Matlab计算并绘制点云的法向量和曲率_第2张图片
3、曲率计算

clear
% 读取茶壶点云
ptCloud = pcread('teapot.ply');
% 读取xyz
a = ptCloud.Location;
%vec储存法向量
vec = zeros(size(a));
%q储存曲率
q = zeros(length(a),1);
 
k = 8;
% 搜索每个点的最邻近点
neighbors = knnsearch(a(:,1:3),a(:,1:3), 'k', k+1);
for i = 1:length(a)
    curtemp = neighbors(i,2:end);
    indpoint = a(curtemp,:);
    % 计算协方差并提取特征
    [v, c] = eig(cov(indpoint));
    %特征值按照升序排列1<2<3
    c = diag(c)';
    %计算特征值的总和
    z = sum(c);
    %计算曲率,用最小特征值除/特征值总和,这也是特征归一化
    p1 = c(:,1)/z;
    q(i,:) = abs(p1);
    %最小特征值对应的列向量就是法向量,dot是交叉相乘
    vec(i,:) = v(:,1)';
end
 
% 读取x
x = ptCloud.Location(1:5:end,1);
% 读取y
y = ptCloud.Location(1:5:end,2);
% 读取z
z = ptCloud.Location(1:5:end,3);
% uvw为法向量的三列
u = vec(1:5:end,1);
v = vec(1:5:end,2);
w = vec(1:5:end,3);
pcshow(ptCloud)
hold on
% 显示法向量
quiver3(x,y,z,u,v,w);
hold off

Matlab计算并绘制点云的法向量和曲率_第3张图片

你可能感兴趣的:(工程,matlab,开发语言)