DBSCAN原理:
参考这个http://t.csdn.cn/q16orhttp://t.csdn.cn/q16or
下面程序是总程序里的其中一段,点云数据直接粘贴出来了,自己输入就行。
聚类数据:
三维坐标,每列分别代表 x,y,z
X =
-0.254205050780579 0.512071291734422 0.159239070859418
-0.254205050780579 0.512071291734422 0.159239070859418
0.128733266186260 0.752334668900700 0.112377287149361
0.0114677010563783 0.747833439545474 0.189267009935766
-0.0689416158111029 0.746215322626126 0.183352415875273
-0.102327744111227 0.734438680371702 0.212925386177737
-0.275224825353079 0.695440620742921 0.189267009935766
-0.301298357660834 0.677562131550977 0.212925386177737
0.128582440809606 0.751453224941670 0.118291881209854
-0.0688062063382698 0.744749667632868 0.189267009935766
-0.102089851835522 0.732731252043674 0.218839980238229
-0.300597897071261 0.675986930232845 0.218839980238229
0.128423689805010 0.750525462537908 0.124206475270346
-0.0686662182755925 0.743234454562713 0.195181603996258
-0.102089851835522 0.732731252043674 0.218839980238229
-0.273489571874279 0.691055966288529 0.207010792117244
-0.299876541775286 0.674364740733569 0.224754574298722
0.128423689805010 0.750525462537908 0.124206475270346
-0.0686662182755925 0.743234454562713 0.195181603996258
-0.101844863244437 0.730972891209024 0.224754574298722
-0.246382615954376 0.687600883564807 0.248412950540693
-0.272873984296604 0.689500493933917 0.212925386177737
-0.299876541775286 0.674364740733569 0.224754574298722
-0.0812692467033105 0.811380952855264 0.159239070859418
-0.118200572436418 0.802820978930093 0.178347759362548
0.120218834330128 0.816529059659713 0.0955434425156510
-0.0811427034448379 0.810117562411746 0.165608633693795
-0.117992835176254 0.801410022729737 0.184717322196925
0.136165352783682 0.877566893730215 -0.0614207844743471
0.136233685691654 0.878007289885054 -0.0545962528660863
0.136346159355566 0.878732167115917 -0.0409471896495647
0.136346159355566 0.878732167115917 -0.0409471896495647
0.858122313359829 0.529632384246686 -0.0309378766241156
0.858122313359829 0.529632384246686 -0.0309378766241156
0.858298983025968 0.529741424618956 -0.0232034074680867
0.858298983025968 0.529741424618956 -0.0232034074680867
0.908600096498642 0.560787230378844 -0.0327577517196518
0.908787158498084 0.560902684890659 -0.0245683137897389
0.908787158498084 0.560902684890659 -0.0245683137897389
0.908920750641390 0.560985137851355 -0.0163788758598258
1.72969042875245 0.610627420255816 0.141040319904056
1.72861635666401 0.610248243810351 0.155144351894462
1.72743922573615 0.609832684812142 0.169248383884867
1.72743922573615 0.609832684812142 0.169248383884867
1.77196512422499 0.670402922593092 0.131031006878607
-1.61486138952764 0.996691118093331 0.0727950038214484
1.81743470412587 2.38786677060281 0.394457926957473
1.81553211421144 2.38536702124486 0.417661334425560
1.81351863449105 2.38272157747365 0.440864741893647
1.83851353337867 2.36769944700739 0.417661334425560
1.82769874788821 2.44979636030660 0.425850772355473
1.82567177500513 2.44707946246053 0.449509148597444
1.84907782340264 2.42944160840451 0.449509148597444
1.84907782340264 2.42944160840451 0.449509148597444
1.84691142253798 2.42659524664815 0.473167524839414
1.86663772483970 2.50198907758019 0.385813520253676
1.86997627572963 2.50646397795877 0.337586830221967
1.86078084760139 2.49413868289246 0.458153555301241
1.86078084760139 2.49413868289246 0.458153555301241
1.88903416062500 2.58301357925391 0.171978196528172
1.88819888368633 2.58187144444237 0.196546510317911
1.98716580533279 2.50830246835073 0.171978196528172
1.98628713738432 2.50719336865804 0.196546510317911
1.86450124944805 2.60077763398276 0.171978196528172
1.86450124944805 2.60077763398276 0.171978196528172
1.90037179285629 2.65081316334622 0.125116412818114
1.92112220450213 2.62688989170709 0.250232825636229
2.04701913684803 2.53236466763712 0.225209543072606
1.92401627471065 2.63084716405491 0.175162977945360
2.02307023252106 2.55362287548504 0.200186260508983
1.89902905036376 2.64894018276022 0.175162977945360
2.02307023252106 2.55362287548504 0.200186260508983
1.89818935397038 2.64776889708804 0.200186260508983
2.06349575821288 2.60465004473537 0.0509565026750137
1.93064843030957 2.69304579877299 0.254782513375069
2.10847131204861 2.55621747158055 0.254782513375069
2.01183524870370 2.64328315476818 0.101913005350028
2.06155392945615 2.60219896901207 0.152869508025041
2.20359638613608 2.46164389668545 0.726130163118947
2.14840522335738 2.60463158146799 0.207465760891128
0 3.53038743728151 0.464068149361733
0 3.53038743728151 0.464068149361733
0 3.52669163488654 0.491366275794776
0 3.52669163488654 0.491366275794776
0.109422042073990 3.56656462481511 0.610568094552398
0 3.58922722790287 0.471802618517762
0.109422042073990 3.56656462481511 0.610568094552398
0 3.58546982880132 0.499555713724689
0.109273003988380 3.56170679220836 0.638321189759325
0.109117124651768 3.55662597195347 0.666074284966252
0.109117124651768 3.55662597195347 0.666074284966252
0.197171915755471 3.66885308740109 0.197456447865678
0.0840627939300897 3.65407319169004 0.423120959712169
-0.193494138983844 3.60041929138275 0.733409663501092
0.197171915755471 3.66885308740109 0.197456447865678
0.197171915755471 3.66885308740109 0.197456447865678
-0.193795895984817 3.60603419906584 0.705201599520281
0.196985877027060 3.66539139377961 0.253872575827301
0.0838145875510867 3.64328406331208 0.507745151654602
-0.192514625813888 3.58219311599680 0.818033855443526
0.196875333353937 3.66333446546465 0.282080639808112
0.0838145875510867 3.64328406331208 0.507745151654602
-0.192853565554155 3.58849989710077 0.789825791462715
0.196875333353937 3.66333446546465 0.282080639808112
0.0837216346526025 3.63924355170688 0.535953215635414
-0.192853565554155 3.58849989710077 0.789825791462715
0.228872591961979 3.72473201101036 0.229304262037563
0 3.72294045902710 0.343956393056343
-0.114165928372806 3.72118956817033 0.343956393056343
0.228757792676587 3.72286373762089 0.257967294792258
-0.114165928372806 3.72118956817033 0.343956393056343
0.143277254718166 3.73505945937216 -0.0859890982640861
0 3.72547738668952 0.315293360301648
0.228487449189264 3.71846409748675 0.315293360301648
0.0284465836151188 3.71045289355781 0.458608524075125
-0.113989845545203 3.71545022377441 0.401282458565734
3.09931634948513 3.18286798674166 0.272981264330432
2.96788599059059 3.31543402447041 0.102367974123912
3.09931634948513 3.18286798674166 0.272981264330432
2.96727508991674 3.31475158556066 0.136490632165216
2.96858400885086 3.31621378268792 0.0341226580413038
2.96788599059059 3.31543402447041 0.102367974123912
3.13089809457111 3.21530111523346 0.449509148597443
3.00259043264406 3.35420245706826 0.276621014521504
1.71889449631655 4.14171514221905 0.484086775412632
3.13089809457111 3.21530111523346 0.449509148597443
3.00259043264406 3.35420245706826 0.276621014521504
1.71889449631655 4.14171514221905 0.484086775412632
1.71582567837225 4.13432075601568 0.553242029043008
4.62421392906393 3.22408255125086 0.0864440670379696
4.62353426733385 3.22360867924616 0.129666100556955
4.67220304909525 3.25754140218559 0.131031006878607
4.45492331259274 3.55105294836339 0.0436770022928689
4.67124133695398 3.25687087971808 0.174708009171476
4.45492331259274 3.55105294836339 0.0436770022928689
4.09249478630370 4.02486283696276 -0.936325736653380
4.27880569358945 3.84693330916219 -0.847151856972106
4.09755372565998 4.02983817305303 -0.891738796812743
4.13425493718435 4.06593286591136 -0.945880080904945
4.13425493718435 4.06593286591136 -0.945880080904945
4.13425493718435 4.06593286591136 -0.945880080904945
4.13936549837079 4.07095897073724 -0.900838172290424
4.17059422659059 4.10167161772170 -1.00093130254492
4.22613616646958 4.06585987172803 -0.909937547768105
4.18117727108161 4.11207976842145 -0.909937547768105
1.86561624221911 8.18137491852552 0.775266790698425
1.61129000794024 8.25118890930904 0.581450093023819
1.94353901872753 8.22394882103370 0.780726415985033
1.68913636584987 8.30419288173762 0.455423742657936
原始点云图像:
聚类程序:
X; %
epsilon= 0.3;
MinPts= 3;
IDX1=DBSCAN(X,epsilon,MinPts);
%% DBSCAN
function [IDX, isnoise]=DBSCAN(X,epsilon,MinPts)
C=0; %
n=size(X,1);
IDX=zeros(n,1);
D = pdist2(X,X); % 互相之间的距离
visited=false(n,1);
isnoise=false(n,1);
for i=1:n
if ~visited(i)
visited(i)=true;
Neighbors=RegionQuery(i); % 找出小于半径的值 返回 ID
if numel(Neighbors)<MinPts % 如果元素个数小于邻域中数据对象数目阈值 判定为噪点
% X(i,:) is NOISE
isnoise(i)=true;
else
C = C+1; % 定为一个簇
ExpandCluster(i,Neighbors,C); % 扩展群集
end
end
end
function ExpandCluster(i,Neighbors,C)
IDX(i)=C;
k = 1;
while true
j = Neighbors(k);
if ~visited(j)
visited(j)=true;
Neighbors2=RegionQuery(j);
if numel(Neighbors2)>=MinPts
Neighbors=[Neighbors Neighbors2]; %#ok
end
end
if IDX(j)==0
IDX(j)=C;
end
k = k + 1;
if k > numel(Neighbors)
break;
end
end
end
function Neighbors=RegionQuery(i)
Neighbors=find(D(i,:)<=epsilon);
end
% 画图
PlotClusterinResult(X, IDX)
end
function PlotClusterinResult(X, IDX)
k=max(IDX);
Colors=hsv(k);
Legends = {};
for i=0:k
Xi=X(IDX==i,:);
if i~=0
Style = 'x';
MarkerSize = 8;
Color = Colors(i,:);
Legends{end+1} = ['Cluster #' num2str(i)];
else
Style = 'o';
MarkerSize = 6;
Color = [0 0 0];
if ~isempty(Xi)
Legends{end+1} = 'Noise';
end
end
if ~isempty(Xi)
plot3(Xi(:,1),Xi(:,2),Xi(:,3),Style,'MarkerSize',MarkerSize,'Color',Color,'LineWidth',2);
end
hold on;
end
hold off;
axis equal;
grid on;
legend(Legends);
legend('Location', 'NorthEastOutside');
end
处理后点云图像: