1

I am really struggling to fit a mean plane to point cloud data in Matlab (least square). I've tried numerous other approaches as exemplified on this page, but get the same mean plane as in the image, which obviously is terribly wrong. Do you have any idea what may be wrong?

load('xyz.mat'); %//load the variable 'a' that is 44005x3 in size

x = a(:,1);
y = a(:,2);
z = a(:,3);
%//Calculate coeffs for a mean plane for points x,y,z
%//eq: xh(1) xh(2)*x + xh(3)y +  - z = 0

[X,Y] = meshgrid(min(x):10:max(x),min(y):10:max(y));

A = [ones(length(x),1) x y ];
xh = A'*A\(A'*z);
Zp = X.*(xh(2)) + Y.*xh(3) + xh(1);

%//plot mean plane
mesh(X,Y,Zp,'EdgeColor','Black');
hold on;
%//plot pointcloud
pcshow(a)
hold off;

Result from the script run

Link to matlab point cloud data

Dima
  • 38,860
  • 14
  • 75
  • 115
Hakan
  • 11
  • 2
  • 2
    Isnt the plane equation `xh(1)*x+xh(2)*y+xh(3)*z+xh(4)=0`? – Ander Biguri Mar 16 '16 at 14:58
  • This approach only works if your plane is z=f(x,y). That is, the solution will degrade then collapse as the plane approaches vertical. I like this solution better: http://stackoverflow.com/a/10904220/1401351 – Peter Mar 16 '16 at 17:09

1 Answers1

0

Try using the pcfitplane function. It does a robust fit using RANSAC.

Dima
  • 38,860
  • 14
  • 75
  • 115