MATLAB Answers

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

12 views (last 30 days)
Ty Olmstead
Ty Olmstead on 31 Jan 2019
Commented: Abdulrahman on 7 Feb 2020
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

Geoff Hayes
Geoff Hayes on 31 Jan 2019
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.

Accepted Answer

Kevin Chng
Kevin Chng on 22 Feb 2019
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

  1 Comment

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!