Detailed function of matlab pcnormals() function

Official explanation: 

pcnormals - Estimate normals for point cloud
    This MATLAB function returns a matrix that stores a normal for each
    point in the input ptCloud.

    normals = pcnormals(ptCloud)
    normals = pcnormals(ptCloud,k)

    输入参数
        ptCloud - Object for storing point cloud
            pointCloud object
        k - Number of points used for local plane fitting
            integer greater than or equal to 3

    输出参数
        normals - Normals used to fit a local plane
            M-by-3 | M-by-N-by-3

translate:

Estimate Normals of Point Clouds
This MATLAB function returns a matrix that stores the normals for each point in the input ptCloud.

normal = pcnormals(ptCloud)
normals = pcnormals(ptCloud,k)

Input parameter
ptCloud - the object storing the point cloud
pointCloud object
k - the number of points used for local plane fitting, an
integer greater than or equal to 3

Output parameters
normal - the normal used to fit the local plane
m × 3 | m × n × 3

k is the number of nearest neighbors, the default is 8.

clear
% 加载茶壶的点云
ptCloud = pcread('teapot.ply');
% 计算法向量,6个邻近点
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

 

Curvature calculation - and calculate the normal vector directly using the covariance matrix for calculation

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

 

Guess you like

Origin blog.csdn.net/Vertira/article/details/131832876