-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfindMarkers.m
executable file
·86 lines (77 loc) · 2.4 KB
/
findMarkers.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function [allMarkers, allMarkersOrg] = findMarkers(map, binMask, tArea, dSize, initH)
[dx, dy] = size (map);
allMarkers = zeros (dx,dy);
allMarkersOrg = zeros (dx,dy);
h = initH;
while 1
if max(max(map)) > 0
hmap = imhmin (map, h);
currMarkers = imregionalmin (hmap);
currMarkers = maskBasedOnArea (currMarkers, tArea);
currMarkers = maskBasedOnMap (currMarkers, binMask);
currMarkers = eliminateOverlappingAreas (allMarkers, currMarkers, dx, dy);
[allMarkers, allMarkersOrg, inserted] = insertNewMarkers (allMarkers, allMarkersOrg, currMarkers, dSize);
if inserted == 0
return;
else
h = h + 1;
end
else
return;
end
end
end
function markers = maskBasedOnArea (markers, tArea)
if tArea ~= -1
markers = bwareaopen (markers, tArea);
end
end
function markers = maskBasedOnMap (markers, binMask)
[L n] = bwlabel (markers);
for i=1:n
t = L == i;
if (sum (sum (binMask & t)) / sum(sum(t))) < 1
markers (L == i) = 0;
end
end
end
function curr = eliminateOverlappingAreas (prev, curr, dx, dy)
cc = bwlabeln (curr);
maxCC = max (max (cc));
overlappingCC = zeros (maxCC,1);
for i=1:dx
for j=1:dy
if cc (i,j) > 0
if prev (i,j) > 0
overlappingCC (cc (i,j)) = overlappingCC (cc (i,j)) + 1;
end
end
end
end
eliminateCC = zeros (maxCC,1);
for i=1:maxCC
if overlappingCC (i) > 0
eliminateCC (i) = 1;
end
end
for i=1:dx
for j=1:dy
if cc (i,j) > 0 && eliminateCC (cc (i,j)) == 1
curr (i,j) = 0;
end
end
end
end
function [allMarkers, allMarkersOrg, inserted] = insertNewMarkers (allMarkers, allMarkersOrg, currMarkers, dSize)
maxNo = max (max (allMarkers));
cc = bwlabeln (currMarkers);
if dSize ~= -1
se = strel ('disk', dSize);
cc2 = imdilate (cc, se);
end
inserted = max (max (cc));
cc2(cc2>0) = cc2 (cc2>0) + maxNo;
allMarkers (cc2>0) = cc2 (cc2>0);
cc(cc>0) = cc (cc>0) + maxNo;
allMarkersOrg (cc>0) = cc (cc>0);
end