MATLAB: How to start image acquisition in GUI

guiguideimage acquisition

Hi everyone,
I have written some code to save 1 frame every 10 seconds. In script-form, it works beautifully:
% ImgAcqu_SCRIPT
info = imaqhwinfo; % Get info

feed = info.InstalledAdaptors; % Find available camera feeds

vid = videoinput(feed{1}); % Use first camera input, or whatever # camera

vid.ReturnedColorSpace = 'grayscale';
vid.FramesPerTrigger = 1; % Specify number of frames to acquire per trigger

vid.TriggerRepeat = Inf; % Specify number of additional times to execute trigger

triggerconfig(vid, 'manual'); % Set trigger to manual

vid.TimerPeriod = 10; % Period of time between timer events

vid.TimerFcn = 'trigger(vid)'; % Specify function to execute for each period of time

vid.FramesAcquiredFcnCount = 1; % Number of frames that must be acquired before frames acquired event is generated

vid.FramesAcquiredFcn = @saveFrame;
start(vid);
function saveFrame(vid, event)
image = getdata(vid, 1);
imwrite(image, [datestr(now,'yyyymmddHHMMSS') '.jpg']);
end
% Command to stop image acquisition: stop(vid)
However, when I stick it in a GUI, it throws an error at start(vid):
function varargout = supersimple(varargin)
% SUPERSIMPLE MATLAB code for supersimple.fig
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @supersimple_OpeningFcn, ...
'gui_OutputFcn', @supersimple_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before supersimple is made visible.
function supersimple_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = supersimple_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
info = imaqhwinfo; % Get info
feed = info.InstalledAdaptors; % Find available camera feeds
handles.vid = videoinput(feed{1}); % Use first camera input, or whatever # camera
guidata(hObject,handles);
vid = handles.vid;
vid.ReturnedColorSpace = 'grayscale';
vid.FramesPerTrigger = 1; % Specify number of frames to acquire per trigger
vid.TriggerRepeat = Inf; % Specify number of additional times to execute trigger
triggerconfig(vid, 'manual'); % Set trigger to manual
vid.TimerPeriod = 10; % Period of time between timer events
vid.TimerFcn = 'trigger(vid)'; % Specify function to execute for each period of time
vid.FramesAcquiredFcnCount = 1; % Number of frames that must be acquired before frames acquired event is generated
vid.FramesAcquiredFcn = @saveFrame;
start(vid);
function saveFrame(vid, event)
image = getdata(handles.vid, 1);
imwrite(image, [datestr(now,'yyyymmddHHMMSS') '.jpg']);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
stop(handles.vid)
The error:
>> supersimple
Error using imaqdevice/trigger (line 48)
OBJ must be running before TRIGGER is used.
Warning: The TimerFcn callback is being disabled.
To enable the callback, set the TimerFcn property.
>>
I think the hang-up is with start(vid), because when I change that to preview(vid), the preview works.
What am I missing in translating my script into a GUI? I appreciate any help. Thank you.

Best Answer

Not sure what needs to be on the handles structure and what doesn't. This saves images every 10 seconds for me.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
info = imaqhwinfo; % Get info
feed = info.InstalledAdaptors; % Find available camera feeds
vid = videoinput(feed{1}); % Use first camera input, or whatever # camera
vid.TimerFcn = @trigger; % Specify function to execute for each period of time
vid.TimerPeriod = 10; % Period of time between timer events
triggerconfig(vid, 'manual'); % Set trigger to manual
vid.FramesPerTrigger = 1; % Specify number of frames to acquire per trigger
vid.TriggerRepeat = Inf; % Specify number of additional times to execute trigger
vid.ReturnedColorSpace = 'grayscale';
vid.FramesAcquiredFcnCount = 1; % Number of frames that must be acquired before frames acquired event is generated
vid.FramesAcquiredFcn = @saveFrame;
start(vid)
handles.vid = vid;
guidata(hObject, handles);
end
I was playing with the order, though I don't think it's that important. I did change how vid.TimerFcn called the trigger. I also changed the saveFrame function a bit.
function saveFrame(src, event)
image = getdata(src, 1);
imwrite(image, [datestr(now,'yyyymmddHHMMSS') '.jpg']);
end