[Matlab]基于matlab的ransac平面拟合程序

输入:①数据 ②抽样次数N ③距离阈值t ④数量阈值T
输出:最终估计的模型
程序流程:
1。data :数据
2。取样本 :确定模型参数p所需要的最小数据数n,随机取n个数据作为一个样本J
3。建模型:根据样本J建立模型Mp(J)。
4。判断距离:根据模型Mp(J)判断所有数据点到模型的距离。
5。记录:记录 距离小于t的个数total 和 距离小于t的点的索引。
6。判断: 若total>数量阈值T :则用距离小于t的点重新估计模型 重复3-5一次。
若total<数量阈值T:则跳出。
7。记录最大total和此时的模型作为最佳模型。
8。循环N次。
9。输出

函数ransac_fitplane

function [a,b,c,d]=ransac_fitplane(data,N,t,T)
figure;plot3(data(1,:),data(2,:),data(3,:),'o');hold on; % 显示数据
iter = N; %抽样次数N
number = size(data,2); % 总数
maxNum=0;     %符合拟合模型的数据的个数
for i=1:iter %循环次数
     sampleidx = randperm(number); sampleidx =sampleidx(1,1:3);
     sample = data(:,sampleidx); %取样本
     [a1,a2,a3,a4]=get_nice_plane(sample);%拟合直线方程 z=ax+by+c
     plane = [-a1/a3,-a2/a3,-1,-a4/a3];%建模型
     mask=abs(plane*[data; ones(1,size(data,2))]);    %求每个数据到拟合平面的距离
     total=sum(mask<t); %计算数据距离平面小于一定阈值的数据的个数
     index= mask<t;
     if total>T
         nsample=data(:,index);
         [a1,a2,a3,a4]=get_nice_plane(nsample);
         plane = [-a1/a3,-a2/a3,-1,-a4/a3];  %z=ax+by+c
         mask=abs(plane*[data; ones(1,size(data,2))]);
         total=sum(mask<t);              %计算数据距离平面小于一定阈值的数据的个数
     end;
     if total>maxNum   %记录最大total
         maxNum=total;
         bestplane=plane;%最好的拟合平面
         bestindex=index;
         bestplane2=[a1,a2,a3,a4];
    end  
end
%显示符合最佳拟合的数据
maxNum %最大一致集
avgerror=abs(bestplane*[data; ones(1,size(data,2))]);
avgerror=sum(avgerror(find(avgerror<t)))/maxNum  %计算一致集内平均误差
a=bestplane2(1);b=bestplane2(2);c=bestplane2(3);d=bestplane2(4);
% 图形绘制
temp1=data(1,bestindex);
temp2=data(2,bestindex);
xfit = min(temp1):0.2:max(temp1);
yfit = min(temp2):0.2:max(temp2);
[XFIT,YFIT]= meshgrid (xfit,yfit);
ZFIT = bestplane(1)*XFIT+bestplane(2)*YFIT+bestplane(4);
mesh(XFIT,YFIT,ZFIT);grid on;
xlabel('X');
ylabel('Y');
end

函数 拟合平面

function [a,b,c,d]=get_nice_plane(data)
planeData=data';
% 协方差矩阵的SVD变换中,最小奇异值对应的奇异向量就是平面的方向
xyz0=mean(planeData,1);
centeredPlane=bsxfun(@minus,planeData,xyz0);
[~,~,V]=svd(centeredPlane);
a=V(1,3);
b=V(2,3);
c=V(3,3);
d=-dot([a b c],xyz0);
end

参数t=1 N=3000 T=200

参数t=0.5 N=3000 T=300

把参数调大之后明显好多了。

调试代码

clc;clear all;close all;
mu=[0 0 0];
S=[2 0 4;0 4 0;4 0 8]; 
data1=mvnrnd(mu,S,400);
%外点
mu=[2 2 2];
S=[8 1 4;1 8 2;4 2 8];
data2=mvnrnd(mu,S,500);
data=[data1',data2'];%% 相当于原始数据
[a,b,c,d]=ransac_fitplane(data,3000,0.5,300)
以下是使用RANSAC算法拟合平面Matlab程序示例: ```matlab % 生成数据 n = 200; % 数据点数量 inliers_ratio = 0.7; % 内点比例 noise = 0.1; % 噪声 X = randn(n, 3); % 随机生成点 % 生成一个平面 plane_normal = randn(3,1); plane_normal = plane_normal./norm(plane_normal); plane_offset = randn(1); d = abs(X*plane_normal - plane_offset) < noise; % 标记内点 % 添加一些噪声 d = d & (rand(n,1) < inliers_ratio); % RANSAC参数 max_iters = 1000; inlier_threshold = 0.1; % RANSAC best_model = []; best_score = 0; for iter = 1:max_iters % 选择随机样本 sample_indices = randperm(n, 3); sample_points = X(sample_indices, :); % 计算平面方程 plane_normal = cross(sample_points(2,:)-sample_points(1,:), sample_points(3,:)-sample_points(1,:)); plane_normal = plane_normal./norm(plane_normal); plane_offset = -plane_normal*sample_points(1,:)'; % 计算内点数量 dists = abs(X*plane_normal - plane_offset); inliers = dists < inlier_threshold; score = sum(inliers); % 更新最好的模型 if score > best_score best_model = [plane_normal', plane_offset]; best_score = score; end end % 输出结果 disp(['平面法向量:' num2str(best_model(1:3)')]); disp(['平面偏移量:' num2str(best_model(4))]); % 绘制结果 figure; scatter3(X(:,1), X(:,2), X(:,3), 20, d, 'filled'); hold on; [x,y] = meshgrid(-3:0.1:3, -3:0.1:3); z = (-best_model(1)*x - best_model(2)*y - best_model(4))/best_model(3); surf(x,y,z); axis equal; ``` 在本示例中,我们生成200个随机点,并在其中生成一个平面。然后我们添加一些噪声,并使用RANSAC算法拟合平面。最终,我们输出最佳模型的法向量和偏移量,并在图形中绘制结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值