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

5 次查看(过去 30 天)
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 个评论
Geoff Hayes
Geoff Hayes 2019-1-31
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

请先登录,再进行评论。

采纳的回答

Kevin Chng
Kevin Chng 2019-2-22
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 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by