I need help with my GUI
古いコメントを表示
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 件のコメント
Ben Cunningham
2019 年 4 月 24 日
Could you briefly elaborate on what the specific problems you are trying to solve are?
ie what is your code doing now vs what you want it to do.
saeeda saher
2019 年 4 月 25 日
Walter Roberson
2019 年 4 月 25 日
What error message are you seeing? Or describe the difference between what you are getting and what you want to get.
採用された回答
その他の回答 (1 件)
Geoff Hayes
2019 年 4 月 24 日
編集済み: Geoff Hayes
2019 年 4 月 24 日
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 件のコメント
saeeda saher
2019 年 4 月 25 日
saeeda saher
2019 年 4 月 25 日
Geoff Hayes
2019 年 4 月 25 日
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...
カテゴリ
ヘルプ センター および File Exchange で Creating, Deleting, and Querying Graphics Objects についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!