i dont get an output for this using a GUI and a toggle button. Whats wrong?

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)

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

thats the thing.. i jst dont get anything... my webcam turns on and when i press my toggle button it turns back off. i dont get any error msg.
but when i use the debugger i get the output figures. why is this so?
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.
i get my output figures when i use the debugger but not when i run the program completely.why is this so?
i removed the GUI and used a simple coding jst defining webcam parameters and the main function. i still dont get anything
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.
here is the simplified version of my code
clc;
source = videoinput('winvideo');
set(source, 'ReturnedColorSpace', 'RGB');
set(source, 'FramesPerTrigger', 3);
set(source, 'TriggerRepeat', 50);
triggerconfig(source, 'manual');
start(source);
thresh = 15/255;
trigger(source);
bg = getdata(source,1,'double');
bg=bg(:,:,:,1);
bg_bw = rgb2gray(bg); % convert background to greyscale
% ----------------------- set frame size variables -----------------------
fr_size = size(bg);
width = fr_size(2);
height = fr_size(1);
f = zeros(height, width);
for i=1:50
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
f = 255 * ((fr_diff1 > thresh) & (fr_diff2 > thresh));
figure(1),subplot(2,1,1),imshow(fr1)
subplot(2,1,2),imshow(uint8(f))
end
stop(source); delete(source); clear (source);
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.

Asked:

on 31 Jan 2013

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!