MATLAB: Fitting a curve (3D) to pointcloud data

MATLABpointcloud curve 3 d points interpolation

Hello
I have a pointcloud from which I want to extract the border of a street. I have manually created sampling datapoints using the datacursor.
Now I have a list of x, y, z points from which I want to derive a curve (road boarder).
What is the best method of making such a curve given a list of x y z points. It should be smooth.
Thanks for any help!

Best Answer

Assuming that the points are listed in sequence along your curve, you can express x, y, and z as functions of the (approximate) position along the curve, using splines. To illustrate and test, I first generate a set of points to represent your point cloud. The points are randomly distributed along a 3D curve:
t = sort(rand(50,1))*10;
x = sin(t);
y = cos(1.7*t);
z = sin(t*0.22);
plot3(x,y,z,'*')
grid on
Of course, you do not know the parameter t, so we must create a parameter vector s, based on the euclidean distance between points:
s = zeros(size(x));
for i = 2:length(x)
s(i) = s(i-1) + sqrt((x(i)-x(i-1))^2+(y(i)-y(i-1))^2+(z(i)-z(i-1))^2);
end
Now you have x, y, and z as functions of s, and you can generate splines passing through the points:
ss = linspace(0,s(end),100);
xx = spline(s,x,ss);
yy = spline(s,y,ss);
zz = spline(s,z,ss);
hold on
plot3(xx,yy,zz)
hold off
If there is noise in your data and you want to smooth the curve, consider using polyfit / polyval instead of spline.
If your points are not in sequence, the problem gets MUCH harder to automate, and I recommend that you sequence them manually.