-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateROIMask.m
48 lines (48 loc) · 2.02 KB
/
createROIMask.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
function [fgo, roi, bw_mask1] = createROIMask(fg, bg, roitype)
% has 3 options - rectangular, elliptical and freeform.
%check roitype
if(roitype == 1)
roi = drawrectangle('Label', 'ROI', 'color', [1 0 0]);
elseif(roitype == 2)
roi = drawellipse('Label', 'ROI', 'color', [1 0 0]);
else
roi = drawfreehand('Label', 'ROI', 'color', [1 0 0]);
end
%to handle difference in sizes of images
%create mask from the selected ROI
bw_mask = createMask(roi);
[rb, cb, bch] = size(bg);
[rf, cf, fch] = size(fg);
%resize/ pad fg based on its original size.
if(rf*cf>rb*cb)
fgo=upscale(fg, [rb,cb,bch]);
bw_mask1 = upscale(bw_mask, [rb,cb]);
elseif(rf*cf<rb*cb)
fgo = zeros(rb,cb,bch);
bw_mask1 = zeros(rb,cb);
if rf<rb && cf<cb
% center the smaller fg and its mask by zero padding to bg size
fgo(floor(rb/2)+(1:rf)-floor(rf/2), floor(cb/2)+(1:cf)-floor (cf/2),:) = fg;
bw_mask1(floor(rb/2)+(1:rf)-floor(rf/2), floor(cb/2)+(1:cf)-floor(cf/2)) = bw_mask;
elseif rf<rb && cf>cb
fac = 1-((cf-cb)/cb);
fg = downscale(fg,fac);
bw_mask = downscale(bw_mask,fac);
[rf, cf, fch] = size(fg);
% center the smaller fg and its mask by zero padding to bg size
fgo(floor(rb/2)+(1:rf)-floor(rf/2), floor(cb/2)+(1:cf)-floor (cf/2),:) = fg;
bw_mask1(floor(rb/2)+(1:rf)-floor(rf/2), floor(cb/2)+(1:cf)- floor(cf/2)) = bw_mask;
elseif rf>rb && cf<cb
fac = 1-((rf-rb)/rb);
fg = downscale(fg,fac);
bw_mask = downscale(bw_mask,fac);
[rf, cf, fch] = size(fg);
% center the smaller fg and its mask by zero padding to bg size
fgo(floor(rb/2)+(1:rf)-floor(rf/2), floor(cb/2)+(1:cf)-floor (cf/2),:) = fg;
bw_mask1(floor(rb/2)+(1:rf)-floor(rf/2), floor(cb/2)+(1:cf)- floor(cf/2)) = bw_mask;
end
else
fgo = fg;
bw_mask1 = bw_mask;
end
end