0
votes

Code below is supposed to display the webcam preview in figure called axes1, when button preview is clicked. It must display the webcam images in real time.

% --- Executes on button press in Preview.
function Preview_Callback(hObject, eventdata, handles)
% hObject    handle to Preview (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    img = snapshot(cam);

    imshow(img,'Parent',handles.axes1);

    disp(clock);
    pause(0.5);

Error appearing is: Error while evaluating UIControl Callback

Undefined function or variable 'cam'.

Error in Main>Preview_Callback (line 161) img = snapshot(cam);

Error in gui_mainfcn (line 95) feval(varargin{:});

Error in Main (line 43) gui_mainfcn(gui_State, varargin{:});

Error in @(hObject,eventdata)Main('Preview_Callback',hObject,eventdata,guidata(hObject))

Error while evaluating UIControl Callback

If I place cam=webcam; before img = snapshot(cam); the error states Error using Main>Preview_Callback (line 161) An active connection to webcam, Integrated Webcam, already exists. You cannot create another connection to the same webcam.

Error in gui_mainfcn (line 95) feval(varargin{:});

Error in Main (line 43) gui_mainfcn(gui_State, varargin{:});

Error in @(hObject,eventdata)Main('Preview_Callback',hObject,eventdata,guidata(hObject))

Error while evaluating UIControl Callback

It is clear that the initialization is at the incorrect place. Where should one intialise/ state connection to the webcam?

1

1 Answers

0
votes
Intialise counter as follows:
handles.PressOne=0;


if handles.PressOne==0
     Connect to the camera (cam=webcam). 
     Update counter for handles.PressOne.
else
     Capture the image (img=snapshot(cam)) and disp the data. 
end