www.gusucode.com > vision 源码程序 matlab案例代码 > vision/EstimateNormalsOfPointCloudExample.m

    %% Estimate Normals of Point Cloud
%
%% 
% Load a point cloud.

% Copyright 2015 The MathWorks, Inc.

load('object3d.mat'); 
%%
% Estimate the normal vectors.
normals = pcnormals(ptCloud);

figure
pcshow(ptCloud)
title('Estimated Normals of Point Cloud')
hold on

x = ptCloud.Location(1:10:end,1:10:end,1);
y = ptCloud.Location(1:10:end,1:10:end,2);
z = ptCloud.Location(1:10:end,1:10:end,3);
u = normals(1:10:end,1:10:end,1);
v = normals(1:10:end,1:10:end,2);
w = normals(1:10:end,1:10:end,3);
%%
% Plot the normal vectors.
quiver3(x,y,z,u,v,w);
hold off
%%
% Flip the normals to point towards the sensor location. This step necessary only for determining the inward or outward direction of the surface. The sensor center is set in _x_ , _y_ , _z_ coordinates.
sensorCenter = [0,-0.3,0.3]; 
for k = 1 : numel(x)
   p1 = sensorCenter - [x(k),y(k),z(k)];
   p2 = [u(k),v(k),w(k)];
   % Flip the normal vector if it is not pointing towards the sensor.
   angle = atan2(norm(cross(p1,p2)),p1*p2');
   if angle > pi/2 || angle < -pi/2
       u(k) = -u(k);
       v(k) = -v(k);
       w(k) = -w(k);
   end
end
%% 
% Plot the adjusted normals.
figure
pcshow(ptCloud)
title('Adjusted Normals of Point Cloud')
hold on
quiver3(x, y, z, u, v, w);
hold off