-
Notifications
You must be signed in to change notification settings - Fork 3
/
fibatLocal.m
60 lines (47 loc) · 2.17 KB
/
fibatLocal.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
function imgSep = fibatLocal(imgDiff, imgThresh, C, tree_id)
%% FIBATLOCAL Applies FIBAT at the local level of an image
%
% BW2 = fibatLocal(IMG, BW, C, STR) recursively applies FIBAT, using
% parameters C, to each ROI in the binary image BW and to each of the
% resulting ROIs in order to separate overlapping ROIs. STR is used to
% display separation progress
%
imgSep = zeros(size(imgThresh));
[imgLabel, num] = bwlabel(imgThresh);
for i=1:num
% Display separation progress
if isempty(tree_id)
current_tree_id = [num2str(i),'(',num2str(num),')'];
else
current_tree_id = [tree_id,'->',num2str(i),'(',num2str(num),')'];
end
fprintf(['\t',current_tree_id,'\n']);
% Get an individual ROI
imgMaskBW = (imgLabel == i);
imgMaskBW = dilate(imgMaskBW);
% Crop
roiStats = regionprops(imgMaskBW,'BoundingBox');
rect = roiStats.BoundingBox;
row_min = max(floor(rect(2)),1);
row_max = min(floor(rect(2)+ceil(rect(4))), size(imgThresh,1));
col_min = max(floor(rect(1)),1);
col_max = min(floor(rect(1)+ceil(rect(3))), size(imgThresh,2));
crop_imgDiff = imgDiff(row_min:row_max, col_min:col_max);
crop_imgThresh = imgMaskBW(row_min:row_max, col_min:col_max);
crop_imgMask = crop_imgThresh .* crop_imgDiff;
% Perform FIBAT segmentation
threshValFinal = fibat(crop_imgMask, C);
crop_imgSep = applyThresh(crop_imgMask, threshValFinal);
crop_imgSep = morphologicalOps(crop_imgSep);
crop_imgSep = removeSmall(crop_imgSep, C.minSize);
numSeparated = bwconncomp(crop_imgSep);
imgSep(logical(imgMaskBW)) = 0;
if numSeparated.NumObjects > 1
% FIBAT separated the ROI so keep the output and continue recursively applying FIBAT
imgSep(row_min:row_max, col_min:col_max) = imgSep(row_min:row_max, col_min:col_max) | fibatLocal(crop_imgDiff, crop_imgSep, C, current_tree_id);
else
% FIBAT could not separate the ROI so exit the recursive loop
imgSep(row_min:row_max, col_min:col_max) = imgSep(row_min:row_max, col_min:col_max) | imgThresh(row_min:row_max, col_min:col_max);
end
end
end