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

 採用された回答

per isakson
per isakson 2016 年 7 月 7 日
編集済み: per isakson 2016 年 7 月 7 日

0 投票

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.

1 件のコメント

harshil meena
harshil meena 2016 年 7 月 7 日
Thank you for your help 'per isakson'

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by