MATLAB Answers

0

How do I actively control a webcam in app designer using a toggle switch?

Asked by Ty Olmstead on 31 Jan 2019
Latest activity Answered by Kevin Chng on 22 Feb 2019
I have an application where I am using a stereo camera system that inputs one large image from two cameras into Matlab. I take a snapshot and then sparse the image into two images, crop, offset, display, etc using a subroutine ShowImages. I would like to be able to view the images live while I align the system that is collecting them. Currently I am using a while loop that is linked to a toggle switch. When the toggle switch changes the while loop breaks. Although the code seems to work, it is extremely slow. It can take 20 seconds after the switch is changed to off before the system responds. Is there a way to get the system to respond faster?
% Value changed function: CamsLiveSwitch
function CamsLiveSwitchValueChanged(app, event)
while isequal(app.CamsLiveSwitch.Value,'On')
app.AquiringImagesLamp.Color = [0 1 0];
% Display image and stretch to fill axes
ShowImages(app) % This function manipulates the imagery for display
if isequal(app.CamsLiveSwitch.Value,'Off')
break;
end
end
app.AquiringImagesLamp.Color = [1 0 0];

  1 Comment

Ty - I haven't used App Designer for GUIs but if you want to interrupt the "tight" while loop you could add a pause to it so that the change to the state of the toggle button is "captured". For example,
while isequal(app.CamsLiveSwitch.Value,'On')
app.AquiringImagesLamp.Color = [0 1 0];
% Display image and stretch to fill axes
ShowImages(app) % This function manipulates the imagery for display
if isequal(app.CamsLiveSwitch.Value,'Off')
break;
end
pause(0.001);
end

Sign in to comment.

Products


Release

R2018b

1 Answer

Answer by Kevin Chng on 22 Feb 2019
 Accepted Answer

The algorithm logic given by Geoff Hayes is right.
Code below is the code you may consider to put into the callback function of your 'toggle switch' in app designer.
value = app.Switch.Value;
if strcmp(value,'On')
cam=webcam;
while(strcmp(value,'On'))
img=snapshot(cam);
imshow(img,'Parent',app.UIAxes);
drawnow;
value = app.Switch.Value;
if strcmp(value,'Off')
break;
end
end
end

  0 Comments

Sign in to comment.