Video feed will not start() in my axes object, but works with preview().

조회 수: 1 (최근 30일)
Lisa Li
Lisa Li 2015년 10월 8일
답변: Rohit Kudva 2015년 10월 20일
Hello,
I am trying to build a GUI that will allow a user to switch between a selection of cameras in a dropdown menu. The camera selected will show up in a display window on the GUI called cameraAxes. (The dropdown menu build is just fine.) Here is my code:
function varargout = NWEMultiLEDGUI(varargin)
% NWEMULTILEDGUI MATLAB code for NWEMultiLEDGUI.fig
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @NWEMultiLEDGUI_OpeningFcn, ...
'gui_OutputFcn', @NWEMultiLEDGUI_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 NWEMultiLEDGUI is made visible.
function NWEMultiLEDGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for NWEMultiLEDGUI
handles.output = hObject;
%Initialize camera selection dropdown menu display:
imaqreset;
result = imaqhwinfo;
adapterName = result.InstalledAdaptors{1}; %this is hardcoded; change later
axes(handles.cameraAxes); %makes current axes object the cameraAxes
axis off;
hold('off');
%currently 'winvideo' since it's the only adaptor.
%Will have to write more code if more adaptor options are needed
camNum = 1;
assignin('base','adapterName', adapterName);
assignin('base','camNum', camNum);
info = imaqhwinfo(adapterName);
for count = 1: length(info.DeviceIDs)
info = imaqhwinfo(adapterName, count);
name = info.DeviceName;
posList{count} = sprintf('%d: %s', count, name);
end
set(handles.selectCameraMenu,'String',posList);
disp('Camera selection menu built...');
%Camera display settings:
%handles.video = videoinput(adapterName, camNum); %create video input
imshow(imread(strcat(pwd,'\testImg.jpg'))); %currently displaying a test image.
assignin('base','newCam', 'true'); % a flag variable to let the start/stop code know when to redefine the video source
%assignin('base','newCam', 'false');
%Turn on start/stop camera once initialization is ready
set(handles.startStopCameraButton,'Enable','on');
set(handles.statusBar,'String','Select a camera');
disp('End initialization sequence.');
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = NWEMultiLEDGUI_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on selection change in selectCameraMenu.
function selectCameraMenu_Callback(hObject, eventdata, handles)
assignin('base','camNum', get(handles.selectCameraMenu,'Value'));
assignin('base','newCam','true'); %a flag variable to let start/stop know if you need to load a new camera.
% --- Executes during object creation, after setting all properties.
function selectCameraMenu_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in startStopCameraButton.
function startStopCameraButton_Callback(hObject, eventdata, handles)
% Start/Stop Camera
set(handles.acqImgButton,'Enable','on');
axes(handles.cameraAxes);
vid = evalin('base','vidFeed');
%get(handles.video);
%runs if camera is currently stopped/has never been started
if strcmp(get(handles.startStopCameraButton,'String'),'Start Camera')
% Camera is off. Change button string and start camera.
%read in vars from base workspace
if(strcmp(evalin('base','newCam'),'true'))
disp('Establishing camera parameters...');
set(handles.statusBar,'String','Establishing camera parameters...');
handles.video = videoinput(evalin('base','adapterName'),
evalin('base','camNum'));
axes(handles.cameraAxes);
hold(handles.cameraAxes, 'off')
set(handles.video,'TimerPeriod', 0.05, ...
'TimerFcn',['if(~isempty(gco)),'...
'handles=guidata(gcf);'... % Update handles
'image(getsnapshot(handles.video));'... % Get picture using GETSNAPSHOT and put it into axes using IMAGE
'set(handles.cameraAxes,''ytick'',[],''xtick'',[]),'... % Remove tickmarks and labels that are inserted when using IMAGE
'else '...
'end']);
triggerconfig(handles.video,'manual');
handles.video.FramesPerTrigger = Inf; % Capture frames until we manually stop it
set(handles.statusBar,'String','Loading camera settings...');
assignin('base','newCam','false');
assignin('base','vidFeed','vid');
end
hold('off');
disp('Starting camera...');
axes(handles.cameraAxes);
% preview(handles.video); %This line works when uncommented, so I think the handle is still alive here.
start(handles.video);
set(handles.statusBar,'String','Camera is live.');
set(handles.contImgButton,'Enable','on');
set(handles.startStopCameraButton,'String','Stop Camera');
imaqfind %SEE BELOW FOR OUTPUT FROM THIS LINE
else
% Camera is on. Stop camera and change button string.
disp('Stopping camera...');
stop(handles.video);
% preview(handles.video); %This line works when uncommented.
set(handles.statusBar,'String','Camera stopped.');
set(handles.contImgButton,'Enable','off');
set(handles.startStopCameraButton,'String','Start Camera');
end
guidata(hObject, handles); % Update handles structure
The output when I run imaqfind inside startStopCameraButton is:
Video Input Object Array:
Index: Type: Name:
1 videoinput YUY2_1280x960-winvideo-1
2 videoinput YUY2_1280x960-winvideo-1
There is no error output when I try to start or stop the camera.
The commented out "preview(handles.video)" line works in both the 'Start section' and 'Stop section' of the startStopCameraButton code.
The test image I load upon opening the GUI shows up throughout the testing sequence and appears unchanged. If I comment out the test image line, the region where the cameraAxes object exists on my GUI remains blank.
Hopefully someone has some insight. Thank you! -Lisa

답변 (1개)

Rohit Kudva
Rohit Kudva 2015년 10월 20일
Hi Lisa,
I understand that you are unable to view any image on your GUI. I looked into your code and one of the possible reason for this issue can be that the condition for the 'if' statement of the 'TimerFcn' callback function is never true. In order to figure this out, write a separate function in MATLAB and set the 'TimerFcn' property of the videoinput object to the handle of that function
function mytimercallback(src,events)
% input parameter 'src' will be the handle to the 'videoinput' object
% The code goes here
end
And in the main function
set(handles.video,'TimerPeriod', 0.05, 'TimerFcn', @mytimercallback);
You can now add breakpoints to the 'mytimercallback' function to see if the 'if' statement condition is ever true.
Another possible reason can be that the reference to 'handles' in the callback function may be empty.
handles=guidata(gcf); % handles may be []
This again can be checked by debugging into the callback function.
Regards,
Rohit

Community Treasure Hunt

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

Start Hunting!

Translated by