|||
这是我写的label编码,试了一下挺快的。
clc;
clear all;
bw = [0 0 0 0 0 0 0
0 1 1 0 0 0 0
0 1 0 1 1 1 0
0 1 0 0 1 0 0
0 0 0 0 0 0 0]
figure(1)
imshow(uint8(bw),[]);
labelIm=zeros(size(bw));
[HH,WW]=size(bw);
offsets = [-1; HH; 1; -HH];
pixNum=HH*WW;
indx=1;
n=1;
label=0;
while 1
if bw(n)==1
label=label+1;
labelIm(n)=label;
bw(n)=0;
neighbors=n+offsets';
neighbors(find(neighbors<1))=[];
neighbors=bw(neighbors).*neighbors;
neighbors(find(neighbors==0))=[];
idx=neighbors(:);
clear neighbors;
while 1
for m=1:length(idx)
labelIm(idx(m))=label;
bw(idx(m))=0;
neighbors(m,:)=idx(m)+offsets';
end;
clear idx;
neighbors(find(neighbors<1))=[];
neighbors=bw(neighbors).*neighbors;
neighbors(find(neighbors==0))=[];
idx=neighbors(:);
clear neighbors;
if isempty(idx)
break;
end;
end;
end;
n=n+1;
if n>pixNum
break;
end;
end;
labelIm
figure(2)
imshow(uint8(labelIm),[]);
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-23 10:30
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社