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});endif 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 infofeed = info.InstalledAdaptors; % Find available camera feedshandles.vid = videoinput(feed{1}); % Use first camera input, or whatever # cameraguidata(hObject,handles);vid = handles.vid;vid.ReturnedColorSpace = 'grayscale';vid.FramesPerTrigger = 1; % Specify number of frames to acquire per triggervid.TriggerRepeat = Inf; % Specify number of additional times to execute triggertriggerconfig(vid, 'manual'); % Set trigger to manualvid.TimerPeriod = 10; % Period of time between timer eventsvid.TimerFcn = 'trigger(vid)'; % Specify function to execute for each period of timevid.FramesAcquiredFcnCount = 1; % Number of frames that must be acquired before frames acquired event is generatedvid.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