-
Notifications
You must be signed in to change notification settings - Fork 0
/
transCoord2d.m
196 lines (153 loc) · 4.79 KB
/
transCoord2d.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
function varargout = transCoord2d(trans_type,varargin)
% Transforms 2d data from one coordinate system to another
% If data_in are coordinates (n x 2):
% coord_out = transCoord2d(trans_type,coord_in,tform);
% trans_type - type of transformation ('global to local' or
% 'local to global' for coordinates or 'bw G2L' or 'bw L2G'
% for binary images)
% tform - transformation structure for L system defined in G system,
% created by defineSystem2d
% If data_in is an image (n x m):
% im_out = transCoord2d(im_,trans_type,tform,im);
% trans_type - type of transformation ('global to local' or
% 'local to global' for coordinates or 'bw G2L' or 'bw L2G'
% for binary images)
% tform - transformation structure for L system defined in G system,
% created by defineSystem2d
%
% Code developed by McHenryLab at UC Irvine
%% Translate inputs
% Inputs for coordinate transformation
if strcmp(trans_type,'G2L') || ...
strcmp(trans_type,'L2G')
% First input needs to be tform
tform = varargin{1};
% Second input needs to be the coordinates in L frame
coord_in = varargin{2};
% Check coordinate dimensions
if size(coord_in,2)~=2
error('Coordinates need to given as a n x 2 matrix')
end
% Inputs for image transformation
elseif strcmp(trans_type,'bw L2G')
% First input needs to be tform
tform = varargin{1};
% Image in roi FOR
bw_roi = varargin{2};
% binary denoting the roi in global FOR
bw_G = varargin{3};
% Image in global FOR
bw_roi_mask = varargin{4};
else
error('trans_type not recognized');
end
% Check dimensions of tform
if tform.Dimensionality~=2
error('Code only handles 2D transformations')
end
%% Transformations
% Global to local transformation (coordinates)
if strcmp(trans_type,'global to local')
% Translate
coord_in(:,1) = coord_in(:,1) - tform.T(3,1);
coord_in(:,2) = coord_in(:,2) - tform.T(3,2);
% Rotate
coord_out = [tform.T(1:2,1:2) * coord_in']';
% Local to global transformation (coordinates)
elseif strcmp(trans_type,'local to global')
% Rotate points
coord_out = (tform.T(1:2,1:2) \ coord_in')';
% Translate global coordinates wrt origin
coord_out(:,1) = coord_out(:,1) + tform.T(3,1);
coord_out(:,2) = coord_out(:,2) + tform.T(3,2);
% Local to global transformation (coordinates)
elseif strcmp(trans_type,'bw L2G')
% Coordinate system for im_roi
R = imref2d(size(bw_roi));
% Adjust WorldLimits to restrict transformation to just rotation
% around center
R.XWorldLimits = R.XWorldLimits-mean(R.XWorldLimits);
R.YWorldLimits = R.YWorldLimits-mean(R.YWorldLimits);
% Stablize image
roiRot = imwarp(bw_roi,R,invert(tform),'OutputView',R,...
'FillValues',255,'SmoothEdges',true);
%
% % White out beyond roi
roiRot(~bw_roi_mask) = 0;
% Black out region outside of roi
imOut = logical(bw_G.*0);
imOut(bw_G) = roiRot;
% % Extract origin
% origin = tform.T(3,1:2);
%
% % Remove translation component to transformation
% tform.T(3,:) = [0 0 1];
%
% % Perform rotation
% im_rot = imwarp(im_roi,tform,'OutputView',imref2d(size(im_roi)),...
% 'FillValues',255,'SmoothEdges',true);
%
% % Perform translation
% data_out = imtranslate(im_rot,-origin);
%
%
% aaa=3;
% Global to local transformation (coordinates)
elseif strcmp(trans_type,'im G2L')
else
error('Do not recognize requested transformation')
end
%% Set outputs
if strcmp(trans_type,'G2L') || ...
strcmp(trans_type,'L2G')
varargout{1} = coord_out;
elseif strcmp(trans_type,'bw L2G')
varargout{1} = imOut;
end
%
%
% % FUNCTIONS --------------------------
%
% function ptsT = globalToLocal(tform,coord_in)
% % Assumes column vectors for coordinates
%
%
%
% %pts = [x y];
%
% % Translate
% coord_in(:,1) = coord_in(:,1) - tform.T(3,1);
% coord_in(:,2) = coord_in(:,2) - tform.T(3,2);
%
% % Rotate points
% data_out = [tform.T(1:2,1:2) * coord_in']';
%
% % Extract columns of points
% %xT = ptsT(:,1);
% %yT = ptsT(:,2);
% end
%
% function ptsT = localToGlobal(tform,pts)
% % Assumes columns vectors for coordinates
%
% % Check dimensions
% if tform.Dimensionality~=2
% error('Code only handles 2D transformations')
% end
%
% % Loop thru columns of coordinates
% i = 1;
%
% %pts = [x(:,i) y(:,i)];
%
% % Rotate points
% data_out = (tform.T(1:2,1:2) \ coord_in')';
%
% % Translate global coordinates wrt origin
% data_out(:,1) = data_out(:,1) + tform.T(3,1);
% data_out(:,2) = data_out(:,2) + tform.T(3,2);
%
%
% clear ptsT pts
% %end
% end