-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakevideo.m
275 lines (239 loc) · 7.95 KB
/
makevideo.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
function background = makevideo(inputFileName, varargin)
%% Create a video from any series of image inputs
% Version 3.0
% inputs:
% temp - description
% optional inputs:
% 'fps' - frames per second e.g. 'fps',20 (default)
% 'outputFileName' - file name
% outputs:
% vidout - optional output video stored as a 3D matrix
%
% Daniel Shuldman, UC Berkeley, [email protected]
%% Set Defaults and detect GPU and initial image size
tic
saveoff = false;
framerate = 15;
resize = 1;
background = 1;
newbackground = 0;
outputpathstr = 'analysis';
outputFileName = 'video';
backgroundFileName = 'background';
rollingbgflag = false;
averagebgflag = false;
usebgflag = false;
vortflag = false;
fftflag = false;
skipframesflag = false;
vidtype = 'MPEG-4';
[pathstr, firstname, ext] = fileparts(inputFileName);
firstname = strrep(firstname, '*', '');
try
gpu_num = gpuDeviceCount; %Determines if there is a CUDA enabled GPU
catch err
gpu_num = 0;
end
% Import first image (or AVI file if input file is already an AVI file).
if strcmpi(ext,'.avi')
% vidin=VideoReader('Basler_acA2040-25gm__21407047__20150122_144715129.avi');
vidin=VideoReader(inputFileName);
numframes = vidin.NumberOfFrames;
numfiles = numframes;
framerate = vidin.FrameRate;
m = vidin.Width;
n = vidin.Height;
vidin=VideoReader(inputFileName);
else
filesort = dir([firstname,'*',ext]);
numfiles = numel(filesort);
for L = 1:numfiles
[filesort(L).pathstr, filesort(L).firstname, filesort(L).ext] = ...
fileparts([filesort(L).name]);
end
if strcmpi(ext,'.mat')
% varnam = who('-file',(filesort(1, 1).name));
% Ein = load((filesort(1, 1).name),varnam{1});
% Ein = Ein.(varnam{1});
Ein = varextract(filesort(1, 1).name);
else
Ein = imread(filesort(1, 1).name);
end
[m,n]=size(Ein);
end
rect = [1,1,m-1,n-1];
while ~isempty(varargin)
switch upper(varargin{1})
case 'CPU'
varargin(1) = [];
gpu_num = 0;
case 'EXT'
ext = varargin{2};
ext = ['.', ext];
strrep(ext, '..', '.');
varargin(1:2) = [];
case 'OUTPUT'
outputFileName = varargin{2};
% [outputpathstr, outputfilename, outputext] = fileparts(firstname);
varargin(1:2) = [];
case 'SAVEOFF'
saveoff = true;
varargin(1) = [];
case 'TRIMFRAMES'
trimframes = varargin{2};
if numel(trimframes) == 2;
trimframes(3) = 1;
end
firstframe = trimframes(1);
lastframe = trimframes(2);
skipframes = trimframes(3);
numframes = floor((1+lastframe-firstframe)/skipframes);
backgroundFileName = ['background',num2str(firstframe),'to',num2str(lastframe),'skip',num2str(skipframes)];
varargin(1:2) = [];
case 'SKIPFRAMES'
skipframesflag = true;
skipframesvalue = varargin{2};
backgroundFileName = ['background_skip',num2str(skipframesvalue)];
varargin(1:2) = [];
case 'FPS'
framerate = varargin{2};
varargin(1:2) = [];
case 'AVI'
vidtype = 'Motion JPEG AVI';
varargin(1) = [];
case 'MPEG-4'
vidtype = 'MPEG-4';
varargin(1) = [];
case {'CROP','IMCROP'}
if numel(varargin{2}) ~= 4
rect = [(varargin{2}(1:2)), 1023,1023];
else
rect = [varargin{2}];
end
varargin(1:2) = [];
case 'RESCALE'
resize = varargin{2};
varargin(1:2) = [];
case 'BACKGROUND'
usebgflag = true;
background = varargin{2};
varnam=who('-file',background);
background=load(background,varnam{1});
background=background.(varnam{1});
varargin(1:2) = [];
case 'AVERAGE'
averagebgflag = true;
varargin(1) = [];
case 'VORTFLAG'
vortflag = true;
vortloc = varargin{2};
varargin(1:2) = [];
case {'ROLLINGBACKGROUND','ROLLINGBG'}
rollingbgflag = true;
varargin(1) = [];
case 'FFT'
fftflag = true;
varargin(1) = [];
otherwise
error(['Unexpected option: ' varargin{1}])
end
end
% Do NOT skip frames (unless you want to)
if ~exist('trimframes','var')
trimframes = [1 numfiles 1];
firstframe = trimframes(1);
lastframe = trimframes(2);
skipframes = trimframes(3);
numframes = floor((1+lastframe-firstframe)/skipframes);
end
if skipframesflag == true;
skipframes = skipframesvalue;
numframes = floor((1+lastframe-firstframe)/skipframes);
end
if ~exist(['.\', outputpathstr], 'dir') && ~isempty(['.\', outputpathstr])
mkdir(outputpathstr);
end
if usebgflag == true && vortflag == true
background(vortloc(2):vortloc(2)+vortloc(4),vortloc(1):vortloc(1)+vortloc(3)) = mean(background(:));
end
% GPU can't be used with stuctures. Otherwise I would use this code:
% if gpu_num > 0;
% vidout=gpuArray(vidout);
% end
% Preallocate video array
Ein = imcrop(Ein,rect);
Ein = imresize(Ein,resize);
[m,n]=size(Ein);
vidout(1:numframes) = struct('cdata',zeros(n,m,3,'uint8'),'colormap',[]);
if saveoff == false;
writerObj = VideoWriter([outputpathstr,'\',outputFileName,'_',...
num2str(uint8(rand*100))],vidtype);
writerObj.FrameRate = framerate;
open(writerObj);
end
% Create Video Array
wb = waitbar(0,'Creating Video');
for L = firstframe:skipframes:lastframe
% load(filesort(L, 1).name,'Imin'); Ein=Imin;
if rollingbgflag == true
% varnam = who('-file',['background\',(filesort(L, 1).firstname),'.mat']);
% background = load(['background\',(filesort(L, 1).firstname),'.mat'],varnam{1});
% background = background.(varnam{1});
background = varextract(['background\',(filesort(L, 1).firstname),'.mat']);
end
if exist('vidin','var')
Ein = single(readFrame(vidin));
elseif strcmpi(ext,'.mat')
% varnam = who('-file',(filesort(L, 1).name));
% Ein = load((filesort(L, 1).name),varnam{1});
% Ein = Ein.(varnam{1});
Ein = varextract(filesort(L, 1).name);
else
Ein = single(imread(filesort(L, 1).name))./background;
end
newbackground = newbackground + Ein;
Ein = imcrop(Ein,rect);
if fftflag == true
EinFFT = log10(abs(fftshift(fft2(Ein))));
minFFTlog = min(EinFFT(:));
maxFFTlog = max(EinFFT(:));
EinFFT = (EinFFT - minFFTlog)/(maxFFTlog - minFFTlog);
EinFFT = 255*adapthisteq(EinFFT);
EinFFT = imresize(EinFFT,resize);
end
Ein = imresize(Ein,resize);
maxint = 2*mean(Ein(:));
% maxint = max(Ein(:));
Ein(Ein>maxint) = maxint;
Ein = 255.*Ein./maxint;
% Eout = propagate(Ein,lambda,z,eps,zpad);
if fftflag == true
Eout = cat(2,Ein,EinFFT);
else
Eout = Ein;
end
vidout(L).cdata = uint8(abs(Eout));
vidout(L).cdata(:,:,2) = vidout(L).cdata(:,:,1);
vidout(L).cdata(:,:,3) = vidout(L).cdata(:,:,1);
writeVideo(writerObj,vidout(L));
waitbar((L-firstframe)/numframes,wb);
end
close(writerObj);
background = newbackground/numframes;
close(wb);
if gpu_num > 0;
vidout=gather(vidout);
end
if saveoff == 1;
% save([outputFileName, '.mat'],'vidout');
% imwrite(uint8(vidout), [outputFileName, '.tif'], 'tif');
end
if averagebgflag == true;
save([backgroundFileName, '.mat'],'background');
if max(background) > 256
imwrite(uint16(background), [backgroundFileName,'.tif'], 'tif');
else
imwrite(uint8(background), [backgroundFileName,'.png'], 'png');
end
end
toc2