-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgiveSequence.m
279 lines (227 loc) · 7.51 KB
/
giveSequence.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
function varargout = giveSequence(vid_path,v,opType,imProcess,varargin)
% Creates an image sequence by modifying the source video frames
% seq = motionOperation(vid_path,v,opType,imProcess)
% seq - 3D matrix of an image sequence
% vid_path - path to video file or image sequence
% v - structure of info about video (generated by defineVidObject)
% opType - designates the type of operation ('mean','mean and roi')
% imProcess - manipulation of video images ('none','enhance contrast')
%
% seq = motionOperation(vid_path,v,'mean roi sub',...
% imProcess,imRoiMean,fr_num,Centroid,Rotation,dSample,imInvert)
% Subtracts the mean image from roi of every video frame
% imRoiMean - Mean stablized image for the roi
% fr_num - sequence of frame numbers to be included in mean image
% Centroid - struction denoting the center of the roi (generated by tracker)
% Rotation - rotation structure generated by tracker
% dSample - logical that indicates whether to downsample the roi image
% imInvert - logical that indicates whether to invert the images
%
% Developed by McHenryLab at UC Irvine
%% Process inputs
% imType
if strcmp(opType,'mean roi sub')
% Mean image
imRoiMean = varargin{1};
% Extract frame numbers
if nargin>4
if length(varargin{2})>1
fr_num = varargin{2};
else
% Max number of frames to analyze
maxFrames = varargin{1};
fr_num = [];
end
else
maxFrames = 100;
fr_num = [];
end
% Define data from inputs
Centroid = varargin{3};
Rotation = varargin{4};
dSample = varargin{5};
imInvert = varargin{6};
else
error('Do not recognize opType');
end
% Define NumFrames
if isfield(v.UserData,'NumFrames')
numFrames = v.UserData.NumFrames;
else
numFrames = length(v.UserData.FileInfo);
end
% imProcessing
if strcmp(imProcess,'none')
%Do nothing
elseif strcmp(imProcess,'enhance contrast')
%Do nothing
else
error('Do not recognize imProcessing');
end
%% Preliminaries
% Check for image sequence
if ~isdir(vid_path)
error('This function requires that the video is saved as a series of images')
end
% If frame numbers not provided . . .
if isempty(fr_num)
% Proportion of video duration to exclude, from the beginning
exclude_prop = 0.2;
% Define list of frame numbers, depending on max number of frames
% requested
if (1-exclude_prop)*numFrames > maxFrames
dframe = floor(numFrames/maxFrames);
frame1 = round(exclude_prop*numFrames);
fr_num = frame1:dframe:numFrames;
clear dframe frame1
else
%fr_num = 1:numFrames;
fr_num = v.UserData.FirstFrame:v.UserData.LastFrame;
end
end
% Invert, if requested
if imInvert
imRoiMean = imcomplement(imRoiMean);
end
%% Step thru frames
% Loop thru frames
for i = 1:length(fr_num)
% Current frame
cFrame = fr_num(i);
% Index for current frame in the data
iFrame = find(Centroid.frames==cFrame);
% Current parameters
x = Centroid.x_pix(iFrame);
y = Centroid.y_pix(iFrame);
r = Centroid.r_pix;
tform = Rotation(iFrame).tform_roi;
theta = Centroid.theta;
% Current image
im = getFrame(vid_path,v,cFrame,imInvert,'gray');
% Extract roi
[bw_mask,im_roi,roi_rect,bw_roi,imStable] = giveROI('circular',...
im,x,y,r,theta,dSample,tform);
% Adjust contrast
if i==1 && strcmp(imProcess,'enhance contrast')
imStable = imadjust(adapthisteq(imStable));
if i==1
% Adjust mean image
imRoiMean = imadjust(adapthisteq(imRoiMean));
end
end
% Define mask
if i==1
roi_rect = [round(x-r) round(y-r) ceil(r*2) ceil(r*2)];
% Redefine mask with smaller r
[tmp1,tmp2,tmp3,Mask,tmp4] = giveROI('circular',...
im,x,y,r-1,theta,dSample,tform,roi_rect);
Mask = ~Mask;
% White out region outside of circle
%imRoiMean(~bw_roi) = 255;
clear roi_rect tmp1 tmp2 tmp3 tmp4
end
% Subtract background
warning off
if imInvert
imCurr = imadjust(uint8(imcomplement(imsubtract(imRoiMean,imStable))));
else
imCurr = imadjust(uint8(imcomplement(imsubtract(imStable,imRoiMean))));
end
warning on
imCurr(Mask) = 255;
% Store image
im_seq(:,:,i) = imCurr;
% Visualize
if 0
subplot(2,2,1)
imshow(imStable,'InitialMagnification','fit');
title('imStable')
subplot(2,2,2)
imshow(imRoiMean,'InitialMagnification','fit');
title('mean image')
subplot(2,2,3)
warning off
imshowpair(imStable,imRoiMean)
warning on
title('stable over mean image')
subplot(2,2,4)
imshow(im_seq(:,:,i),'InitialMagnification','fit');
title('mean image subtraction')
%title([num2str(cFrame)]
pause(0.001);
end
% Clear for next
clear x y r tform cFrame im theta im bw_mask im_roi roi_rect
clear bw_roi imStable
% Update status
disp(['motionOperation (' opType '): done ' num2str(i) ' of ' ...
num2str(length(fr_num))]);
end
% % Loop through frames
% for i = 1:length(fr_num)
%
% cFrame = fr_num(i);
%
% % Index for current frame in the data
% iFrame = find(Centroid.frames==cFrame);
%
% % Get current frame
% imCurr = getFrame(vid_path,v,cFrame);
%
% % Convert to grayscale
% imCurr = rgb2gray(imCurr);
%
% % Enhance contrast, if requested
% if strcmp(imProcess,'enhance contrast') || strcmp(imType,'mean and roi')
% imCurr = imadjust(imCurr);
%
% % roi data
% if strcmp(imType,'mean and roi')
% imStable = roiImage(imCurr,Centroid,Rotation,iFrame,dSample);
% end
% end
%
% % If mean image(s) . . .
% if strcmp(imType,'mean') || strcmp(imType,'mean and roi')
%
% % Add current to total
% imSum = imSum + double(imCurr);
%
% if strcmp(imType,'mean and roi')
% % Sum matrix for stablized roi
% imSum_roi = imSum_roi + double(imStable);
% end
% end
%
% % Clear fopr next loop
% clear imCurr
%
% % Update status
% disp(['motionImage (' imType ') : ' num2str(i) ' of ' num2str(length(fr_num))])
%
% end
%
% % Calculate mean image(s)
% if strcmp(imType,'mean') || strcmp(imType,'mean and roi')
% % Calculate mean from sum image
% imMean = uint8(round(imSum./length(fr_num)));
%
% if strcmp(imType,'mean and roi')
% imMeanROI = uint8(round(imSum_roi./length(fr_num)));
% end
% end
%% Output results
varargout{1} = im_seq;
% function imStable = roiImage(im,Centroid,Rotation,iNum,dSample)
% % Returns stabilized roi image
%
% % Extract current parameters
% x = Centroid.x_pix(iNum);
% y = Centroid.y_pix(iNum);
% r = Centroid.r_pix;
% theta = Centroid.theta;
% tform = Rotation(iNum).tform_roi;
%
% % Extract roi
% [bw_mask,im_roi,roi_rect,bw_roi,imStable] = giveROI('circular',...
% im,x,y,r,theta,dSample,tform);