Estimate normals for point cloud
Estimate Normals of Point Cloud
Load a point cloud.
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 is 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
ptCloud — Object for storing point cloud
Object for storing point cloud, returned as a
k — Number of points used for local plane fitting
integer greater than or equal to 3
Number of points used for local plane fitting, specified as an integer greater than or equal
to 3. Increasing this value improves accuracy but slows down computation
time. If you do not specify
k, the function uses six
neighboring points to fit a local plane to determine each normal
normals — Normals used to fit a local plane
M-by-3 | M-by-N-by-3
Normals used to fit a local plane, returned as an M-by-3 or an M-by-N-by-3 vector. The normal vectors are computed locally using six neighboring points. The direction of each normal vector can be set based on how you acquired the points. The Estimate Normals of Point Cloud example, shows how to set the direction when the normal vectors are pointing towards the sensor.
 Hoppe, H., T. DeRose, T. Duchamp, J. Mcdonald, and W. Stuetzle. "Surface Reconstruction from Unorganized Points". Computer Graphics (SIGGRAPH 1992 Proceedings). 1992, pp. 71–78.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
For GPU code generation, the number of neighbors must be a compile-time constant.
Because of architectural and implementation differences between the GPU and CPU, the choice of neighboring points may differ during computation of normal vectors. As a result, there may be numerical mismatch between MATLAB® simulation and the output from code generation.