Azzera filtri
Azzera filtri

How to create a custom preview window for MATLAB webcam?

12 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 21 Set 2017
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
webcamlist
%%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'));
axis(ax, 'image');
%%Start preview
preview(c, im)
setappdata(fig, 'cam', c);
fig.CloseRequestFcn = @closePreviewWindow_Callback;
%%Local functions
function closePreviewWindow_Callback(obj, ~)
c = getappdata(obj, 'cam');
closePreview(c)
delete(obj)
end
 
  1 Commento
Andrei
Andrei il 26 Lug 2018
Modificato: Andrei il 26 Lug 2018
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.

Accedi per commentare.

Più risposte (1)

MathWorks MATLAB Hardware Team

Tag

Non è stata ancora inserito alcun tag.

Prodotti


Release

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by