Matlab Gui - Adquire Video

3 views (last 30 days)
Sérgio Pereira
Sérgio Pereira on 20 Jan 2020
Answered: Geoff Hayes on 20 Jan 2020
Hello guys,
I have one app matlab gui to adquire one vídeo IP from my telephone.
When i press connect, I start transmitting the image in an 'axe' and it works fine.
But i need one function for when i press 'DISCONNECT' button, he Stop transmitting the image and clean the graphic, i try do: cla, delete, but nothing do.
Can you help me?
duvida.JPG
My code:
% --- Executes on button press in connect.
function connect_Callback(hObject, eventdata, handles)
global vid;
% hObject handle to connect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
vid = ipcam('http://100.75.51.93:8081/', 'admin', '12345');
while true
[Imag_filled1, Imag_in1] = IPProcessamentoRed(vid);
axes(handles.axes1);
imshow(Imag_in1);
axes(handles.axes2);
imshow(Imag_filled1);
end
% --- Executes on button press in disconnect.
function disconnect_Callback(hObject, eventdata, handles)
global vid;
stop(vid);
flushdata(vid);
cla(handles.axes1);
delete(vid);
clear vid

Accepted Answer

Geoff Hayes
Geoff Hayes on 20 Jan 2020
Sérgio - I suspect the problem might be with your while loop that is preventing the disconnect_Callback from being processed. You could try adding a brief pause to the body of the while loop so that the other function can be evaluated (but I think that you will see one or more errors when doing this since your while loop condition is always set to true and once vid becomes invalid, any call to it will result in an error.
An alternative approach might be to use a timer which you start and stop from your "connect" and "disconnect" callbacks respectively. The timer would fire periodically (at a rate to be determined by you) and its callback would acquire the video (or image) from your phone and display it in the axes.
Your code might look something like
function connect_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)
handles.timer = timer('Name','MyTimer', ...
'Period',1, ...
'StartDelay',1, ...
'TasksToExecute',inf, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn',{@timerCallback,handles.figure1});
handles.vid = ipcam('http://100.75.51.93:8081/', 'admin', '12345');
guidata(hObject,handles);
start(handles.timer);
% --- Executes on button press in disconnect.
function disconnect_Callback(hObject, eventdata, handles)
if isfield(handles, 'vid') && isfield(handles, 'timer')
stop(handles.timer);
stop(handles.vid);
flushdata(handles.vid);
cla(handles.axes1);
delete(handles.vid);
handles.vid = [];
guidata(hObject, handles);
end
function [] = timerCallback(~,~,guiHandle)
if ~isempty(guiHandle)
[Imag_filled1, Imag_in1] = IPProcessamentoRed(handles.vid);
axes(handles.axes1);
imshow(Imag_in1);
axes(handles.axes2);
imshow(Imag_filled1);
end
Note how the global variable has been removed and is stored as a field to the handles structure (along with the timer object). The abovie is untested but it might give you an idea of what you can do.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!