forked from cao13jf/3DMMS_new
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seedCCA.m
57 lines (47 loc) · 1.23 KB
/
seedCCA.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
function seeds = seedCCA(seeds)
% seeds = ImageCCA(seeds, T) performs connected component analysis on given
% binary image seeds.
%
% Input:
% seeds: label image as seeds
% permIds: permute ids (neighboring seeds having different ids)
% conn: connectivity
%
% Output:
% seeds: label image as output seeds
%
permIds = true;
conn = sel(ndims(seeds) == 3, 26, 8);
tmp = false(size(seeds) + ones(1, ndims(seeds))*2);
if ndims(seeds) == 3
tmp(2:end-1, 2:end-1, 2:end-1) = seeds ~= 0;
else
tmp(2:end-1, 2:end-1) = seeds ~= 0;
end
opts = struct('backgroundValue', 0, 'conn', conn);
seeds = vigraConnectedComponents(uint16(tmp), opts);
if ndims(seeds) == 3
seeds = seeds(2:end-1, 2:end-1, 2:end-1);
else
seeds = seeds(2:end-1, 2:end-1);
end
if permIds
seeds = PermuteSegmentId(seeds);
end
seeds = uint16(seeds);
end
%%
function segments = PermuteSegmentId(segments)
ids = unique(segments(:));
ids = ids(ids ~= 0);
I = randperm(length(ids));
idsPerm = ids(I);
for x = 1:size(segments, 1)
for y = 1:size(segments, 2)
for z = 1:size(segments, 3)
if segments(x, y, z) == 0, continue; end
segments(x, y, z) = idsPerm(segments(x, y, z));
end
end
end
end