i dont get an output for this using a GUI and a toggle button. Whats wrong?
Info
This question is closed. Reopen it to edit or answer.
Show older comments
clc;
source= videoinput('winvideo');
set(source, 'ReturnedColorSpace', 'RGB');
set(source, 'FramesPerTrigger', 1);
set(source, 'TriggerRepeat', inf);
triggerconfig(source, 'manual');
if get(handles.startstop,'Value')
start(source);
thresh = 15/255;
pause(2);
trigger(source);
bg = getdata(source,1,'double');
bg=bg(:,:,:,1);
bg_bw = rgb2gray(bg);
set(handles.startstop,'String','Stop');
set(handles.startstop,'Value',1);
handles.source = source;
guidata(gcbo, handles);
drawnow();
fr_size = size(bg);
width = fr_size(2);
height = fr_size(1);
f = zeros(height, width);
else
set(handles.startstop,'String','Start');
set(handles.startstop,'Value',0);
source = handles.source;
stop(source);
handles.source = [];
guidata(gcbo, handles)
end
while(get(handles.startstop,'Value'));
trigger(source);
fr = getdata(source,3,'double');
fr1=fr(:,:,:,1);
fr_bw1 = rgb2gray(fr1);
trigger(source);
fr2=fr(:,:,:,2);
fr_bw2 = rgb2gray(fr2);
trigger(source);
fr3=fr(:,:,:,3); % read in frame-i+1
fr_bw3 = rgb2gray(fr3); % convert frame-i+1 to grayscale
fr_diff1 = abs((fr_bw1) - (fr_bw2)); % First frame Difference cast operands as double to avoid negative overflow
fr_diff2 = abs((fr_bw2) - (fr_bw3)); % Second frame difference cast operands as double to avoid negative overflow
for j=1:width % if fr_diff > thresh pixel in foreground
for k=1:height
if ((fr_diff1(k,j) > thresh) && (fr_diff2(k,j) > thresh))
f(k,j) = 255;
else
f(k,j) = 0;
end
end
end
subplot(1,2,1); imshow(fr1); title('REAL TIME VIDEO');
subplot(1,2,2); imshow(uint8(f)); title('DETECTED MOVING OBJECT'); bg_bw=fr_bw1;
end
Answers (1)
Jan
on 31 Jan 2013
Please explain the occurring problem with any detail: Do you get an infinite loop, an error message, is the code too slow or does the figure disappear before you can see it?
The debugger can help you to find out, what happens: Set a breakpoint in the code and step through the program line by line.
Btw., you can omit the double FOR loop:
f = 255 * ((fr_diff1 > thresh) & (fr_diff2 > thresh));
6 Comments
And what does pressing the toggle button trigger? Is the posted code the callback function? Did you try to run the code in the debugger already? Or you could use some DISP commands to find out, in which line the program stops working as expected.
E.g. we cannot know if get(handles.startstop,'Value') is a valid trigger to control the loop.
Sanjeev
on 31 Jan 2013
Jan
on 31 Jan 2013
Sorry, Sanjeev, this is still a vague description as a text and we cannot reproduce your problem. I suggest to spend more time with the debugging because the forum users cannot guess the reason of the behviour.
Sanjeev
on 31 Jan 2013
Image Analyst
on 31 Jan 2013
Don't double space all your code after you paste it in. That is not necessary to get it to show up with one line of code per statement. Simply make sure there is a blank line before it, then highlight it all and click {}Code.
This question is closed.
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!