I have my 3D points(lidar data) as a text file. can anyone suggest me a method for fitting the plane.

 Accepted Answer

Try this:
P = bsxfun(@times, rand(49, 3), [1 10 100]); % Create Matrix [x, y, z]
B = [P(:,1), P(:,2), ones(size(P,1),1)] \ P(:,3); % Linear Regression
xv = [min(P(:,1)) max(P(:,1))];
yv = [min(P(:,2)) max(P(:,2))];
zv = [xv(:), yv(:), ones(2,1)] * B; % Calculate Regression Plane
figure
stem3(P(:,1), P(:,2), P(:,3), '.')
hold on
patch([min(xv) min(xv) max(xv) max(xv)], [min(yv) max(yv) max(yv) min(yv)], [min(zv) min(zv) max(zv) max(zv)], 'r', 'FaceAlpha',0.5)
hold off
grid on
xlabel('X')
ylabel('Y')
producing (with this set of random data):
Plane fitting using 3D points - 2019 03 26.png

7 Comments

Hello star strider,
I already have 3D points and I'm need of a multiple planes for those points and not a single plane considering all the points. Can you suggest me any codes sir?
I have no idea what you are asking.
If you have multiple sets of points ((Nx3) matrices), use my code for each set.
I'm doing research on 'Automated Building Extraction'. I have 3D point data for an urban region with no vegetation and ground points. Now the points I have is mostly on building roof. To extract each building roof separately, I need to fit separate planes on each building roof with the points that lie on the respective roofs. Since the points on each roof will be planar. I hope you understood my query. Can you help me to find a solution sir? Looking forward for your help sir.
Just use my code with the roof coordinates instead of the random data.
It will be best to do each plane of each roof separately.
lidarFile = '/Users/Ashi/Downloads/las_data/Subset/LIDAR_subset.txt';
fp = fopen(lidarFile, 'r');
ALS = fscanf(fp, '%f %f %f', [3 inf]);
fclose(fp);
ALS = ALS';
x=ALS(:,1);
y=ALS(:,2);
z =ALS(:,3);
% Create Matrix [x, y, z]
B = [ALS(:,1), ALS(:,2), ones(size(ALS,1),1)] \ ALS(:,3); % Linear Regression
xv = [min(ALS(:,1)) max(ALS(:,1))];
yv = [min(ALS(:,2)) max(ALS(:,2))];
zv = [xv(:), yv(:), ones(2,1)] * B; % Calculate Regression Plane
figure
stem3(ALS(:,1), ALS(:,2), ALS(:,3), '.')
hold on
patch([min(xv) min(xv) max(xv) max(xv)], [min(yv) max(yv) max(yv) min(yv)], [min(zv) min(zv) max(zv) max(zv)], 'r', 'FaceAlpha',0.5)
hold off
grid on
xlabel('X')
ylabel('Y')
This is the code I'm using for my points. I have included my result image and the points I'm using and the original orthoimage.
@Star Strider When I use your method, the plane fit for a 3D dataset is not a good fit, maybe because instead of regression you can calculate mean of the points and find a basis of the plane and fit the plane to the data.
When I used Plane Fit (Affine fit function to fit the data) https://nl.mathworks.com/matlabcentral/fileexchange/43305-plane-fit . I got a better fit

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!