曲线拟合:在 Matlab 中将具有不同 length/number 点的多条曲线合并为一条 3D 曲线

curve fitting: a number of curves with different length/number of points into one curve in 3D in Matlab

假设我有许多不同长度的曲线(每条曲线中的点数和点距离都不同)。我能否在 3D space 中找到最适合这组线条的曲线?

Matlab 中的代码示例将不胜感激。

示例数据集:

第一条曲线有10个点。

   18.5860   18.4683   18.3576   18.2491   18.0844   17.9016   17.7709   17.6401   17.4617   17.2726
   91.6178   91.5711   91.5580   91.5580   91.5701   91.6130   91.5746   91.5050   91.3993   91.2977
   90.6253   91.1090   91.5964   92.0845   92.5565   93.0199   93.5010   93.9785   94.4335   94.8851

第二条曲线有8个点

   15.2091   15.0894   14.9765   14.8567   14.7360   14.6144   14.4695   14.3017
   90.1138   89.9824   89.8683   89.7716   89.6889   89.6040   89.4928   89.3624
   99.4393   99.9066  100.3802  100.8559  101.3340  101.8115  102.2770  102.7296

想要的曲线是可以代表这两条存在曲线的曲线。

我想把这些曲线做成点散布并从中拟合出一条线。但是我只能从网上的许多代码片段中得到一条直线。

我是不是遗漏了什么或者有人可以提供一些提示。谢谢。

如果没有更多细节,很难想出万无一失的解决方案,但这里有一种适用于所提供示例数据的方法。我找到了所有点的最佳拟合线,然后参数化了沿着该最佳拟合线的所有点。然后我分别对每个维度进行了最小二乘多项式拟合。这产生了一个三维参数曲线,似乎很好地拟合了数据。 请注意,多项式最小二乘法以外的曲线拟合方法可能更适合某些情况——只需将首选拟合函数替换为 polyfit 和 polyval。

希望对您有所帮助!

clear;
close all;

pts1=[18.5860   18.4683   18.3576   18.2491   18.0844   17.9016   17.7709   17.6401   17.4617   17.2726;
   91.6178   91.5711   91.5580   91.5580   91.5701   91.6130   91.5746   91.5050   91.3993   91.2977;
   90.6253   91.1090   91.5964   92.0845   92.5565   93.0199   93.5010   93.9785   94.4335   94.8851]';
pts2=[ 15.2091   15.0894   14.9765   14.8567   14.7360   14.6144   14.4695   14.3017;
   90.1138   89.9824   89.8683   89.7716   89.6889   89.6040   89.4928   89.3624;
   99.4393   99.9066  100.3802  100.8559  101.3340  101.8115  102.2770  102.7296]';

%Combine all of our curves into a single point cloud
X = [pts1;pts2];

%=======================================================
%We want to first find the line of best fit
%This line will provide a parameterization of the points
%See accepted answer to 

% calculate centroid
x0 = mean(X)';

% form matrix A of translated points
A = [(X(:, 1) - x0(1)) (X(:, 2) - x0(2)) (X(:, 3) - x0(3))];

% calculate the SVD of A
[~, S, V] = svd(A, 0);

% find the largest singular value in S and extract from V the
% corresponding right singular vector
[s, i] = max(diag(S));
a = V(:, i);
%=======================================================
a=a / norm(a);
%OK now 'a' is a unit vector pointing along the line of best fit.
%Now we need to compute a new variable, 't', for each point in the cloud
%This 't' value will parameterize the curve of best fit.
%Essentially what we're doing here is taking the dot product of each
%shifted point (contained in A) with the normal vector 'a'
t = A * a;

tMin = min(t);
tMax = max(t);

%This variable represents the order of our polynomial fit
%Use the smallest number that produces a satisfactory result
polyOrder = 8;

%Polynomial fit all three dimensions separately against t
pX = polyfit(t,X(:,1),polyOrder);
pY = polyfit(t,X(:,2),polyOrder);
pZ = polyfit(t,X(:,3),polyOrder);

%And that's our curve fit: (pX(t),pY(t),pZ(t))

%Now let's plot it.
tFine = tMin:.01:tMax;
fitXFine = polyval(pX,tFine);
fitYFine = polyval(pY,tFine);
fitZFine = polyval(pZ,tFine);

figure;
scatter3(X(:,1),X(:,2),X(:,3));
hold on;
plot3(fitXFine,fitYFine,fitZFine);
hold off;