我使用 RANSAC 作为我稳健的回归方法。我在这里找到了一个简洁的工具箱,它执行 Marco Zuliani 的 RANSAC。我看到有直线和平面的示例,但是如果像多元回归中那样有许多自变量怎么办。反正有没有修改代码来处理这个?
到目前为止,我尝试的是修改 3D 代码以处理 N 维。当我这样做时,我得到了所有的点作为内点,我知道这可能是不正确的。这是对数据的过度拟合。以下是我厌倦的修改。
因为test_RANSAC_plane.m
我刚刚添加了更多行X
对于estimate_plane.m
:
function [Theta, k] = estimate_plane(X, s)
% cardinality of the MSS
k = size(X,1);
if (nargin == 0) || isempty(X)
Theta = [];
return;
end;
if (nargin == 2) && ~isempty(s)
X = X(:, s);
end;
% check if we have enough points
N = size(X, 2);
if (N < k)
error('estimate_plane:inputError', ...
'At least k points are required');
end;
A = [];
for i=1:k
A = [A transpose(X(i, :))];
end
A = [A ones(N, 1)];
[U S V] = svd(A);
Theta = V(:, k+1);
return;
对于error_plane.m
:
function [E T_noise_squared d] = error_plane(Theta, X, sigma, P_inlier)
% compute the squared error
E = [];
k = size(X,1);
den = 0;
if ~isempty(Theta) && ~isempty(X)
for i=1:k
den = den + Theta(i)^2;
end
sum = Theta(1)*X(1,:);
for j=2:k
sum = sum + Theta(j)*X(j,:);
end
sum = sum + Theta(j+1);
E = (sum).^2 / den;
end;
% compute the error threshold
if (nargout > 1)
if (P_inlier == 0)
T_noise_squared = sigma;
else
d = k;
% compute the inverse probability
T_noise_squared = sigma^2 * chi2inv_LUT(P_inlier, d);
end;
end;
return;