Clear Filters
Clear Filters

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

31 views (last 30 days)
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)

Ayush Anand
Ayush Anand on 23 May 2024 at 11:48
Edited: Ayush Anand on 23 May 2024 at 11:55
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
Ayush Anand
Ayush Anand on 24 May 2024 at 4:59
Edited: Ayush Anand on 24 May 2024 at 4:59
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!
Ron
Ron on 25 May 2024 at 9:14
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

Community Treasure Hunt

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

Start Hunting!