问题
I was wondering if it was possible for someone to provide me with some code examples for working with scattered XYZ point data in MATLAB Curve Fitting Toolbox? I would like to fit a surface to points that approximate a cylinder.
Thanks.
回答1:
In Matlab R2015b and above, You can use pcfitcylinder to fit a cylinder to a pointCloud object. Let's start with producing an example data to see how it works:
[theta, r, h] = meshgrid(0:.1:6.28, 1, 0:.2:4); % making a cylinder
r = r + 0.05 * randn(size(r)); % adding some radial noise
[x, y, z] = pol2cart(theta, r, h); % transforming the coordinate system
P = (rotx(60) * [x(:), y(:), z(:)]')'; % rotating the data points around x axis
figure;
scatter3(P(:, 1), P(:, 2), P(:, 3))
axis equal
The point cloud object is created as follows:
ptCloud = pointCloud(P);
Then the model can be fitted:
maxDistance = 0.02;
model = pcfitcylinder(ptCloud, maxDistance);
hold on
plot(model)
Depending on your data, to get a reasonable cylinder you might need to take a look at pcfitcylinder
and consider including other input arguments.
来源:https://stackoverflow.com/questions/39589384/how-to-fit-a-cylinder-to-scattered-3d-xyz-point-data-in-matlab