-
Notifications
You must be signed in to change notification settings - Fork 0
/
motionImage.m
307 lines (225 loc) · 7.92 KB
/
motionImage.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
299
300
301
302
303
304
305
function varargout = motionImage(vid_path,v,imType,varargin)
% Creates a single image from a video sequence
% im = motionImage(vid_path,v,imType,imProcessing)
% im - output image
% vid_path - path to video file or image sequence
% v - structure of info about video (generated by defineVidObject)
% imType - designates the type of image ('mean','mean and roi')
% imProcess - manipulation of video images ('none','enhance contrast')
%
% im = motionImage(vid_path,v,'mean',imProcess,imInvert,fr_num)
% Returns mean image of provided frame numbers.
% fr_num - column vector of frame numbers to be included in mean image,
% or single integer for the max number of frames at an equal interval
%
% im = motionImage(vid_path,v,'mean',imProcess,imInvert,fr_num)
% Returns mean image 90% brightest intensity over recording. Works well
% for dark object moving thru light background.
% fr_num - column vector of frame numbers to be included in mean image,
% or single integer for the max number of frames at an equal interval
%
% im = motionImage(vid_path,v,'mean roi',imProcess,fr_num,S,dSample)
% Returns mean image for roi at even intervals or frnums.
% S - structure for roi coorindate trasnformation (generated by
% defineSystem2d('roi'))
% dSample - logical that indicates whether to downsample the roi image
%
% im = motionImage(vid_path,v,'bw static',fr_num,B)
% Returns an image of the static elements in a binary image sequence
% B - structure of blob data
% Developed by McHenryLab at UC Irvine
%% Process inputs
% imType
if strcmp(imType,'mean') || ...
strcmp(imType,'mean bright') || ...
strcmp(imType,'mean color')
% Image processing
imProcess = varargin{1};
% Image processing
imInvert = varargin{2};
% Extract frame numbers
if nargin>5
if length(varargin{3})>1
fr_num = varargin{3};
else
% Max number of frames to analyze
maxFrames = varargin{3};
fr_num = [];
end
else
maxFrames = 100;
fr_num = [];
end
elseif strcmp(imType,'mean roi')
% Image processing
imProcess = varargin{1};
imInvert = varargin{2};
% Extract frame numbers
if nargin>4
if length(varargin{3})>1
fr_num = varargin{3};
else
% Max number of frames to analyze
maxFrames = varargin{3};
fr_num = [];
end
else
maxFrames = 100;
fr_num = [];
end
S = varargin{4};
dSample = varargin{5};
elseif strcmp(imType,'im static')
fr_num = varargin{1};
S = varargin{2};
imInvert = varargin{3};
imProcess = 'none';
elseif strcmp(imType,'bw static')
fr_num = varargin{1};
B = varargin{2};
imProcess = [];
imInvert = 0;
if length(fr_num)<2
error('fr_num must be a vector of at least 2 numbers')
end
else
error('Do not recognize imType');
end
if isfield(v.UserData,'NumFrames')
numFrames = v.UserData.NumFrames;
elseif isfield(v.UserData,'FileInfo')
numFrames = length(v.UserData.FileInfo);
else
numFrames = floor(v.Duration*v.FrameRate);
end
% imProcessing
if strcmp(imProcess,'none') || isempty(imProcess)
%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 = v.UserData.FirstFrame+[frame1:dframe:numFrames]-1;
clear dframe frame1
else
%fr_num = 1:numFrames;
fr_num = v.UserData.FirstFrame:v.UserData.LastFrame;
end
end
if strcmp(imType,'bw static')
% Create sum image based on first frame
imCurr = getFrame(vid_path,v,fr_num(1),imInvert,'gray');
else
% Create sum image based on first frame
imCurr = getFrame(vid_path,v,fr_num(1),imInvert,'gray');
end
clear imCurr
%% Step thru frames
% Loop through frames
for i = 1:length(fr_num)
% Current frame number
cFrame = fr_num(i);
% Load current image
if strcmp(imType,'bw static')
% Create sum image based on first frame
imCurr = getFrame(vid_path,v,cFrame,imInvert,'gray');
elseif strcmp(imType,'im static')
% Create sum image based on first frame
imCurr = getFrame(vid_path,v,cFrame,imInvert,'gray');
elseif strcmp(imType,'mean') || strcmp(imType,'mean bright')
% Get current frame
imCurr = getFrame(vid_path,v,cFrame,imInvert,'gray');
elseif strcmp(imType,'mean color')
% Get current frame
imCurr = getFrame(vid_path,v,cFrame,imInvert,'rgb');
elseif strcmp(imType,'mean roi')
% Get current frame
im = getFrame(vid_path,v,cFrame,imInvert,'gray');
imCurr = giveROI('stabilized',im,S.roi(i),0,S.tform(:,:,i));
end
% Convert to grayscale
%imCurr = rgb2gray(imCurr);
if strcmp(imType,'bw static')
% Start with blank
currIm = logical(zeros(size(imCurr)));
end
% Enhance contrast, if requested
if strcmp(imProcess,'enhance contrast')
imCurr = imadjust(imCurr);
end
% If mean image(s) . . .
if strcmp(imType,'mean') || ...
strcmp(imType,'mean roi') || ...
strcmp(imType,'mean bright')
% Add current to total
%imSum = imSum + double(imCurr);
imStack(:,:,i) = double(imCurr);
elseif strcmp(imType,'mean color')
imStack.R(:,:,i) = imCurr(:,:,1);
imStack.G(:,:,i) = imCurr(:,:,2);
imStack.B(:,:,i) = imCurr(:,:,3);
elseif strcmp(imType,'bw static')
% Loop thru blobs
for k = 1:length(B(i).propsG),
% Score pixels with blobs
currIm(B(i).propsG(k).PixelIdxList) = 1;
end
% Store resulting image
bwStack(:,:,i) = currIm;
elseif strcmp(imType,'im static')
% Store resulting image
bwStack(:,:,i) = imCurr;
end
% Clear fopr next loop
clear imCurr currIm
% Update status
disp(['motionImage (' imType ') : ' num2str(i) ' of ' num2str(length(fr_num))])
end
% Calculate mean image(s)
if strcmp(imType,'mean') || ...
strcmp(imType,'mean roi')
imOut = uint8(round(mean(imStack,3)));
elseif strcmp(imType,'mean color')
imOut(:,:,1) = uint8(round(mean(imStack.R,3)));
imOut(:,:,2) = uint8(round(mean(imStack.G,3)));
imOut(:,:,3) = uint8(round(mean(imStack.B,3)));
elseif strcmp(imType,'mean bright')
imOut = uint8(round(quantile(imStack,0.9,3)));
elseif strcmp(imType,'im static')
error('Need to finish this line of code');
% Create image from motion score
elseif strcmp(imType,'bw static')
% image of the pixels that are most static
imOut = imcomplement(uint8(sum(double(bwStack),3)./length(fr_num).*255));
end
%% Output results
varargout{1} = imOut;
% 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);
%