1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| plot3(data(:,1),data(:,2),data(:,3),'ro'); xlabel('x') ylabel('y') zlabel('z')
K=2000; sigma=0.01; pretotal=0; planeNumber=10; k=1; i=0; bestplane =zeros(planeNumber,4); planeNo=1; breakFlag = 0; while planeNo <= planeNumber while pretotal < 30 && k< K SampIndex=floor(1+(size(data,1)-1)*rand(3,1)); sampFinderCounter = 0; while(sum(isnan([data(SampIndex(1),:) data(SampIndex(2),:) data(SampIndex(3),:)])) ~= 0 || length([data(SampIndex(1),:) data(SampIndex(2),:) data(SampIndex(3),:)]) ~= length(unique([data(SampIndex(1),:) data(SampIndex(2),:) data(SampIndex(3),:)]))) SampIndex=floor(1+(size(data,1)-1)*rand(3,1)); sampFinderCounter = sampFinderCounter +1; if(sampFinderCounter > 5000) breakFlag = 1; break; end end if breakFlag == 1 break; end samp1=data(SampIndex(1),:); samp2=data(SampIndex(2),:); samp3=data(SampIndex(3),:);
plane = planeCreate([samp1;samp2;samp3]); mask(planeNo,:)=abs(plane*[data ones(size(data,1),1)]'); total=sum(mask(planeNo,:)<sigma);
if total>pretotal pretotal=total; bestplane(planeNo,:)=plane; end k=k+1; end if pretotal<30 || breakFlag ~= 0 disp(breakFlag); break; end mask(planeNo,:)=abs(bestplane(planeNo,:)*[data ones(size(data,1),1)]')<sigma; hold on; color(planeNo,:) = rand(1,3); disp(['画第' num2str(planeNo) '个平面']); for i=1:length(mask) if mask(planeNo, i) plot3(data(i,1),data(i,2),data(i,3),'o','color',color(planeNo,:)); data(i,:) = NaN; end end planeNo = planeNo+1; pretotal = 0; k=1; end
|