Webcam preview in GUI not showing up

15 Ansichten (letzte 30 Tage)
Jesse McAlister
Jesse McAlister am 12 Jun. 2019
Kommentiert: May Mon Phyoo am 31 Aug. 2020
Using MatlabR2019a. Trying to get a webcam preview to show up in a GUI, but just getting a black box in the resulting axes - see image below
gui.JPG
Here is the code:
function align_GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to align_GUI (see VARARGIN)
% Choose default command line output for align_GUI
handles.output = hObject;
cam=webcam(2)
imWidth=640;
imHeight=480;
axes(handles.axes1);
hImage=image(zeros(imHeight,imWidth,3),'Parent',handles.axes1);
preview(cam,hImage)
Webcam preview when just using the command window works fine using:
cam=webcam(2);
preview(cam)
  3 Kommentare
Jesse McAlister
Jesse McAlister am 13 Jun. 2019
That worked! Thanks
May Mon Phyoo
May Mon Phyoo am 31 Aug. 2020
Thank u so much.
i'm also face with that situation. It's helped.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 13 Jun. 2019
The issue seems to be because the cam object is a local variable within the OpeningFcn function. When the function completes, the cam object is destroyed. The fix to add it to the handles structure so that it is available for the lifetime of the GUI.
function align_GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to align_GUI (see VARARGIN)
% Choose default command line output for align_GUI
handles.output = hObject;
handles.cam=webcam(2)
imWidth=640;
imHeight=480;
axes(handles.axes1);
handles.hImage=image(zeros(imHeight,imWidth,3),'Parent',handles.axes1);
preview(handles.cam, handles.hImage)
guidata(hObject, handles)

Weitere Antworten (0)

Tags

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by