How to create a custom preview window for MATLAB webcam?
조회 수: 32(최근 30일)
표시 이전 댓글
답변: MathWorks MATLAB Hardware Team 2021년 5월 4일
I am using the webcam function from "MATLAB Support Package for USB Webcams" to acquire images from my webcam.
The preview function creates a standalone preview window, but how can I create a preview image in a custom window or app?
To create a custom preview window for webcam you can use the following example as a starting point. This code creates a figure, axes, and image elements programmaticaly, but the same configuration would apply for an app created with GUIDE.
%%Example WEBCAM custom preview window for MATLAB R2017a
%%List connected webcams
%%Connect to webcam
c = webcam(1);
%%Setup preview window
fig = figure('NumberTitle', 'off', 'MenuBar', 'none');
fig.Name = 'My Camera';
ax = axes(fig);
frame = snapshot(c);
im = image(ax, zeros(size(frame), 'uint8'));
setappdata(fig, 'cam', c);
fig.CloseRequestFcn = @closePreviewWindow_Callback;
function closePreviewWindow_Callback(obj, ~)
c = getappdata(obj, 'cam');
댓글 수: 1
Andrei Ursache 2018년 7월 26일
편집: Andrei Ursache 2018년 7월 26일
With the way webcam preview is currently working (as of MATLAB R2018a), you need to specifically assign the figure's CloseRequestFcn to a callback function which closes the figure with close(hFig) or delete(hFig) as in the example above, where hFig is the preview figure handle. If you are previewing multiple cameras in different figures, you also need to do this for the CloseRequestFcn of each of the multiple preview figures.
MathWorks MATLAB Hardware Team 2021년 5월 4일
You can also try using the Acquire Webcam Image MATLAB Live Task. For more details refer to the following answer https://www.mathworks.com/matlabcentral/answers/821010-how-do-i-view-the-live-video-feed-and-capture-images-from-my-usb-webcam-in-matlab
댓글 수: 0
Find more on Startup and Shutdown 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!