capture a snapshot and saving it to a matrix variable

I am trying to use my webcam to record and take snapshots and be able to save it as a matrix variable. This is the best code I found online so far but I keep getting an error. Could someone help me iether with a better code that will work on app designer or how to fix this error?
% Button pushed function: OnButton
function OnButtonPushed(app, event)
clear all; clc; close all
cam = webcam('FaceTime HD Camera');
frame = snapshot(cam);
im = image(app.UIAxes, zeros(size(frame), 'uint8'));
axis(app.UIAxes,'image');
preview(cam,im);
pause;
Reference to a cleared variable app.
Error in Final/OnButtonPushed (line 19)
im = image(app.UIAxes, zeros(size(frame), 'uint8'));
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 309) Error while evaluating Button PrivateButtonPushedFcn.,

Answers (1)

Are you using it inside App designer? Remove the line
clear all; clc; close all

7 Comments

Yes that worked, do you know how to code for the webcma to stop recording?
You can delete() the webcam object.
delete(cam)
Yes thank you! So I created another button that when pressed will record the snapshot from the camera. Do you know how to code for that button to save the frames of the snapshot to a matrix so that I can continue using that matrix for later analysis?
You can create properties in your app to share the data between different callback functions (a property can be defined from toolbar in the code view). For example, define a property named 'cam', and then inside the callback function, use app.cam
app.cam = webcam('FaceTime HD Camera');
Using this, all of the callback functions will be able to use app.cam variable. Then you can use
img = snapshot(app.cam)
inside the callback function of the other button to get the image from the webcam. If you want to share the images with other callback functions, then define 'img' as a property too and directly modify it
app.img{end+1} = snapshot(app.cam);
Also, do you know how to code for a lamp to turn a different color n when the button "on" (which turns on the webcam) is on, and then change to a different color when a button "off" (turns off the video) is pushed?
You can use the "Enable" property of the lamp to turn it on and off. You can also use the "Color" property to change its color.
Hey, I keep tyring to put a rectangle on the camera plot I have. I added a callback on the plot and put rectangle(app.UIAxes, 'Position', [25,25,50,50])
but nothing shows up when I run the code. Do you know how to fix this?

Sign in to comment.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!