-
Notifications
You must be signed in to change notification settings - Fork 0
/
applyMask.m
59 lines (47 loc) · 1.27 KB
/
applyMask.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
function imOut = applyMask(im,xMask,yMask,clr,bwInvert)
% Applies mask to color or grayscale image
if nargin<4
clr = [255 255 255];
end
if nargin<5
bwInvert = 0;
end
if ~isempty(xMask)
% Color
if size(im,3)==3
% Get individual channels
imR = im(:,:,1);
imG = im(:,:,2);
imB = im(:,:,3);
if bwInvert==1
% Make binary mask of tank region
bwMask = roipoly(imR,xMask,yMask);
else
% Make binary mask of tank region
bwMask = ~roipoly(imR,xMask,yMask);
end
% Set white mask in each
imR(bwMask) = clr(1);
imG(bwMask) = clr(2);
imB(bwMask) = clr(3);
% Package into image
imOut(:,:,1) = uint8(imR);
imOut(:,:,2) = uint8(imG);
imOut(:,:,3) = uint8(imB);
% Grayscale
else
% Set default output
imOut = im;
if bwInvert==1
% Make binary mask of tank region
bwMask = roipoly(im,xMask,yMask);
else
% Make binary mask of tank region
bwMask = ~roipoly(im,xMask,yMask);
end
% Apply mask
imOut(bwMask) = 255;
end
else
imOut = im;
end