Plotting the preview from webcam in the matlab app window in real time.
28 views (last 30 days)
Show older comments
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?
0 Comments
Answers (1)
Ayush Anand
on 23 May 2024
Edited: Ayush Anand
on 23 May 2024
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
on 24 May 2024
Edited: Ayush Anand
on 24 May 2024
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!
See Also
Categories
Find more on Image Acquisition Support Packages for Hardware Adaptors (Generic Video Interface) 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!