-
Notifications
You must be signed in to change notification settings - Fork 1
/
ps_bwlabeln.m
57 lines (51 loc) · 1.68 KB
/
ps_bwlabeln.m
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
function b = ps_bwlabeln(img)
%ps_bwlabeln Label connected components in binary image
% Part of the LST toolbox, www.statistical-modeling.de/lst.html
%
% Sorry, there is no further documentation at this moment.
%
% Set borders of image to zero
img(:,:,1) = 0 .* img(:,:,1); img(:,:,end) = 0 .* img(:,:,end);
img(:,1,:) = 0 .* img(:,1,:); img(:,end,:) = 0 .* img(:,end,:);
img(1,:,:) = 0 .* img(1,:,:); img(end,:,:) = 0 .* img(end,:,:);
img = 1 .* (img ~= 0);
img2 = img;
nx = size(img, 1); ny = size(img, 2);
indx = find(img ~= 0);
%indx1 = [indx - nx, indx - 1, indx + 1, indx + nx];
%indx2 = indx - nx * ny;
%indx3 = indx + nx * ny;
indx1 = [indx - nx - 1, indx - nx, indx - nx + 1, indx - 1, indx + 1, indx + nx - 1, indx + nx + 1, indx + nx];
indx2 = [indx - nx - 1, indx - nx, indx - nx + 1, indx - 1, indx + 1, indx + nx - 1, indx + nx + 1, indx + nx, indx] - nx * ny;
indx3 = [indx - nx - 1, indx - nx, indx - nx + 1, indx - 1, indx + 1, indx + nx - 1, indx + nx + 1, indx + nx, indx] + nx * ny;
indx_nh = [indx1, indx2, indx3]';
indx_label = 0 .* indx;
indx_done = 0 .* indx;
label = 0;
lo = 1;
indx_label(lo) = label;
while any(indx_label == 0)
st = 0;
label = label + 1;
lo = find(indx_label == 0);
lo = lo(1);
indx_label(lo) = label;
indx_done(lo) = 1;
img2(indx(lo)) = 0;
while ~st
indx_nh_tmp = indx_nh(:,lo);
[~, lo] = ismember(indx_nh_tmp(find(img2(indx_nh_tmp))), indx);
lo = unique(lo);
lo = lo(lo > 0);
if isempty(lo)
st = 1;
else
indx_label(lo) = label;
indx_done(lo) = 1;
img2(indx(lo)) = 0;
end
end
end
b = img2;
b(indx) = indx_label;
end