How do I get live video from multiple cameras at the same time in a GUI?

4 views (last 30 days)
I am trying to see the video feed from two camera's at the same time in a Matlab GUI. The code below works for one camera at a time but not for both at the same time. I believe the issue is with my axes handle call, but I am not sure how to fix the issue.
function varargout = myCameraGUI(varargin)
% MYCAMERAGUI MATLAB code for mycameragui.fig
% MYCAMERAGUI, by itself, creates a new MYCAMERAGUI or raises the existing
% singleton*.
%
% H = MYCAMERAGUI returns the handle to a new MYCAMERAGUI or the handle to
% the existing singleton*.
%
% MYCAMERAGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MYCAMERAGUI.M with the given input arguments.
%
% MYCAMERAGUI('Property','Value',...) creates a new MYCAMERAGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before myCameraGUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to myCameraGUI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIhandles
% Edit the above text to modify the response to help mycameragui
% Last Modified by GUIDE v2.5 19-Jun-2012 08:08:03
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @myCameraGUI_OpeningFcn, ...
'gui_OutputFcn', @myCameraGUI_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 mycameragui is made visible.
function myCameraGUI_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 mycameragui (see VARARGIN)
% Choose default command line output for mycameragui
handles.output = hObject;
% Create video object
% Putting the object into manual trigger mode and then
% starting the object will make GETSNAPSHOT return faster
% since the connection to the camera will already have
% been established.
handles.video1 = videoinput('gige', 1, 'Mono12');
handles.video2 = videoinput('gige', 2, 'Mono12');
% Update handles structure
guidata(hObject, handles);
set(handles.video1,'TimerPeriod', 0.05, ...
'TimerFcn',['if(~isempty(gco)),'...
'handles=guidata(gcf);'... % Update handles
'imagesc(getsnapshot(handles.video1));'... % Get picture using GETSNAPSHOT and put it into axes using IMAGE
'set(handles.camera1,''ytick'',[],''xtick'',[]),'... % Remove tickmarks and labels that are inserted when using IMAGE
'else '...
'delete(imaqfind);'... % Clean up - delete any image acquisition objects
'end']);
triggerconfig(handles.video1,'manual');
handles.video1.FramesPerTrigger = Inf; % Capture frames until we manually stop it
% Update handles structure
guidata(hObject, handles);
set(handles.video2,'TimerPeriod', 0.05, ...
'TimerFcn',['if(~isempty(gco)),'...
'handles=guidata(gcf);'... % Update handles
'imagesc(getsnapshot(handles.video2));'... % Get picture using GETSNAPSHOT and put it into axes using IMAGE
'set(handles.camera2,''ytick'',[],''xtick'',[]),'... % Remove tickmarks and labels that are inserted when using IMAGE
'else '...
'delete(imaqfind);'... % Clean up - delete any image acquisition objects
'end']);
triggerconfig(handles.video2,'manual');
handles.video2.FramesPerTrigger = Inf; % Capture frames until we manually stop it
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes mycameragui wait for user response (see UIRESUME)
uiwait(handles.myCameraGUI);
% --- Outputs from this function are returned to the command line.
function varargout = myCameraGUI_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
handles.output = hObject;
varargout{1} = handles.output;
% --- Executes on button press in startStopCamera1.
function startStopCamera1_Callback(hObject, eventdata, handles)
% hObject handle to startStopCamera1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Start/Stop Camera
if strcmp(get(handles.startStopCamera1,'String'),'Start Camera')
% Camera is off. Change button string and start camera.
set(handles.startStopCamera1,'String','Stop Camera')
start(handles.video1)
axes(handles.camera1)
% hold(handles.camera1)
set(handles.startAcquisition1,'Enable','on');
set(handles.captureImage1,'Enable','on');
else
% Camera is on. Stop camera and change button string.
set(handles.startStopCamera1,'String','Start Camera')
stop(handles.video1)
set(handles.startAcquisition1,'Enable','off');
set(handles.captureImage1,'Enable','off');
guidata(hObject, handles);
end
% --- Executes on button press in captureImage1.
function captureImage1_Callback(hObject, eventdata, handles)
% hObject handle to captureImage1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% frame = getsnapshot(handles.video1);
frame = get(get(handles.camera1,'children'),'cdata'); % The current displayed frame
save('testframe.mat', 'frame');
disp('Frame saved to file ''testframe.mat''');
% --- Executes on button press in startAcquisition1.
function startAcquisition1_Callback(hObject, eventdata, handles)
% hObject handle to startAcquisition1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Start/Stop acquisition
if strcmp(get(handles.startAcquisition1,'String'),'Start Acquisition')
% Camera is not acquiring. Change button string and start acquisition.
set(handles.startAcquisition1,'String','Stop Acquisition');
trigger(handles.video1);
else
% Camera is acquiring. Stop acquisition, save video data,
% and change button string.
stop(handles.video1);
disp('Saving captured video...');
videodata = getdata(handles.video1);
save('testvideo.mat', 'videodata');
disp('Video saved to file ''testvideo.mat''');
start(handles.video1); % Restart the camera
set(handles.startAcquisition1,'String','Start Acquisition');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on button press in startStopCamera2.
function startStopCamera2_Callback(hObject, eventdata, handles)
% hObject handle to startStopCamera1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Start/Stop Camera
if strcmp(get(handles.startStopCamera2,'String'),'Start Camera')
% Camera is off. Change button string and start camera.
set(handles.startStopCamera2,'String','Stop Camera')
axes(handles.camera2)
% hold(handles.camera2)
start(handles.video2)
set(handles.startAcquisition2,'Enable','on');
set(handles.captureImage2,'Enable','on');
else
% Camera is on. Stop camera and change button string.
set(handles.startStopCamera2,'String','Start Camera')
stop(handles.video2)
set(handles.startAcquisition2,'Enable','off');
set(handles.captureImage2,'Enable','off');
guidata(hObject, handles);
end
% --- Executes on button press in captureImage2.
function captureImage2_Callback(hObject, eventdata, handles)
% hObject handle to captureImage1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% frame = getsnapshot(handles.video1);
frame = get(get(handles.camera2,'children'),'cdata'); % The current displayed frame
save('testframe.mat', 'frame');
disp('Frame saved to file ''testframe.mat''');
% --- Executes on button press in startAcquisition2.
function startAcquisition2_Callback(hObject, eventdata, handles)
% hObject handle to startAcquisition1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Start/Stop acquisition
if strcmp(get(handles.startAcquisition2,'String'),'Start Acquisition')
% Camera is not acquiring. Change button string and start acquisition.
set(handles.startAcquisition2,'String','Stop Acquisition');
trigger(handles.video2);
else
% Camera is acquiring. Stop acquisition, save video data,
% and change button string.
stop(handles.video2);
disp('Saving captured video...');
videodata = getdata(handles.video2);
save('testvideo.mat', 'videodata');
disp('Video saved to file ''testvideo.mat''');
start(handles.video2); % Restart the camera
set(handles.startAcquisition2,'String','Start Acquisition');
end
% --- Executes when user attempts to close myCameraGUI.
function myCameraGUI_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to myCameraGUI (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: delete(hObject) closes the figure
delete(hObject);
delete(imaqfind);
I get the following error when I try to activate both camera's:
gige: Block/frame 200 is being dropped because a lost packet is unable to be resent. There are several possible causes for packets 
being lost. See the troubleshooting information in the "Configuring GigE Vision Devices" section of the Image Acquisition Toolbox 
documentation.
Error in uiwait (line 82)
waitfor (hFigDlg, 'WaitStatus', 'inactive');
Error in myCameraGUI>myCameraGUI_OpeningFcn (line 98)
uiwait(handles.myCameraGUI);
Error in gui_mainfcn (line 221)
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in myCameraGUI (line 42)
gui_mainfcn(gui_State, varargin{:});
Thanks to anyone who can help me!
Alex
  3 Comments
Alex
Alex on 20 Jun 2012
Looking at the error message below it doesn't seem like the error message I posted above is the real problem it is in fact the way I am using the handles. I am not sure how to fix them though.
gige: Block/frame 200 is being dropped because a lost packet is unable to be resent. There are several possible causes for packets
being lost. See the troubleshooting information in the "Configuring GigE Vision Devices" section of the Image Acquisition Toolbox
documentation.
Walter Roberson
Walter Roberson on 20 Jun 2012
You probably do not need some of those axes() calls.
I did not see anything obvious when I skimmed your code, but I did not study it.
You may wish to read http://www.mathworks.com/matlabcentral/answers/22208-show-figure

Sign in to comment.

Answers (2)

Alex
Alex on 20 Jun 2012
I was able to fix this by setting the axes in:
function myCameraGUI_OpeningFcn(hObject, eventdata, handles, varargin)
I changed my code to:
% --- Executes just before mycameragui is made visible.
function myCameraGUI_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 mycameragui (see VARARGIN)
% Choose default command line output for mycameragui
handles.output = hObject;
% Create video object
% Putting the object into manual trigger mode and then
% starting the object will make GETSNAPSHOT return faster
% since the connection to the camera will already have
% been established.
handles.video1 = videoinput('gige', 1, 'Mono12');
handles.video2 = videoinput('gige', 2, 'Mono12');
% Update handles structure
guidata(hObject, handles);
set(handles.video1,'TimerPeriod', 0.05, ...
'TimerFcn',['if(~isempty(gco)),'...
'handles=guidata(gcf);'... % Update handles
'axes(handles.camera1);'...
'image(getsnapshot(handles.video1));'... % Get picture using GETSNAPSHOT and put it into axes using IMAGE
'set(handles.camera1,''ytick'',[],''xtick'',[]),'... % Remove tickmarks and labels that are inserted when using IMAGE
'else '...
'delete(imaqfind);'... % Clean up - delete any image acquisition objects
'end']);
triggerconfig(handles.video1,'manual');
handles.video1.FramesPerTrigger = Inf; % Capture frames until we manually stop it
% Update handles structure
guidata(hObject, handles);
set(handles.video2,'TimerPeriod', 0.05, ...
'TimerFcn',['if(~isempty(gco)),'...
'handles=guidata(gcf);'... % Update handles
* 'axes(handles.camera2);'...*
'image(getsnapshot(handles.video2));'... % Get picture using GETSNAPSHOT and put it into axes using IMAGE
'set(handles.camera2,''ytick'',[],''xtick'',[]),'... % Remove tickmarks and labels that are inserted when using IMAGE
'else '...
'delete(imaqfind);'... % Clean up - delete any image acquisition objects
'end']);
triggerconfig(handles.video2,'manual');
handles.video2.FramesPerTrigger = Inf; % Capture frames until we manually stop it
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes mycameragui wait for user response (see UIRESUME)
uiwait(handles.myCameraGUI);
However when I activate both cameras I get the following error a few seconds after viewing the live video:
gige: Block/frame 146 is being dropped because a lost packet is unable to be resent. There are several possible causes for packets
being lost. See the troubleshooting information in the "Configuring GigE Vision Devices" section of the Image Acquisition Toolbox
documentation.
Error in uiwait (line 82)
waitfor (hFigDlg, 'WaitStatus', 'inactive');
Error in myCameraGUI>myCameraGUI_OpeningFcn (line 102)
uiwait(handles.myCameraGUI);
Error in gui_mainfcn (line 221)
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in myCameraGUI (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in guidefunc>layoutActivate (line 1158)
feval(fcn);
Error in guidefunc (line 13)
varargout = layoutActivate(varargin{:});

Fiddin Yusfida
Fiddin Yusfida on 4 Sep 2018
Edited: Fiddin Yusfida on 4 Sep 2018
Hi Alex have you solved this problem?
I want to control 2 cameras, same with yours. But, my problem is both cameras only displayed in a axes ( actually i made two axes).
Anyone can help me?

Community Treasure Hunt

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

Start Hunting!