-
Notifications
You must be signed in to change notification settings - Fork 0
/
correct_tether_occlusion_imageV2.m
66 lines (58 loc) · 2.29 KB
/
correct_tether_occlusion_imageV2.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
function [OcclusionFlag] = correct_tether_occlusion_imageV2(init, showplot,label,sI,eI)
%% Detect the location and type of wing occlusions & remake masks
se_correction = strel('disk',150);
se = strel('square',2);
fig_correction = figure;
maskdir = init.folders.mask;
label = char(string(label));
maskdir_wing=fullfile(maskdir, ['wing_' label]);
maskdir_tether=fullfile(maskdir, ['tether_' label]);
n_frame=eI;
OcclusionFlag = uint8(zeros(n_frame,1));
for n = sI:n_frame
if ~mod(n,10) || (n == sI) || (n == n_frame)
disp(n)
end
%% Wing and tether mask directory for each frame
maskdir_wing_frame=fullfile(maskdir_wing,['frame_',num2str(n) '.png']);
maskdir_tether_frame=fullfile(maskdir_tether,['frame_',num2str(n) '.png']);
%% Read tether & wing masks
mask_wing = imread(maskdir_wing_frame);
mask_tether = imread(maskdir_tether_frame);
mask_tether_morp = bwmorph(mask_tether, 'thicken');
% Determine occlusion range for automatic corrections
mask_WingTetherInter = mask_tether_morp & mask_wing; % remove the wing and tether but keeping the interesection range
mask_WingTetherInter = imdilate(mask_WingTetherInter,se);
CC_WTmask = bwconncomp(mask_WingTetherInter);
if CC_WTmask.NumObjects >= 2
OcclusionFlag(n) = 2; % automated fix
elseif CC_WTmask.NumObjects == 1
OcclusionFlag(n) = 1; % wing is behind a portion of the tether (hard to fix)
else
OcclusionFlag(n) = 0; % no need for fix
end
% Correction statements based on occlusion flag
if OcclusionFlag(n) == 2
% performs a closing to reduce tether occlusion
bw = imclose(mask_WingTetherInter, se_correction);
mask_wing_correct = mask_wing | bw;
imwrite(mask_wing_correct,maskdir_wing_frame);
else
mask_wing_correct = mask_wing; % does nothing
end
if showplot
if n ~= sI
subplot(1,2,1) ; set(H.raw, 'CData', mask_wing);
subplot(1,2,2) ; set(H.corrct, 'CData', mask_wing_correct);
else
subplot(1,2,1) ; H.raw = imshow(mask_wing);
subplot(1,2,2) ; H.corrct = imshow(mask_wing_correct);
end
drawnow
end
end
try
close(fig_correction)
catch
end
end