-
Notifications
You must be signed in to change notification settings - Fork 3
/
tri_mask.m
65 lines (64 loc) · 1.83 KB
/
tri_mask.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
function mask = tri_mask(pts,npx)
%TRI_MASK Creates an image mask based on the two-dimensional (2-D)
% coordinates of three points which form a triangle. The
% region of interest (ROI) is defined as the space within the
% triangle.
%
% MASK = TRI_MASK(PTS,NPX) given the two-dimensional
% coordinates of three points defining a triangular region of
% interest with X coordinates in the first column and Y in the
% second column in PTS and the size of the image for the mask
% in array, NPX, creates a logical array mask for the region
% of interest, MASK. Note: If NPX has only one element, the
% image is assumed to be square (symmetrical) (NPX by NPX).
%
% NOTES: 1. M-file in_tri2d.m must be in the current
% directory or path.
%
% 04-Aug-2020 * Mack Gardner-Morse
%
%#######################################################################
%
% Check for Inputs
%
if (nargin<2)
error(' *** ERROR in TRI_MASK: Two inputs are required!');
end
%
[nr,nc] = size(pts);
if nr~=3&&nc~=2
error([' *** ERROR in TRI_MASK: Input coordinate array must have'
' three rows and two columns!']);
end
%
ndim = size(npx(:),1);
if ndim>2||ndim<1||any(npx<1)
error(' *** ERROR in TRI_MASK: Incorrect image dimensions!');
end
%
if ndim==1
npx = [npx; npx];
end
%
% Define Triangle Connectivity
%
tri = [1 2 3];
%
% Find Pixels within the Region of Interest
%
mask = false(npx(1)*npx(2),1);
%
minr = floor(min(pts));
maxr = ceil(max(pts));
idx = minr(:,1):maxr(:,1);
idy = minr(:,2):maxr(:,2);
[xg,yg] = meshgrid(idx,idy);
xym = [xg(:) yg(:)];
in_roi = in_tri2d(tri,pts,xym);
%
idr = sub2ind([npx(1) npx(2)],xym(:,2),xym(:,1));
idr = idr(in_roi);
%
mask(idr) = true;
%
return