Is there any way i can speed up my processing?

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일

0 개 추천

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월 5일
thanks for that Jan Simon..
i used the profile instruction and found that the function name imaqdevice.wait uses a total of 17.773s of total time. what does this mean? and is it possible 2 reduce this?
Jan
Jan 2013년 4월 5일
And is the total time of the program days or 18 seconds?
Actually it seems like the time is spent by waiting for a device. Then it might be a solution to increase the speed of this device, but this is most like not a Matlab problem and therefore off-topic.
i got the elapsed time as 18s... i eliminated the codes wait(). and now it is imaqdevice.getdata... i understand it is because of the code
fr=getdata(source,3,'double')
is there any alternative?
Cedric
Cedric 2013년 4월 5일
편집: Cedric 2013년 4월 5일
EDIT: discard this comment. GETDATA is compiled, so what I wrote originally is invalid; also, the OP is using a class from the Image Acquisition Toolbox, which I don't have and can therefore not investigate.
Original comment - Unless GETDATA was poorly written, the bottleneck is likely to be the device itself or a slow communication. If you have the source code of GETDATA, you can profile it as well. Type
profile viewer
in your command window, and run your script from there. If GETDATA is not compiled, you'll be able to see what takes time in this function, and you might realize that it is a communication related function call.
Sanjeev
Sanjeev 2013년 4월 6일
i dont understand
Cedric
Cedric 2013년 4월 6일
I meant discard my comment. I wrote it before realizing that you cannot profile what happens inside GETDATA as there is no source code for it. But GETDATA will use the VIDEOINPUT class (from the Image Acquisition Toolbox, which I don't have) to effectively get data, so someone who has this toolbox might be able to help further.
Jan
Jan 2013년 4월 6일
See [EDITED]
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.

댓글을 달려면 로그인하십시오.

질문:

2013년 4월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by