I need help with my GUI

7 Ansichten (letzte 30 Tage)
saeeda saher
saeeda saher am 24 Apr. 2019
Kommentiert: Geoff Hayes am 25 Apr. 2019
I need some help with my following code:
(1) I my following code when push the button the start button the camera starts , while running I want to capture image from the cam after each 5 seconds and want to save that fram in a folder.
(2) when push stop button the cam should stop capturing frames too.
function varargout = MyFERApp(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @MyFERApp_OpeningFcn, ...
'gui_OutputFcn', @MyFERApp_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 MyFERApp is made visible.
function MyFERApp_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 MyFERApp (see VARARGIN)
% Choose default command line output for MyFERApp
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes MyFERApp wait for user response (see UIRESUME)
% uiwait(handles.figure1);
handles.output = hObject;
imaqreset
handles.cam = webcam(1);
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = MyFERApp_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in Start_Camera_button.
function Start_Camera_button_Callback(hObject, eventdata, handles)
% hObject handle to Start_Camera_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global KeepRunning;
KeepRunning=1;
while KeepRunning
cam = handles.cam;
data = snapshot(cam);
imshow(data, 'Parent', handles.axes1);
hold on
end
guidata(hObject, handles);
% --- Executes on button press in Stop_Camera_Button.
function Stop_Camera_Button_Callback(hObject, eventdata, handles)
% hObject handle to Stop_Camera_Button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global KeepRunning;
KeepRunning=0;
guidata(hObject, handles);
% --- Executes on button press in Delete_Camera_Button.
function Delete_Camera_Button_Callback(hObject, eventdata, handles)
% hObject handle to Delete_Camera_Button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
cam = handles.cam;
delete (cam);
clear cam;
guidata(hObject, handles);
  3 Kommentare
saeeda saher
saeeda saher am 25 Apr. 2019
I am trynig to run cam and on live streem want to take snapshots with 5 seconds of pause , and want to save those images in to the folder in image formate. I want 2 buttons here to work, one for starting the camera and the other for stoping the camera; between start and stop the camera should take snapshots and save them unless stop button is pressed
Walter Roberson
Walter Roberson am 25 Apr. 2019
What error message are you seeing? Or describe the difference between what you are getting and what you want to get.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ben Cunningham
Ben Cunningham am 25 Apr. 2019
See the 'interruptible' property of callbacks in app designer - e.g. here under the heading 'Callback execution control'.
You need to create an opportunity for the Stop_Camera_Button_Callback to interrupt the while loop in Start_Camera_button_Callback. The sentence of interest is:
"The interruption occurs at the next point where MATLAB processes the queue, such as when there is a drawnow, figure, uifigure, getframe, waitfor, or pause command."
In other words a drawnow command can provide an oppurtunity for your stop button to interrupt the execution of the while loop to set the global variable, then when the while loop resumes KeepRunning will evaluate false (0).
Without drawnow, the stop button will wait until the previous callback is finished - which never occurs since your while loop will spin forever.
In your case you want the while loop to execute with a 5 second pause so the pause command might be more applicable.
ie
while KeepRunning
cam = handles.cam;
data = snapshot(cam);
imshow(data, 'Parent', handles.axes1);
hold on
pause(5) % Pause for 5 seconds and allow opportunity for Stop Button Callback to interrupt
end
Depending what you are after then the timer based implementation suggested by Geoff Hayes may be preferable.

Weitere Antworten (1)

Geoff Hayes
Geoff Hayes am 24 Apr. 2019
Bearbeitet: Geoff Hayes am 24 Apr. 2019
saeeda - rather than using a loop to capture your images, consider using a timer that executes every five. (By the way, a problem with the while loop is that it might not be interruptible and so even if you press the stop button, the loop will continue. You can get around this by calling an appropriate function, see Interrupt Callback Execution for details.)
In your start function, you could do something like
function Start_Camera_button_Callback(hObject, eventdata, handles)
handles.timer = timer('Name','MyTimer', ...
'Period',5, ...
'StartDelay',0, ...
'TasksToExecute',inf, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn',{@timerCallback,handles.figure1});
guidata(hObject,handles);
start(handles.timer);
The above will start your timer and its callback will fire every five seconds. We save the timer object to the handles structure so that we can stop it later.
Your timer callback will look something like
function [] = timerCallback(~,~,guiHandle)
if ~isempty(guiHandle)
% get the handles
handles = guihandles(guiHandle);
data = snapshot(handles.cam);
imshow(data, 'Parent', handles.axes1);
% save your image to file with imwrite
end
You will need to define how you want to save the file (i.e. format, name, location).
Finally, your stop pushubtton callback will be
function Stop_Camera_Button_Callback(hObject, eventdata, handles)
if isfield(handles, 'timer')
stop(handles.timer);
handles = rmfield(handles, 'timer');
guidata(hObject, handles);
end
The above is untested but should give you an idea of what you can do.
  3 Kommentare
saeeda saher
saeeda saher am 25 Apr. 2019
I am getting this error
Error while evaluating TimerFcn for timer 'MyTimer'
Reference to non-existent field 'cam'.
Geoff Hayes
Geoff Hayes am 25 Apr. 2019
The error message is telling you that the handles structure (or something else) doesn't have the 'cam' field. When is the cam object created? Have you assigned it to the handles object and called guidata(hObject, handles)? It looks like you did this in the opening function of your GUI but perhaps you have changed something since posting that initial code...

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Specifying Target for Graphics Output finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by