-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefineSystem2d.m
215 lines (152 loc) · 5.36 KB
/
defineSystem2d.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
function varargout = defineSystem2d(coordType,varargin)
% Defines coordinate local system (L) within global system (G)
% S = defineSystem2d(coordType)
% S - structure that defines a coordinate system
% coordType - type of data used to define coordinate system
% ('x-axis','y-axis','roi')
%
% S = defineSystem2d('x-axis',origin,axCoord)
% origin - a vector of 2 coordinates that define origin L in G
% axCoord - a vector of 2 coordinates defining x-axis for L in G
%
% S = defineSystem2d('y-axis',origin,axCoord)
% origin - a vector of 2 coordinates that define origin L in G
% axCoord - a vector of 2 coordinates defining y-axis for L in G
%
% S = defineSystem2d('roi',roi0,Centroid,Rotation)
% Defines a region-of-interest within an image
% roi0 - structure defining region-of-interest for initial frame
% Centroid - strcuture with centroid data
% Rotation - (optional) strcuture for rotation data
%
% Code developed by McHenryLab at UC Irvine
%% Translate inputs
% If using axis coordinates
if strcmp(coordType,'x-axis') || strcmp(coordType,'y-axis')
% Set origin
origin = varargin{1};
% Set axis
axCoord = varargin{2};
% Check dimensions of origin
if length(origin)~=2
error('Origin needs to have a length of 2')
end
% Check dimensions of axCoord
if length(axCoord)~=2
error('axCoord needs to have a length of 2')
end
% Make origin a column vector
if size(origin,1)>size(origin,2)
origin = origin';
end
% Make axCoord a column vector
if size(axCoord,1)>size(axCoord,2)
axCoord = axCoord';
end
% Translate wrt origin
axCoord(1) = axCoord(1) - origin(1);
axCoord(2) = axCoord(2) - origin(2);
elseif strcmp(coordType,'roi')
% Region of interest rectangle
roi0 = varargin{1};
%
Centroid = varargin{2};
if nargin > 3
Rotation = varargin{3};
else
Rotation = [];
end
else
error('coordType not recognized');
end
%% Define system from x-axis coordinate
if strcmp(coordType,'x-axis')
% Define xaxis
xaxis = axCoord;
% Normalize to create a unit vector
xaxis = [[xaxis./norm(xaxis)] 0];
% Define y-axis
yaxis = [-xaxis(2) xaxis(1) 0];
% Normalize to create a unit vector
yaxis = yaxis./norm(yaxis);
% Define z-axis
zaxis = cross(xaxis,yaxis);
end
%% Define system from y-axis coordinate
if strcmp(coordType,'y-axis')
% Define xaxis
yaxis = axCoord;
% Normalize to create a unit vector
yaxis = [[yaxis./norm(yaxis)] 0];
% Define x-axis
xaxis = [yaxis(2) -yaxis(1) 0];
% Normalize to create a unit vector
yaxis = yaxis./norm(yaxis);
% Define y-axis
xaxis = [-xaxis(2) xaxis(1) 0];
% Define z-axis
zaxis = cross(xaxis,yaxis);
end
%% Define system for an roi
if strcmp(coordType,'roi')
% Store centroid data
S.frames = Centroid.frames;
S.xCntr = Centroid.x;
S.yCntr = Centroid.y;
% For the 'advanced rotation' method by tracker . . .
if length(Rotation)==1 && isfield(Rotation,'tform_roi')
S.tform = Rotation.tform_roi;
S.ref_frame = Rotation.ref_frame;
%S.rot_deg = Rotation_net;
elseif length(Rotation)==1 && isfield(Rotation,'rot_ang')
S.ang = Rotation.rot_ang;
S.tform = [];
S.ref_frame = Rotation.ref_frame;
%S.rot_deg = Rotation_net;
elseif ~isempty(Rotation) && length(Rotation)~=length(Centroid.x)
error('mismatch in length of data sources');
end
% Set reference as first frame
S.ref_frame = zeros(length(Centroid.x),1);
S.ref_frame(1) = 1;
% Reference rotation
ref_rot = 0;
% Loop thru frames, store varying parameters
for i = 1:length(Centroid.x)
if ~isfield(S,'tform') && ~isempty(Rotation) && length(Rotation)~=1
S.tform(:,:,i) = Rotation(i).tform_roi;
elseif ~isfield(S,'tform')
S.tform = [];
end
if ~isempty(S.tform)
% Get angular rotation since last reference frame
tformInv = invert(S.tform(:,:,i));
rot_ang = atan2(tformInv.T(2,1),tformInv.T(1,1))*180/pi;
% If this is a reference frame, update reference angle
if Rotation.ref_frame(i)
% Add current angle to prior reference
ref_rot = ref_rot + rot_ang;
% Zero rotation angle for storage
rot_ang = 0;
end
% Store net angle
S.ang(i,1) = ref_rot + rot_ang;
end
% Number of roi points
numroipts = length(roi0.xPerimG);
% Cooridnates of centroid
xC = Centroid.x(i);
yC = Centroid.y(i);
% Current roi
S.roi(i) = giveROI('define','circular',numroipts,roi0.r,xC,yC);
end
end
%% Package system for output
if strcmp(coordType,'x-axis') || strcmp(coordType,'y-axis')
% Create rotation matrix (from inertial axes to local axes)
R = [xaxis; yaxis; [origin 1]];
% Format trans matrix for matlab
S.tform = affine2d(R);
end
% Output
varargout{1} = S;