function varargout = trainCameraGUI(varargin)
%TRAINCAMERAGUI M-file for trainCameraGUI.fig
% TRAINCAMERAGUI, by itself, creates a new TRAINCAMERAGUI or raises the existing
% singleton*.
%
% H = TRAINCAMERAGUI returns the handle to a new TRAINCAMERAGUI or the handle to
% the existing singleton*.
%
% TRAINCAMERAGUI('Property','Value',...) creates a new TRAINCAMERAGUI using the
% given property value pairs. Unrecognized properties are passed via
% varargin to trainCameraGUI_OpeningFcn. This calling syntax produces a
% warning when there is an existing singleton*.
%
% TRAINCAMERAGUI('CALLBACK') and TRAINCAMERAGUI('CALLBACK',hObject,...) call the
% local function named CALLBACK in TRAINCAMERAGUI.M with the given input
% arguments.
%
% *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 trainCameraGUI
% Last Modified by GUIDE v2.5 03-Feb-2008 18:15:04
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @trainCameraGUI_OpeningFcn, ...
'gui_OutputFcn', @trainCameraGUI_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 trainCameraGUI is made visible.
function trainCameraGUI_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 unrecognized PropertyName/PropertyValue pairs from the
% command line (see VARARGIN)
% Choose default command line output for trainCameraGUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes trainCameraGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% declare globally defined variables
global supportedModes myHandles maxWidthNHeights mode currRGB myWidth myHeight myOrigX myOrigY;
global colourSpecFile CAPmov fri mdi;
% supported by the Unibrain fire-i camera -> first entry: mode '0', etc.
supportedModes = { ...
'YUV 4:4:4 (160 x 120, 30 fps)', ...
'YUV 4:2:2 (320 x 240, 30 fps)', ...
'YUV 4:1:1 (640 x 480, 30 fps)', ...
'YUV 4:2:2 (640 x 480, 15 fps)', ...
'RGB8 (640 x 480, 15 fps)', ...
'Y8 (640 x 480, 30 fps)' ...
};
maxWidthNHeights = { ...
[160 120], ...
[320 240], ...
[640 480], ...
[640 480], ...
[640 480], ...
[640 480] ...
};
% provide global access to some of the GUI elements...
myHandles.Image = handles.hImage;
myHandles.Y = handles.hY;
myHandles.U = handles.hU;
myHandles.V = handles.hV;
myHandles.SelectButton = handles.hButton2;
myHandles.Ranges = handles.edit11;
myHandles.ColourDef = handles.edit1;
% defaults
mode = 2; % default mode: YUV411 (640x320, 30 fps)
myWidth = 640; % default width (max)
myHeight = 480; % default height (max)
myOrigX = 0; % default origin-x (0)
myOrigY = 0; % default origin-y (0)
% initialize global variable 'colourSpecFile'
colourSpecFile = get(myHandles.ColourDef, 'String');
% switch camera off (in any case...)
capImage(-1);
% open display and histogram window
% check if we're running with live images or not
try
% try to capture a frame...
rgbDummy = capImage(mode, 1, myWidth, myHeight, myOrigX, myOrigY);
clear rgbDummy CAPmov;
catch
% no camera - run of a prerecorded avi movie
fname = 'testmovies/test_short.avi';
disp(['loading ' fname]);
if(~exist('CAPmov', 'var') || isempty(CAPmov))
eval(['CAPmov = aviread(''' fname ''');']);
end
disp('movie loaded.')
% load testimages/testimageYUYVRGB.mat; % YUYV, rgb
% initialise frame index and movie direction
fri = 1; % initialise frame index
mdi = -1; % movie direction
end
% switch to 'display' axis
axes(myHandles.Image);
if(exist('CAPmov', 'var'))
currRGB = CAPmov(fri).cdata;
image(currRGB);
axis image;
myHandles.Title = title(['Displaying frame ' num2str(fri) '/' num2str(length(CAPmov))]);
else
% on-line mode: acquire image data (camera)
currRGB = capImage(mode, 0, myWidth, myHeight, myOrigX, myOrigY);
image(currRGB);
axis image;
end
drawnow;
% nothing has been selected yet -> display (empty) colour info histograms
axes(myHandles.Y);
axis([0 255 0 1]);
ylabel('y', 'FontSize', 10, 'FontWeight', 'bold');
axes(myHandles.U);
axis([0 255 0 1]);
ylabel('U', 'FontSize', 10, 'FontWeight', 'bold');
axes(myHandles.V);
axis([0 255 0 1]);
ylabel('V', 'FontSize', 10, 'FontWeight', 'bold');
% --- Outputs from this function are returned to the command line.
function varargout = trainCameraGUI_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;
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
global colourSpecFile;
colourSpecFile = get(hObject,'String');
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
global mode supportedModes maxWidthNHeights myWidth myHeight myOrigX myOrigY;
myModeStrings = get(hObject, 'String');
myModeString = myModeStrings{get(hObject, 'Value')};
mode = find(ismember(supportedModes, myModeString)) - 1;
myMaxSize = maxWidthNHeights{mode + 1};
myWidth = myMaxSize(1);
myOrigX = 0;
myHeight = myMaxSize(2);
myOrigY = 0;
% update size info
set(handles.edit7, 'String', myWidth);
set(handles.edit8, 'String', myHeight);
set(handles.edit9, 'String', myOrigX);
set(handles.edit10, 'String', myOrigY);
% switch mode...
mySwitchCameraMode;
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit7_Callback(hObject, eventdata, handles)
% hObject handle to edit7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit7 as text
% str2double(get(hObject,'String')) returns contents of edit7 as a double
global myWidth mode maxWidthNHeights;
myMaxSize = maxWidthNHeights{mode + 1};
myWidthStr = get(hObject,'String');
if(any(~isalphanum(myWidthStr)))
myWidth = myMaxSize(1);
else
myWidth = str2double(myWidthStr);
end
if(myWidth <= 0)
myWidth = 2;
elseif(myWidth > myMaxSize(1))
myWidth = myMaxSize(1);
end
% switch mode...
set(hObject, 'String', myWidth);
mySwitchCameraMode;
% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit8_Callback(hObject, eventdata, handles)
% hObject handle to edit8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit8 as text
% str2double(get(hObject,'String')) returns contents of edit8 as a double
global myHeight mode maxWidthNHeights;
myMaxSize = maxWidthNHeights{mode + 1};
myHeightStr = get(hObject,'String');
if(any(~isalphanum(myHeightStr)))
myHeight = myMaxSize(2);
else
myHeight = str2double(myHeightStr);
end
if(myHeight <= 0)
myHeight = 1;
elseif(myHeight > myMaxSize(2))
myHeight = myMaxSize(2);
end
% switch mode...
set(hObject, 'String', myHeight);
mySwitchCameraMode;
% --- Executes during object creation, after setting all properties.
function edit8_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit9_Callback(hObject, eventdata, handles)
% hObject handle to edit9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit9 as text
% str2double(get(hObject,'String')) returns contents of edit9 as a double
global myOrigX myWidth mode maxWidthNHeights;
myMaxSize = maxWidthNHeights{mode + 1};
myOrigXStr = get(hObject,'String');
if(any(~isalphanum(myOrigXStr)))
myOrigX = 0;
else
myOrigX = str2double(myOrigXStr);
end
if(myOrigX < 0)
myOrigX = 0;
elseif(myOrigX + myWidth > myMaxSize(1))
myOrigX = myMaxSize(1) - myWidth;
end
% switch mode...
set(hObject, 'String', myOrigX);
mySwitchCameraMode;
% --- Executes during object creation, after setting all properties.
function edit9_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit10_Callback(hObject, eventdata, handles)
% hObject handle to edit10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit10 as text
% str2double(get(hObject,'String')) returns contents of edit10 as a double
global myOrigY myHeight mode maxWidthNHeights;
myMaxSize = maxWidthNHeights{mode + 1};
myOrigYStr = get(hObject,'String');
if(any(~isalphanum(myOrigYStr)))
myOrigY = 0;
else
myOrigY = str2double(myOrigYStr);
end
if(myOrigY < 0)
myOrigY = 0;
elseif(myOrigY + myHeight > myMaxSize(2))
myOrigY = myMaxSize(2) - myHeight;
end
% switch mode...
set(hObject, 'String', myOrigY);
mySwitchCameraMode;
% --- Executes during object creation, after setting all properties.
function edit10_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in togglebutton14.
function togglebutton14_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton14 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function edit11_Callback(hObject, eventdata, handles)
% hObject handle to edit11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global currRGB myHandles;
% fetch current ranges
myRangesStr = get(hObject, 'String');
% check for expected format
k1 = regexp(myRangesStr, '[0-9]*', 'start');
k2 = regexp(myRangesStr, '[0-9]*', 'end');
if(length(k1) ~= 6 && length(k2) ~= 6)
disp('YUV-Threshold not in expected format (e.g.: 14:22, 126:126, 127:131 )')
else
yMin = str2double(myRangesStr(k1(1):k2(1)));
yMax = str2double(myRangesStr(k1(2):k2(2)));
uMin = str2double(myRangesStr(k1(3):k2(3)));
uMax = str2double(myRangesStr(k1(4):k2(4)));
vMin = str2double(myRangesStr(k1(5):k2(5)));
vMax = str2double(myRangesStr(k1(6):k2(6)));
end
% convert rgb image to yuv format (for colour selection)
yuvObj = rgb2yuv(currRGB);
% create filtered output...
currRGB = filterYUVimage(yuvObj, currRGB, yMin, yMax, uMin, uMax, vMin, vMax);
% get a handle to the current image data
hRGBData = get(myHandles.Image, 'Children');
% replace image data by filtered version...
set(hRGBData, 'CData', currRGB)
% yuv statistics of the selected object: mean +/- 1 STD
yAve = round(mean(mean(yuvObj(:,:,1))));
uAve = round(mean(mean(yuvObj(:,:,2))));
vAve = round(mean(mean(yuvObj(:,:,3))));
yStd = round(std(double(reshape(yuvObj(:,:,1), 1, numel(yuvObj(:,:,1))))));
uStd = round(std(double(reshape(yuvObj(:,:,2), 1, numel(yuvObj(:,:,2))))));
vStd = round(std(double(reshape(yuvObj(:,:,3), 1, numel(yuvObj(:,:,3))))));
numberSTD = 2;
yMin = max([yAve - numberSTD*yStd, yMin]);
yMax = min([yAve + numberSTD*yStd, yMax]);
uMin = max([uAve - numberSTD*uStd, uMin]);
uMax = min([uAve + numberSTD*uStd, uMax]);
vMin = max([vAve - numberSTD*vStd, vMin]);
vMax = min([vAve + numberSTD*vStd, vMax]);
% display colour info histograms
hist(myHandles.Y, reshape(yuvObj(:,:,1), 1, numel(yuvObj(:,:,1))), 0:255);
ax = axis(myHandles.Y);
axis(myHandles.Y, [0 255 0 ax(4)]);
set(myHandles.Y, 'NextPlot', 'add')
plot(myHandles.Y, [max(1, yMin) min(254, yMax)], 0.5*[ax(4) ax(4)], 'r')
set(myHandles.Y, 'NextPlot', 'replace')
hist(myHandles.U, reshape(yuvObj(:,:,2), 1, numel(yuvObj(:,:,2))), 0:255);
ax = axis(myHandles.U);
axis(myHandles.U, [0 255 0 ax(4)]);
set(myHandles.U, 'NextPlot', 'add')
plot(myHandles.U, [max(1, uMin) min(254, uMax)], 0.5*[ax(4) ax(4)], 'r')
set(myHandles.U, 'NextPlot', 'replace')
hist(myHandles.V, reshape(yuvObj(:,:,3), 1, numel(yuvObj(:,:,3))), 0:255);
ax = axis(myHandles.V);
axis(myHandles.V, [0 255 0 ax(4)]);
set(myHandles.V, 'NextPlot', 'add')
plot(myHandles.V, [max(1, vMin) min(254, vMax)], 0.5*[ax(4) ax(4)], 'r')
set(myHandles.V, 'NextPlot', 'replace')
drawnow;
drawnow;
% --- Executes during object creation, after setting all properties.
function edit11_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in hButton1.
function hButton1_Callback(hObject, eventdata, handles)
% hObject handle to hButton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of hButton1
global run myHandles;
toggleState = get(hObject, 'Value');
run = 1 - toggleState;
% toggle visibility state of 'select' button
if(toggleState == 1)
set(myHandles.SelectButton, 'Visible', 'on');
set(hObject, 'String', 'Frozen');
else
set(myHandles.SelectButton, 'Visible', 'off');
set(hObject, 'String', 'Freeze');
end
% --- Executes on button press in hButton2.
function hButton2_Callback(hObject, eventdata, handles)
% hObject handle to hButton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global selectFlag;
% activate selection
selectFlag = 1;
% --- Executes on button press in hButton3.
function hButton3_Callback(hObject, eventdata, handles)
% hObject handle to hButton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of hButton3
global colourSpecFile;
eval(['edit ' colourSpecFile])
% --- Executes on button press in hButton4.
function hButton4_Callback(hObject, eventdata, handles)
% hObject handle to hButton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of hButton4
global colourSpecFile scan4col;
global testColourDef;
% determine state of the 'test' button
testColourDef = get(hObject, 'Value');
% Ensure that colour definition file exists
cdef = dir(colourSpecFile);
if(isempty(cdef))
disp(['The required colour definition file ' colourSpecFile ' could not be found. Assuming default name ''testcolors.txt''.\n']);
colourSpecFile = 'testcolors.txt';
end
% determine the number of colours to be scanned for
h = fopen(colourSpecFile);
nCDef = 0;
while(~feof(h))
myline = fgetl(h);
if(strcmpi(myline, '[thresholds]'))
break;
end
if(strcmpi(myline, '[colors]'))
nCDef = 0;
elseif(~strcmp(myline, ''))
nCDef = nCDef + 1;
disp(['(' num2str(nCDef) ') : ' myline]);
%pause
end
end
fclose(h);
% define vector of colours to be scanned for
scan4col = [1:nCDef];
% --- Executes on button press in hButton5.
function hButton5_Callback(hObject, eventdata, handles)
% hObject handle to hButton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global myHandles;
% copy contents of the ranges edit box to the Windows 'clipboard'
clipboard('copy', get(myHandles.Ranges, 'String'));
% store string as first entry of the [Thresholds] section in the current
% colour definition file...
% Ensure that colour definition file exists
colour_def = get(myHandles.ColourDef, 'String');
cdef = dir(colour_def);
% only proceed if the file can be found...
if(~isempty(cdef))
% open colour definition file (read access)
h = fopen(colour_def);
% find label '[thresholds]'
myline = {};
while(~feof(h))
% get next line
myline{end+1} = fgetl(h);
if(strcmpi(myline{end}, '[thresholds]'))
break;
end
end
% next line
myline{end+1} = [get(myHandles.Ranges, 'String') ' % trainCamera (' date ')'];
% read remaining lines...
while(~feof(h))
% get next line
myline{end+1} = fgetl(h);
end
fclose(h);
% open colour definition file (write access)
h = fopen(colour_def, 'w');
% write line by line
for(ii = 1:length(myline))
%disp(fprintf('%s\n', myline{ii})) ;
fprintf(h, '%s\n', myline{ii}) ;
end
fclose(h);
end
% ========================================================================
% --- switch camera mode, size, etc. This function is called by a number of
% --- the above callback functions (fw-01-08)
function mySwitchCameraMode
global mode myHandles maxWidthNHeights currRGB myWidth myHeight myOrigX myOrigY;
myMaxSize = maxWidthNHeights{mode + 1};
% switch off camera...
capImage(-1);
try
% capture frame using selected mode... (non-verbose)
currRGB = capImage(mode, 0, myWidth, myHeight, myOrigX, myOrigY);
% switch to 'display' axis
axes(myHandles.Image);
[Y, X, dummy] = size(currRGB);
image(myOrigX + [0:X-1], myOrigY + [0:Y-1], currRGB);
axis image;
% maintain overall axis size (better understanding of OrigX/Y...)
axis([0 myMaxSize(1) 0 myMaxSize(2)])
drawnow
catch
disp(['Video mode ' num2str(mode) ' is not supported by the connected camera...'])
end
% isalphanum ======================================================
function logarray = isalphanum(instr)
logarray = instr >= '0' & instr <= '9';
% =================================================================
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end