Plotting the preview from webcam in the matlab app window in real time.

I am developing an app which requires the real time video being captured from a webcam to be displayed. the preview function does this but it comes as a popup. I dont want a separat popup but i want this figure to appear in my matalb app window inside a panel/ uiaxes. Is it possibel to do so? the below figure shows the popup as "Figure 2" but i want this video to appear in the Panel6. Can someone please help me?

Answers (1)

You can do this using the webcam support in MATLAB (https://in.mathworks.com/help/releases/R2023a/supportpkg/usbwebcams/ug/webcam.html). You can follow the steps below:
1. Install the support for USB Web cam in MATLAB using matlab.internal.addons.showAddon('USBWEBCAM'); command in the command window.
2.Add a "UIAxes" component to your app where the video will be displayed.
3.Since you need to continuously capture frames from the webcam and display them on the UIAxes, you can use a timer object that periodically fetches a frame from the webcam and displays it. Create properties for the webcam and timer in the "Properties" section of the code.
properties (Access = private)
cam % Webcam object
timer % Timer object
end
4.Initialize the webcam and timer in the `"startupFcn"
function startupFcn(app)
app.cam = webcam; % Initializes the webcam
app.timer = timer('ExecutionMode', 'fixedRate', 'Period', 0.1, 'TimerFcn', @(~,~)updateVideo(app));
start(app.timer);
end
5. Implement the "updateVideo" function to capture and display frames.
function updateVideo(app)
frame = snapshot(app.cam);
image(app.UIAxes, frame);
axis(app.UIAxes, 'image');
axis(app.UIAxes, 'off');
end
6. Cleanup in the "CloseRequestFcn" to stop the timer and release the webcam.
function CloseRequestFcn(app)
stop(app.timer);
delete(app.timer);
clear('app.cam');
delete(app);
end

3 Comments

Thank you so much for responding to my query. I am not an expert in using matlab so please help me a bit. this is how my code looks like. Now what should be the next step? Where and how to use these functions?
This looks good! Now you just need to add a UI Axes component to your app from the design view:
And you can just run the app now. You should be able to see the webcam preview in the UI panel!
Thankyou for your quick response. I did the same but the preview is not there. This is what I am getting.

Sign in to comment.

Products

Release

R2023a

Asked:

Ron
on 23 May 2024

Commented:

Ron
on 25 May 2024

Community Treasure Hunt

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

Start Hunting!