Is there any way i can speed up my processing?

2 次查看(过去 30 天)
this is my code for Moving Object detection using both background subtraction and 3frame differencing. But my final output really lags from the heavy processing. Is there any way i can get an output without the lag?
here's my code
%%camera parameters
clc;
source = videoinput('winvideo');
set(source, 'ReturnedColorSpace', 'grayscale');
set(source, 'FramesPerTrigger', 3);
set(source, 'TriggerRepeat', 100);
triggerconfig(source, 'manual');
start(source);
thresh = 15/255;
trigger(source);
wait(source,5,'logging')
bg = getdata(source,1,'double');
bg=bg(:,:,:,1);
bgfilt=medfilt2(bg);
%%----------------------- set frame size variables -----------------------%
fr_size = size(bg);
width = fr_size(2);
height = fr_size(1);
f1 = zeros(height, width);
f2 = zeros(height, width);
flushdata(source)
for i=1:50
trigger(source)
wait(source,5,'logging')
fr = getdata(source,3,'double');
fr1=fr(:,:,:,1);
fr2=fr(:,:,:,2);
fr3=fr(:,:,:,3);
fr_diff1 = abs((fr1) - (fr2)); % First frame Difference
fr_diff2 = abs((fr2) - (fr3)); % Second frame difference
bg_fr_diff = abs((double(bg)) - (double(fr1)));
f1 = 255 * ((bg_fr_diff > thresh));
f2 = 255 * ((fr_diff1 > thresh) & (fr_diff2 > thresh));
bg=fr1;
f=bitand(f1,f2);
f=medfilt2(f,[5,5]);
subplot(3,1,1);
imshow(uint8(f1));
title('background subtraction');
subplot(3,1,2);
imshow(uint8(f2));
title('Frame differencing');
subplot(3,1,3);
imshow(uint8(outp));
title('AND OUTPUT');
end
stop(source);
delete(source);
  2 个评论
Jan
Jan 2013-4-5
Please care about a properly formatted code. You got the instruction in one of your former threads already, therefore I cannot reconsider, why to decide to ignore it. Please note, that ignoring the suggestion of the contributors might cause that they ignore your questions.
Cedric
Cedric 2013-4-5
Have you profiled your code? If not, use multiple tic/toc or the profiler. I would tend to think that the central part of the FOR loop, which is your part of the processing, is not what takes the most time.

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2013-4-5
编辑:Jan 2013-4-6
Use the profile to find the bottleneck of your code at first. It would be senseless to spend time for improving a code sections, which uses 1% of the total processing time.
And, please Sanjeev, care about the suggestions to format your code.
[EDITED] You found out, that the main time is spent in:
fr = getdata(source,3,'double');
Then this is the line, where the most work is done. There is no alternative for this command.
  8 个评论
Sanjeev
Sanjeev 2013-4-8
thanks for that.. what i did 2 simplify that was i changed the framepertrigger to 1 and called the trigger each time to accept the individual frames.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by