I am running this piece of code with and without pre-allocating the matrices(res_stroke and res_erase) but after testing :
- Time taken without pre-allocation : 23.97s
- Time taken with pre-allocation : 21.92s
If someone could point to some possible error then it would be really helpful.
The code is for going through each frame of a video and traversing each pixel to store latest positive pixel in res_stroke and the latest negative pixel in res_erase.
mov = VideoReader('C:\Users\Harshil\Desktop\SURA\Resources\gesture.mp4');
numberOfFrames = mov.NumberOfFrames;
%num = numberOfFrames;
num = 24;
res_stroke = ones(num-1, 3);
% res_stroke = [];
res_erase = ones(num-1, 3);
% res_erase = [];
tic;
for i = 1:(num-1)
fr_in = imcrop(bwmorph(edge(rgb2gray(read(mov, i))), 'close'), [130 5 595 470]);
fr_fin = imcrop(bwmorph(edge(rgb2gray(read(mov, i+1))), 'close'), [130 5 595 470]);
%figure, imshow( imabsdiff(fr_in, fr_fin), [] );
diff = imabsdiff(fr_in, fr_fin);
[x, y] = size(diff);
progressIndication = sprintf('Processed diff[%d]',i);
disp(progressIndication);
if max(max(diff))==0
continue;
end
for l = 1:x
for m = 1:y
if diff(l, m) > 0
res_stroke(i,:) = [l, m, double(i)/double(30)];
%res_stroke = [res_stroke; l, m, double(i)/double(30)];
elseif diff(l, m) < 0
res_erase(i,:) = [l, m, double(i)/double(30)];
%res_erase = [res_erase; l, m, double(i)/double(30)];
end
end
end
end
toc

 Accepted Answer

per isakson
per isakson on 7 Jul 2016
Edited: per isakson on 7 Jul 2016

0 votes

These two line takes most of the time
&nbsp
and this one very little ( res_stroke is not large)
&nbsp
And after the first turn of the outer loop the array is "preallocated" anyhow.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!