How to capture frame (image) using webcam at specific time interval (e.g. at 100ms or 10 frames per second) in matlab app designer?

4 views (last 30 days)
One state button is starting the webcam camera and I am previewing the video camera output in the UIAxes.
function StartCameraButtonValueChanged(app, event)
value = app.StartCameraButton.Value;
app.Camera = webcam(value);
if value == true
app.frame = snapshot(app.Camera);
app.captureCurrentVideo = image(app.UIAxes, zeros(size(app.frame), 'uint8'));
preview(app.Camera, app.captureCurrentVideo);
end
end
Now, i want to capture and store (in variables) only 10 frames per second (in the loop) from the webcam at every seconds, to get histogram of it and update the UIAxes2 to see the liv ehistogram of 10 images per seconds, how can i modify or add some code to perform this task ?

Accepted Answer

Eric Delgado
Eric Delgado on 5 Oct 2022
Edited: Eric Delgado on 5 Oct 2022
Wow... it is simple, but you have a lot of code to write. :)
You need to create some properties in your app to store the images and others to control the index, snapshot flag...
For example:
% Properties
app.imgFlag = false;
app.imgIdx = 1;
app.imgArray = repmat({[]}, 1, 5); % Five images
app.imgTime = 1; % 1 second
% StateButtonCallback
if app.StateButton.Value
app.imgFlag = true;
else
app.imgFlag = false;
end
% Update imgArray
function Fcn_imgSnapshot(app)
app.Camera = webcam;
while app.imgFlag
tic
app.imgArray(app.imgIdx) = snapshot(app.Camera);
if app.imgIdx < numel(app.imgArray)
app.imgIdx = app.imdIdx+1;
else
app.imgIdx = 1;
end
t1 = toc;
if t1 < app.imgTime
pause(app.imgTime-t1)
end
end
end
Or maybe create a timer, so you can avoid the loop.
% Properties
app.imgIdx = 1;
app.imgArray = repmat({[]}, 1, 5);
% Startup of your app
app.tmr = timer('Period', 4, 'ExecutionMode', 'fixedRate', 'TimerFcn', @app.Fcn_imgSnapshot)
% TimerButtonCallback (StateButton)
if app.TimeButton.Value
start(app.tmr)
else
stop(app.tmr);
end
% Update imgArray
function Fcn_imgSnapshot(app)
app.imgArray(app.imgIdx) = snapshot(app.Camera);
if app.imgIdx < numel(app.imgArray)
app.imgIdx = app.imdIdx+1;
else
app.imgIdx = 1;
end
end

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!