Is there any way i can speed up my processing?

1 view (last 30 days)
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 Comments
Jan
Jan on 5 Apr 2013
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 on 5 Apr 2013
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.

Sign in to comment.

Answers (1)

Jan
Jan on 5 Apr 2013
Edited: Jan on 6 Apr 2013
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 Comments
Sanjeev
Sanjeev on 8 Apr 2013
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!