Code covered by the BSD License  

Highlights from
CameraGUI

image thumbnail

CameraGUI

by

 

access all connected cameras, take snapshots, record streams, access and process frames in realtime

CameraGUI(varargin)
function varargout = CameraGUI(varargin)
% CAMERAGUI M-file for CameraGUI.fig
%      CAMERAGUI, by itself, creates a new CAMERAGUI or raises the existing
%      singleton*.
%
%      H = CAMERAGUI returns the handle to a new CAMERAGUI or the handle to
%      the existing singleton*.
%
%      CAMERAGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CAMERAGUI.M with the given input arguments.
%
%      CAMERAGUI('Property','Value',...) creates a new CAMERAGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before CameraGUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to CameraGUI_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 CameraGUI

% Last Modified by GUIDE v2.5 02-Nov-2011 20:47:45

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @CameraGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @CameraGUI_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 CameraGUI is made visible.
function CameraGUI_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 CameraGUI (see VARARGIN)

imaqreset;
handles.camobj = [];
handles.camsrc = [];

% Choose default command line output for CameraGUI
handles.output = hObject;

% set default capturing paths to []
handles.capturePath = [];
handles.captureFileName = [];
handles.captureFullFilePath = [];
handles.captureSubFolder = [];
handles.capturePathInclSubFolder = [];
handles.CaptureToMat = 0;
handles.curCapturingDir = '*.avi';

handles.AllowCapturing = 1;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes CameraGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = CameraGUI_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 FindCams.
function FindCams_Callback(hObject, eventdata, handles)
findCamsFunction(hObject,eventdata,handles); % see further below for this function
handles=guidata(hObject);
if handles.NrCams > 0
    set(handles.InitCam,'Enable','on');
    set(handles.CamListbox,'String',handles.CamList,'Value',handles.NrCams);
end
set(handles.text3,'String','-');
set(handles.text5,'String','-');
set(handles.textColorSpace,'String','-');
set(handles.textFramerate,'String','-');
set(handles.FormatListbox,'String','no camera chosen', 'Value', 1);
set(handles.ChooseFormat,'Enable','off');


% --- Executes on button press in InitCam.
function InitCam_Callback(hObject, eventdata, handles)
if handles.NrCams > 0
    chosenInd = get(handles.CamListbox,'Value');
    handles.chosenCam = handles.CamList{chosenInd};
    handles.chosenDevID = handles.DeviceIDs(chosenInd);
    handles.chosenAdapt = handles.Adaptors{chosenInd};
    camInfo = imaqhwinfo(handles.chosenAdapt,handles.chosenDevID);
    handles.SupportedFormats = camInfo.SupportedFormats;
    set(handles.FormatListbox,'String',handles.SupportedFormats,'Value',length(handles.SupportedFormats));
    set(handles.ChooseFormat,'Enable','on');
    set(handles.text3,'String',handles.chosenCam);
    guidata(hObject, handles);
end


% --- Executes on button press in ChooseFormat.
function ChooseFormat_Callback(hObject, eventdata, handles)
chosenInd = get(handles.FormatListbox,'Value');
handles.chosenFormat = handles.SupportedFormats{chosenInd};
set(handles.FindCams,'Enable','off');
set(handles.InitCam,'Enable','off');
set(handles.ChooseFormat,'Enable','off');
set(handles.DisconCam,'Enable','on');
set(handles.CamSettings,'Enable','on');
set(handles.CamDetails,'Enable','on');
set(handles.Preview,'Enable','on');
set(handles.Snapshot,'Enable','on');
set(handles.MeasureFramerate,'Enable','on');
set(handles.SaveSettings,'Enable','on');
set(handles.LoadSettings,'Enable','on');
if handles.AllowCapturing == 1
    set(handles.capturePanel,'Visible','on');
else
    set(handles.capturePanel,'Visible','off');
end
set(handles.text5,'String',handles.chosenFormat);
handles.camobj = videoinput(handles.chosenAdapt,handles.chosenDevID,handles.chosenFormat);
handles.camsrc = getselectedsource(handles.camobj);
try
    handles.camsrc.ShutterMode = 'manual';
end
handles.colSpace = handles.camobj.ReturnedColorSpace;
handles.framerate = measureFrameRate(handles.camobj,20);
set(handles.textColorSpace,'String',handles.colSpace);
set(handles.textFramerate,'String',handles.framerate);
preview(handles.camobj);
guidata(hObject, handles);


% --- Executes on button press in DisconCam.
function DisconCam_Callback(hObject, eventdata, handles)
set(handles.FindCams,'Enable','on');
set(handles.InitCam,'Enable','on');
set(handles.ChooseFormat,'Enable','on');
set(handles.DisconCam,'Enable','off');
set(handles.CamSettings,'Enable','off');
set(handles.CamDetails,'Enable','off');
set(handles.Preview,'Enable','off');
set(handles.Snapshot,'Enable','off');
set(handles.MeasureFramerate,'Enable','off');
set(handles.capturePanel,'Visible','off');
set(handles.SaveSettings,'Enable','off');
set(handles.LoadSettings,'Enable','off');
closepreview(handles.camobj);
delete(handles.camobj);
handles.camobj = [];
handles.camsrc = [];
guidata(hObject, handles);



% --- Executes on button press in Preview.
function Preview_Callback(hObject, eventdata, handles)
camobj = handles.camobj;
preview(camobj);


% --- Executes on button press in CamSettings.
function CamSettings_Callback(hObject, eventdata, handles)
camsrc = handles.camsrc;
open camsrc;


% --- Executes on button press in SaveSettings.
function SaveSettings_Callback(hObject, eventdata, handles)
camsrc = handles.camsrc;
curset = set(camsrc); % this calls the settings, it does not actually set anything
if ~isempty(curset)
    cursetprop = fieldnames(curset);
    camsetstruct = struct();
    for ii=1:numel(cursetprop)
        camsetstruct.(cell2mat(cursetprop(ii))) = cell2mat(get(camsrc,cursetprop(ii)));
    end
    [savefilename,savefiledir] = uiputfile('*.mat','Location to save camera settings');
    if savefilename ~= 0
        saveFilePath = fullfile(savefiledir, savefilename);
        save(saveFilePath,'camsetstruct');
    end
else
    msgbox('No properties found to save.');
end

% --- Executes on button press in LoadSettings.
function LoadSettings_Callback(hObject, eventdata, handles)
[settingsfilename,settingsfiledir] = uigetfile('*.mat','Load camera settings file');
if settingsfilename ~= 0
    settingsFilePath = fullfile(settingsfiledir, settingsfilename);
    camsetstruct = open(settingsFilePath);
    camsetstruct = camsetstruct.camsetstruct;
    camsrc = handles.camsrc;
    curset = set(camsrc);
    if ~isempty(curset) && ~isempty(camsetstruct)
        loadsetprop = fieldnames(camsetstruct);
        loadnumprop = numel(loadsetprop);
        % check if all loaded properties exist in current camera
        cursetprop = fieldnames(curset);
        curnumprop = numel(cursetprop);
        for ii = 1:loadnumprop
            curfound = false;
            for jj=1:curnumprop
                if strcmp(loadsetprop(ii),cursetprop(jj))
                    curfound = true;
                    break;
                end
            end
            if ~curfound
                msgbox('The loaded settings can not be found in the current camera settings.');
                return
            end
        end
        
        countfields = 0;
        countrounds = 0;
        doagain = true;
        while doagain
            countrounds = countrounds + 1;
            for ii = 1:loadnumprop
                curfield = cell2mat(loadsetprop(ii));
                countfields = countfields + 1;
                try
                    set(camsrc,curfield,camsetstruct.(curfield));
                catch
                    countfields = countfields - 1;
                    if countrounds == loadnumprop
                        disp([curfield, ' could not be set']);
                    end
                end
            end
            if countfields == loadnumprop || countrounds == loadnumprop
                doagain = false;
            end
        end
        msgbox('Camera settings changed.');
    else
        msgbox('No properties found to load');
    end
end


% --- Executes on button press in CamDetails.
function CamDetails_Callback(hObject, eventdata, handles)
camobj = handles.camobj;
open camobj;


% --- Executes on button press in Snapshot.
function Snapshot_Callback(hObject, eventdata, handles)
camobj = handles.camobj;
frame = getsnapshot(camobj);
if strcmp(handles.colSpace,'YCbCr')
    frame=ycbcr2rgb(frame);
end
figure;imshow(frame);


% --- Executes on button press in MeasureFramerate.
function MeasureFramerate_Callback(hObject, eventdata, handles)
handles.framerate = measureFrameRate(handles.camobj,20);
set(handles.textFramerate,'String',handles.framerate);
guidata(hObject, handles);


% --- Executes on selection change in CamListbox.
function CamListbox_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function CamListbox_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in FormatListbox.
function FormatListbox_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function FormatListbox_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




function findCamsFunction(hObject, eventdata, handles)
camInfo = imaqhwinfo;
numAdapt = length(camInfo.InstalledAdaptors);
camCounter = 0;
handles.DeviceIDs = [];
if numAdapt>0
   for count=1:numAdapt
        adaptInfo = imaqhwinfo(camInfo.InstalledAdaptors{1,count});
        numDevices = length(adaptInfo.DeviceIDs);
        if numDevices>0
            for count2=1:numDevices
                camCounter = camCounter+1;
                handles.Adaptors{camCounter} = adaptInfo.AdaptorName;
                handles.DeviceIDs(camCounter) = adaptInfo.DeviceIDs{1,count2};
                handles.CamList{camCounter} = strcat(adaptInfo.AdaptorName,'-',num2str(adaptInfo.DeviceIDs{1,count2}));
            end
        end
   end
end
handles.NrCams = length(handles.DeviceIDs);
guidata(hObject, handles);


% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
button = questdlg('Closing can screw up all dependent functions. Continue?','Close CameraGUI','Yes','No','No');
if strcmp(button, 'No')==1
   return 
end
try
closepreview(handles.camobj);
end
try
delete(handles.camobj);
end
delete(hObject);


% --- Executes on button press in DestFile.
function DestFile_Callback(hObject, eventdata, handles)
[savefile,savepath] = uiputfile(handles.curCapturingDir,'Save Movie As');
if isequal(savefile,0)
    handles.capturePath = [];
    handles.captureFileName = [];
    handles.captureFullFilePath = [];
    handles.captureSubFolder = [];
    handles.capturePathInclSubFolder = [];
    displayname = 'no destination file selected';
else
    fullpath = fullfile(savepath,savefile);
    [saveMainPath, name, ext, versn] = fileparts(fullpath);
    handles.capturePath = saveMainPath;
    handles.captureFile = savefile;
    handles.captureFullFilePath = fullpath;
    handles.captureSubFolder = name;
    handles.capturePathInclSubFolder = strcat(saveMainPath,'\',name);
    displayname = fullpath;
    handles.curCapturingDir = [saveMainPath,'\*.avi'];
end
set(handles.textDestFile,'String',displayname);
guidata(hObject, handles);


function capturetime_Callback(hObject, eventdata, handles)
function capturetime_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function framegrabinterval_Callback(hObject, eventdata, handles)
function framegrabinterval_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function framesPerMat_Callback(hObject, eventdata, handles)
function framesPerMat_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in matFileCheck.
function matFileCheck_Callback(hObject, eventdata, handles)
%current value (either 0 or 1) can be called with:
curCheckedVal = get(hObject,'Value');
if curCheckedVal == 1
    set(handles.framesPerMat,'Enable','on');
else
    set(handles.framesPerMat,'Enable','off');
end



% --- Executes on button press in StartCapturing.
function StartCapturing_Callback(hObject, eventdata, handles)
CaptureToMat = get(handles.matFileCheck,'Value');
if isempty(handles.captureFullFilePath)
    button = questdlg('No destination file selected. Continue with default file path?','Warning','Yes','No','Yes');
    if strcmp(button, 'No')==1
        return
    end
end
capturetime = get(handles.capturetime,'String');
framegrabinterval = get(handles.framegrabinterval,'String');
framesPerMat = get(handles.framesPerMat,'String');
capturetime = str2num(capturetime);
framegrabinterval = str2num(framegrabinterval);
framesPerMat = str2num(framesPerMat);
if isempty(capturetime) || isempty(framegrabinterval)
    msgbox('Capture time / framegrab interval wrong!');
    return
end

if CaptureToMat == 0
    if exist(handles.captureFullFilePath,'file')
        msgbox(['File "',handles.captureFullFilePath,'" already exists. Please choose another destination file']);
        return
    end
    set(handles.mainPanel,'Visible','off')
    set(handles.capturePanel,'Visible','off')
    handles.abstimestamps = [];
    handles.abstimestamps = RecordToAvi(handles.camobj, capturetime, handles.captureFullFilePath, framegrabinterval);
%     while isempty(handles.abstimestamps)
%         pause(1)
%     end
    set(handles.mainPanel,'Visible','on')
    set(handles.capturePanel,'Visible','on')
else
    if exist(handles.capturePathInclSubFolder,'dir')
        msgbox(['Folder "',handles.capturePathInclSubFolder,'" already exists. Please choose another destination file name']);
        return
    end
    if isempty(framesPerMat)
    msgbox('#frames / .mat-file wrong!');
    return
    end
    set(handles.mainPanel,'Visible','off')
    set(handles.capturePanel,'Visible','off')
    handles.abstimestamps = [];
    handles.abstimestamps = RecordToMatFiles(handles.camobj, capturetime, handles.captureFullFilePath, framegrabinterval, framesPerMat);
%     while isempty(handles.abstimestamps)
%         pause(1)
%     end
    set(handles.mainPanel,'Visible','on')
    set(handles.capturePanel,'Visible','on')
end
guidata(hObject, handles);


% --- Executes on button press in StartProcessing.
function StartProcessing_Callback(hObject, eventdata, handles)
capturetime = get(handles.capturetime,'String');
framegrabinterval = get(handles.framegrabinterval,'String');
capturetime = str2num(capturetime);
framegrabinterval = str2num(framegrabinterval);
if isempty(capturetime) || isempty(framegrabinterval)
    msgbox('Capture time / framegrab interval wrong!');
    return
end

set(handles.mainPanel,'Visible','off')
set(handles.capturePanel,'Visible','off')
handles.abstimestamps = [];
handles.abstimestamps = RealtimeProcess(handles.camobj, capturetime, framegrabinterval, str2num(get(handles.ProcessGroup,'String')),str2num(get(handles.CallbacksBuffer,'String')),str2num(get(handles.ProcessInterval,'String')));
%     while isempty(handles.abstimestamps)
%         pause(1)
%     end
set(handles.mainPanel,'Visible','on')
set(handles.capturePanel,'Visible','on')

guidata(hObject, handles);



function ProcessGroup_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function ProcessGroup_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function CallbacksBuffer_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function CallbacksBuffer_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ProcessInterval_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function ProcessInterval_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


Contact us