-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgiveROI.m
300 lines (219 loc) · 7.82 KB
/
giveROI.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
function varargout = giveROI(action,varargin)
% Returns features for a region-of-interest (ROI)
% giveROI(action)
% action - particular operation relating to a ROI ('define')
%
% roi = giveROI('define',shape)
% Defines a region of interest
% roi - structure of roi properties
% shape - string indicating the shape of the roi ('circular')
%
% roi = giveROI('define','circular',num_pts,r)
% num_pts - number of points for bounding the roi
% r - radius of circular roi
% Centroid - structure definining the center coordinates for the roi
%
% roi = giveROI('define','circular',num_pts,r,xC,yC)
% xC - roi x-center
% yC - roi y-center
%
% im_roi = giveROI('unstabilized',im,roi,dSample)
% returns roi image, without rotation correction
% im - full frame image
% roi - structure with stats on roi
% dSample - logical that indicates whether to downsample the roi image
%
% im_roi = giveROI('stabilized',im,roi,dSample,tform,imMean)
% returns roi image, WITH rotation correction
% tform - rotation matrix for current frame
% imMean - (optional) mean image, used for image subtraction
%
%[im_roi,bw_mask,roi_rect,bw_roi_mask,imStable] = giveROI(...
% im_roi - image within the roi (downsampled, if dSampling==1)
% bw_mask - binary image for the roi with dimensions of im
% roi_rect - [x y width height] of the roi bounding box
% bw_roi_mask - binary of mask in roi
% imStable - image stablized against rotation
%
% Developed by McHenryLab at UC Irvine
%% Translate inputs
if strcmp(action,'define')
roi.shape = varargin{1};
num_pts = varargin{2};
roi.r = varargin{3};
% Define center points
if nargin>4
roi.xCntr = varargin{4};
roi.yCntr = varargin{5};
else
roi.xCntr = 0;
roi.yCntr = 0;
end
elseif strcmp(action,'unstabilized')
im = varargin{1};
roi = varargin{2};
dSample = varargin{3};
if nargin > 4
imMean = varargin{4};
else
imMean = [];
end
elseif strcmp(action,'stabilized')
im = varargin{1};
roi = varargin{2};
dSample = varargin{3};
tform = varargin{4};
if nargin > 5
imMean = varargin{5};
else
imMean = [];
end
elseif strcmp(action,'tform stabilized')
im = varargin{1};
roi = varargin{2};
dSample = varargin{3};
tform = varargin{4};
if nargin > 5
imMean = varargin{5};
else
imMean = [];
end
else
error('Do not recognize action')
end
%% Parameters
if strcmp(action,'define')
% Angular coordinates for circular roi
roi.theta = linspace(0,2*pi,num_pts);
% Circular coordinates for new roi in image FOR
roi.xPerimG = round(floor(roi.r-1).*cos(roi.theta) + roi.xCntr);
roi.yPerimG = round(floor(roi.r-1).*sin(roi.theta) + roi.yCntr);
% Circular coordinates for new roi in roi FOR
roi.xPerimL = round(floor(roi.r-1).*cos(roi.theta) + roi.r);
roi.yPerimL = round(floor(roi.r-1).*sin(roi.theta) + roi.r);
% Bounding rectange for the roi
roi.rect = [round(roi.xCntr-roi.r) round(roi.yCntr-roi.r) ceil(roi.r*2) ceil(roi.r*2)];
else
% Maximum size of an image dimension (for downsampling)
maxSize = 250;
end
%% Create images
if ~strcmp(action,'define')
pixval = 255;
% Pad image to the left, if needed
if roi.rect(1)<1
n = abs(roi.rect(1))+1;
im = [ones(size(im,1),n).*pixval im];
roi.rect(1) = 1;
end
% Pad image to the top, if needed
if roi.rect(2)<1
n = abs(roi.rect(2))+1;
im = [ones(n,size(im,2)).*pixval; im];
roi.rect(2) = 1;
end
% Pad image to the right, if needed
if (roi.rect(1)+roi.rect(3)+1)>=size(im,2)
n = ceil(roi.rect(1)+roi.rect(3)+2 - size(im,2));
im = [im ones(size(im,1),n).*pixval];
end
% Pad image to the bottom, if needed
if (roi.rect(2)+roi.rect(4)+1)>=size(im,1)
n = ceil(roi.rect(2)+roi.rect(4)+2 - size(im,1));
im = [im; ones(n, size(im,2)).*pixval];
end
clear n
% Binary mask in image im
bw_mask = roipoly(size(im,1),size(im,2),...
[roi.rect(1) roi.rect(1)+roi.rect(3)+1 ...
roi.rect(1)+roi.rect(3)+1 roi.rect(1) ...
roi.rect(1)],...
[roi.rect(2) roi.rect(2) ...
roi.rect(2)+roi.rect(4)+1 roi.rect(2)+roi.rect(4)+1 ...
roi.rect(2)]);
% Translate image (for tform stabilized mode)
if strcmp(action,'tform stabilized')
% Get angular rotation from tform
%tformInv = invert(tform);
% Translate image
im = imtranslate(im,tform.T(3,1:2));
end
% Crop image
im_roi = imcrop(im,roi.rect);
% Check that roi is square
if size(im_roi,1)~=size(im_roi,2)
error('Image not square and needs to be');
end
if ~isempty(imMean)
im_roi = imadjust(imcomplement(imsubtract(imMean,im_roi)));
end
% White out pixels outside of circular roi
%bw_roi_mask = roipoly(size(im_roi,1),size(im_roi,2),yCirc_roi,xCirc_roi);
bw_roi_mask = roipoly(size(im_roi,1),size(im_roi,2),roi.yPerimL,roi.xPerimL);
% White out area around roi
im_roi(~bw_roi_mask) = 255;
% Reduce size (helps speed up image registration)
if dSample && (length(im)>maxSize)
% Factor by which to resize
imFactor = maxSize/length(im);
% Downsample image
im_roi = imresize(im_roi,imFactor);
bw_roi_mask = imresize(bw_roi_mask,imFactor);
% White out pixels outside of circular roi
%bw_roi = roipoly(size(im_roi,1),size(im_roi,2),round(yC*imFactor),round(xC*imFactor));
end
if strcmp(action,'stabilized')
if length(tform)>1 && isfield(tform,'T') && isempty(tform.T)
error('You need to provide tform if you want imStable');
end
% If tform exists, calculate angle . . .
if isprop(tform,'T')
% Get angular rotation from tform
tformInv = invert(tform);
rot_ang = atan2(tformInv.T(2,1),tformInv.T(1,1))*180/pi;
% Rotation angle passed as tform . . .
elseif length(tform)==1
rot_ang = tform;
end
imStable = imrotate(im_roi,-rot_ang,'bilinear','crop');
% % Coordinate system for im_roi
% R = imref2d(size(im_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);
% % R.XIntrinsicLimits = R.XWorldLimits;
% % R.YIntrinsicLimits = R.YWorldLimits;
%
%
% % Stablize image
% imStable = imwarp(im_roi,R,tform,'OutputView',R,...
% 'FillValues',255,'SmoothEdges',true);
% White out beyond roi
imStable(~bw_roi_mask) = 255;
% Deliver imStable
im_roi = imStable;
elseif strcmp(action,'tform stabilized')
rot_ang = atan2(tform.T(2,1),tform.T(1,1))*180/pi;
imStable = imrotate(im_roi,rot_ang,'bilinear','crop');
% White out beyond roi
imStable(~bw_roi_mask) = 255;
% Deliver imStable
im_roi = imStable;
end
end
%% Define output
% If defining roi . . .
if strcmp(action,'define')
varargout{1} = roi;
% Otherwise . . .
else
varargout{1} = im_roi;
if nargout>1
varargout{2} = bw_mask;
if nargout>2
varargout{3} = bw_roi_mask;
end
end
end