function varargout = ut_getgabor(varargin)
% UT_GETGABOR: conversion of pixels to textural feature vectors
%       UT_GETGABOR is a graphical tool that can be used to convert a
%       selection of pixels of an input image into a set of corresponding
%       feature vectors. The set of vectors, thus obtained, form the
%       training data for the design of a pixel classifier using, for
%       instance, prtools (see: http://www.prtools.org). Each feature
%       vector contains the color information, and/or the texture
%       information of its corresponding pixel. The texture filters are
%       based on radial Gabor filtering.       

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

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help ut_getgabor

% Last Modified by GUIDE v2.5 09-Dec-2004 14:16:20

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

% Choose default command line output for ut_getgabor
handles.output = [];
handles.imview_in = -1;
handles.imview_gab = -1;
handles.roipoly = [];
handles.inputimage = [];
set(handles.gabview_popup,'String',1:18);
draw_ellips(handles);
h_input_image = ...
    [handles.text2;
    handles.text4;
    handles.text5;
    handles.text6;
    handles.text7;
    handles.imtype_text;
    handles.imH_text;
    handles.imV_text;
    handles.impixels_text;
    handles.imdoubles_text;
    handles.imview_toggle;
    handles.calculate_button;
    handles.text24;
    handles.gabdoubles_text;
    handles.save_features_pushbutton;
    handles.save_vectors_edit;
    handles.save_vec_im_radio;
    ];
h_gaborparms = ...
    [handles.gabview_toggle;
    handles.gabview_popup;
    handles.all_gabimages_radiobutton
    handles.one_gabimage_radiobutton;
    handles.save_gabimage_pushbutton;
    handles.save_gabimage_text;
    handles.save_vec_gab_radio;
    handles.save_vec_all_radio;
    ];
h_selection = ...
    [handles.all_radiobutton;
    handles.random_radiobutton;
    handles.text26;
    handles.randnr_edit;
    handles.grid_radiobutton;
    handles.text29;
    handles.text30;
    handles.deltaH_edit;
    handles.deltaV_edit;
    handles.roi_button;
    handles.roi_pushbutton;
    handles.nr_output_text;
    handles.text32;
    handles.saveroi_pusbutton;
    handles.saveroi_text;
    ];
handles.h_input_image = h_input_image;
handles.h_gaborparms = h_gaborparms;
handles.h_selection = h_selection;
set_enable(h_input_image,'off');
set_enable(h_gaborparms,'off');
set_enable(h_selection,'off');


% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ut_getgabor wait for user response (see UIRESUME)
uiwait(handles.get_gabordata);


% --- Outputs from this function are returned to the command line.
function varargout = ut_getgabor_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
%delete(handles.get_gabordata);



% --- Executes on button press in browse_button.
function browse_button_Callback(hObject, eventdata, handles)
% hObject    handle to browse_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[filename, pathname]= uigetfile({'*.jpg; *.tif; *.gif; *.png'});
if filename==0, return; end;
inputfile=fullfile(pathname,filename);
try
    info = imfinfo(inputfile);
    if strcmp(info.ColorType,'indexed');
        [Iin,map] = imread(inputfile);
        Iin = double(ind2rgb(Iin,map));
        info.ColorType = 'truecolor';
    else
        Iin = double(imread(inputfile));
    end
catch
    errordlg(lasterr,sprintf('error reading %s',inputfile),'modal');
    return
end

if max(Iin(:))>1
    Iin = Iin/255;
end

set_enable(handles.h_input_image,'on');
if get(handles.imview_toggle,'Value')
    if ishandle(handles.imview_in)
        close(handles.imview_in);
    end
    handles.imview_in = figure; imshow(Iin,[]); pixval;
end

set(handles.imtype_text,'String',info.ColorType);
set(handles.imH_text,'String',num2str(info.Width));
set(handles.imV_text,'String',num2str(info.Height));
set(handles.impixels_text,'String',num2str(info.Height*info.Width));
set(handles.imdoubles_text,'String',num2str(prod(size(Iin))));
nfr = str2num(get(handles.nfr_edit,'String'));
nfa = str2num(get(handles.nfa_edit,'String'));
nr_gabdoubles = prod(size(Iin))*nfr*nfa;
set(handles.gabdoubles_text,'String',sprintf('%0.2g',nr_gabdoubles));

set_enable(handles.h_gaborparms,'off');
set_enable(handles.h_selection,'on');
set(handles.nr_output_text,'String',sprintf('%0.2g',prod(size(Iin))));
set(handles.all_radiobutton,'Value',1);
handles.inputfile = inputfile;
handles.inputimage = Iin;
h_edit=handles.inputfile_text;
set(h_edit,'String',filename);
set(handles.gabview_toggle,'Value',0);
handles.outputimages=[];
handles.roipoly = ones(info.Height,info.Width);
handles.grid = mkgrid(handles);
if ishandle(handles.imview_gab), close(handles.imview_gab); end;
guidata(hObject, handles);

function grid = mkgrid(handles)
nrow = size(handles.inputimage,1);
ncol = size(handles.inputimage,2);
grid = zeros(nrow,ncol);
d = str2num(get(handles.deltaH_edit,'String'));
r = round(rem((nrow-1),d)/2+1:d:nrow);
d = str2num(get(handles.deltaV_edit,'String'));
c = round(rem((ncol-1),d)/2+1:d:ncol);
[rr,cc]=meshgrid(r,c);
grid(rr,cc)=1;


% --- Executes on button press in imview_toggle.
function imview_toggle_Callback(hObject, eventdata, handles)
% hObject    handle to imview_toggle (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 imview_toggle

button_state = get(handles.imview_toggle,'Value');
if button_state
    if ~ishandle(handles.imview_in) & ~isempty(handles.inputimage)
        handles.imview_in = figure; imshow(handles.inputimage/max(handles.inputimage(:)),[]); pixval
    end
else
    if ishandle(handles.imview_in)
        close(handles.imview_in);
        handles.imview_in = -1;
    end
end
guidata(hObject, handles);



% --- Executes on button press in calculate_button.
function calculate_button_Callback(hObject, eventdata, handles)
% hObject    handle to calculate_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

memr = 8*str2num(get(handles.gabdoubles_text,'String'))/2^20;
if memr>50
    set(handles.get_gabordata,'WindowStyle','modal');
    button = questdlg(sprintf('This operation requires %3.0f Mbyte of memory\nDo you really want to continue?',...
        memr),'excessive memory requirement','yes','no','yes');
    set(handles.get_gabordata,'WindowStyle','normal');
    if ~strcmp(button,'yes')
        return
    end
end
try
    log_gabor(handles);
catch
    errordlg(lasterr);
    draw_ellips(handles);
    return
end
draw_ellips(handles);
set_enable(handles.h_gaborparms,'on');


function nfr_edit_Callback(hObject, eventdata, handles)
% hObject    handle to nfr_edit (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 nfr_edit as text
%        str2double(get(hObject,'String')) returns contents of nfr_edit as a double
update_after_edit_Gabor_parms(hObject,eventdata, handles);


% --- Executes during object creation, after setting all properties.
function nfr_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to nfr_edit (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function nfa_edit_Callback(hObject, eventdata, handles)
% hObject    handle to nfa_edit (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 nfa_edit as text
%        str2double(get(hObject,'String')) returns contents of nfa_edit as a double
update_after_edit_Gabor_parms(hObject,eventdata, handles);



% --- Executes during object creation, after setting all properties.
function nfa_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to nfa_edit (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function stdeva_edit_Callback(hObject, eventdata, handles)
% hObject    handle to stdeva_edit (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 stdeva_edit as text
%        str2double(get(hObject,'String')) returns contents of stdeva_edit as a double
update_after_edit_Gabor_parms(hObject,eventdata, handles);


% --- Executes during object creation, after setting all properties.
function stdeva_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to stdeva_edit (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function ratio_edit_Callback(hObject, eventdata, handles)
% hObject    handle to ratio_edit (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 ratio_edit as text
%        str2double(get(hObject,'String')) returns contents of ratio_edit as a double
update_after_edit_Gabor_parms(hObject,eventdata, handles);


% --- Executes during object creation, after setting all properties.
function ratio_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ratio_edit (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function peakf_edit_Callback(hObject, eventdata, handles)
% hObject    handle to peakf_edit (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 peakf_edit as text
%        str2double(get(hObject,'String')) returns contents of peakf_edit as a double
update_after_edit_Gabor_parms(hObject,eventdata, handles);


% --- Executes during object creation, after setting all properties.
function peakf_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to peakf_edit (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function stdevr_edit_Callback(hObject, eventdata, handles)
% hObject    handle to stdevr_edit (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 stdevr_edit as text
%        str2double(get(hObject,'String')) returns contents of stdevr_edit as a double
update_after_edit_Gabor_parms(hObject,eventdata, handles);

% --- Executes during object creation, after setting all properties.
function stdevr_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to stdevr_edit (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on button press in close_button.
function close_button_Callback(hObject, eventdata, handles)
% hObject    handle to close_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isempty(handles)
    %varargout{1} = handles.output;
    if ishandle(handles.imview_in), close(handles.imview_in); end;
    if ishandle(handles.imview_gab), close(handles.imview_gab); end;
    close(handles.get_gabordata);
end



% --- Executes on selection change in gabview_popup.
function gabview_popup_Callback(hObject, eventdata, handles)
% hObject    handle to gabview_popup (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 gabview_popup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from gabview_popup
gabview_toggle_Callback(hObject, eventdata, handles);

% --- Executes during object creation, after setting all properties.
function gabview_popup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to gabview_popup (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function draw_ellips(handles)
% draw the ellipses in the axes
nfr = str2num(get(handles.nfr_edit,'String'));
nfa = str2num(get(handles.nfa_edit,'String'));
ratio = str2num(get(handles.ratio_edit,'String'));
peakf = str2num(get(handles.peakf_edit,'String'));
stdevr = str2num(get(handles.stdevr_edit,'String'));
stdeva = str2num(get(handles.stdeva_edit,'String'));
axes(handles.gaborplot);
circle = [cos(0:0.05:2*pi);sin(0:0.05:2*pi)]';
dfi = pi/nfa;
fi = 0;

plot([-1 1],[0 0],'b'); hold on; axis off;
plot([0 0],[-1 1],'b');
for na = 1:2*nfa
    r = peakf;
    for nr = 1:nfr
        ellips(:,1) = exp(log(stdevr)*circle(:,1) + log(r));
        ellips(:,2) =        stdeva*dfi*circle(:,2) + fi;

        [xellips,yellips]=pol2cart(ellips(:,2),ellips(:,1));
        
        %ell = [r*cos(fi); r*sin(fi)]*ones(1,length(circle)) + ...
        %    [cos(fi) -sin(fi); sin(fi) cos(fi)]*[stdevr*r 0;0 r*stdeva*dfi]*circle;
        %plot(ell(1,:),ell(2,:),'k');
        plot(xellips,yellips,'k');
        r = r*ratio;
    end
    fi = fi+dfi;
end
axis([-1 1 -1 1]); axis equal;

fi = 0;
n = 1;
for na = 1:2*nfa
    r = peakf;
    for nr = 1:nfr
        text(r*cos(fi),r*sin(fi),num2str(n),'HorizontalAlignment',...
            'center','Color',[1 0 0],'FontSize',ceil(10*(r+0.5)));
        r = r*ratio;
        n = n+1;
        if n>nfa*nfr, n=1; end;
    end
    fi = fi+dfi;
end
hold off;


function update_after_edit_Gabor_parms(hObject,eventdata, handles)
if isempty(str2num(get(hObject,'String')))
    herror = errordlg('input must be numeric','input error','modal');
    set(hObject,'String',num2str(get(hObject,'UserData')));% retrieve from UserData
    return
end
draw_ellips(handles);
nfr = str2num(get(handles.nfr_edit,'String'));
nfa = str2num(get(handles.nfa_edit,'String'));
if ~isempty(handles.inputimage)
    nr_gabdoubles = prod(size(handles.inputimage))*nfr*nfa;
    set(handles.gabdoubles_text,'String',sprintf('%0.2g',nr_gabdoubles));
end
set(hObject,'UserData',str2num(get(hObject,'String')));     % store value in UserData
set(handles.gabimages_text,'String',num2str(nfr*nfa));
set(handles.gabview_popup,'String',1:nfr*nfa);
%gabview_toggle_Callback(hObject, eventdata, handles);
set_enable(handles.h_gaborparms,'off');

button_state = get(handles.gabview_toggle,'Value');
if button_state
    if ishandle(handles.imview_gab)
        close(handles.imview_gab);
        handles.imview_gab = -1;
    end
end
set(handles.gabview_toggle,'Value',0);
handles.outputimages=[];
guidata(hObject, handles);



function log_gabor(handles) % log-gabor filtering
% Based on: D. J. Field, "Relations between the statistics of natural
% images and the response properties of cortical cells", J. Opt.Soc.Am.A,
% Vol 4, No. 12, December 1987. pp 2379-2394

% retrieve parameters:
Iin = handles.inputimage;
nrow = size(Iin,1);
ncol = size(Iin,2);
nfr = str2num(get(handles.nfr_edit,'String'));      % nr of filters in radial dir.
nfa = str2num(get(handles.nfa_edit,'String'));      % nr of filters in angular dir.
peakf = str2num(get(handles.peakf_edit,'String')); % highest peak frequency
ratio = str2num(get(handles.ratio_edit,'String'));  % ratio of neighboring peak frequencies
stdevr = str2num(get(handles.stdevr_edit,'String'));% radial stddev relative to peak frequency
stdeva = str2num(get(handles.stdeva_edit,'String'));% angular stddev relative to angular distance

% Grid of frequencies
v = -floor(nrow/2):floor((nrow-1)/2);   % horizontal frequencies
u = -floor(ncol/2):floor((ncol-1)/2);   % vertical frequencies

% determine scaling factors to normalize:
u = u/floor((ncol-1)/2);
v = v/floor((nrow-1)/2);

[U,V] = meshgrid(u,v);
R = sqrt(U.^2+V.^2);
R(ceil((nrow+1)/2),ceil((ncol+1)/2)) = eps;
sinrho = -V./R;
cosrho = U./R;
rho = atan2(-V,U);
clear U; clear V;     
dfi = pi/nfa;
varrho = (stdeva*dfi)^2;
fi = 0;

if ndims(Iin) == 3
    IR = fft2(Iin(:,:,1));
    IG = fft2(Iin(:,:,2));
    IB = fft2(Iin(:,:,3));
else
    II = fft2(Iin);
end
Iout = zeros([size(Iin) nfa*nfr]);  % allocate space

n=1;
for na = 1:nfa
    sindif = sin(rho-fi);
    cosdif = cos(rho-fi);
    drho = atan2(sindif,cosdif);
    gauss_rho = exp(-0.5*drho.^2 / varrho);
    gauss_rho = gauss_rho+flipud(fliplr(gauss_rho));

    r = peakf;
    for nr = 1:nfr
        gauss_logr = exp(-0.5*log(R/r).^2 / log(stdevr)^2);
        %gauss_logr = exp(-0.5*(R-r).^2 / (r*stdevr)^2);
        gauss=gauss_rho.*gauss_logr;
        imagesc(gauss);  axis off; drawnow;
        gauss=circshift(gauss,[ceil(nrow/2) ceil(ncol/2)]);
        gauss(:,ceil((ncol+1)/2):end) = 0;
        if ndims(Iin) == 3
            Iout(:,:,1,n) = abs(ifft2(IR.*gauss));
            Iout(:,:,2,n) = abs(ifft2(IG.*gauss));
            Iout(:,:,3,n) = abs(ifft2(IB.*gauss));
        else
            Iout(:,:,n) = abs(ifft2(II.*gauss));
        end
        n = n+1;
        r = r*ratio;
    end
    fi = fi+dfi;
end
handles.outputimages = Iout;
guidata(handles.get_gabordata, handles);

function set_enable(h_array,state)
for i=1:length(h_array);
    set(h_array,'Enable',state);
end

% --- Executes on button press in gabview_toggle.
function gabview_toggle_Callback(hObject, eventdata, handles)
% hObject    handle to gabview_toggle (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 gabview_toggle
button_state = get(handles.gabview_toggle,'Value');
nfr = str2num(get(handles.nfr_edit,'String'));
nfa = str2num(get(handles.nfa_edit,'String'));
n = get(handles.gabview_popup,'Value');
if button_state
    if ishandle(handles.imview_gab)
        figure(handles.imview_gab);
    else
        handles.imview_gab = figure;
    end
    if ndims(handles.inputimage)==3
        RGB = handles.outputimages(:,:,:,n);
%        imshow((RGB-min(RGB(:)))/(max(RGB(:))-min(RGB(:))),[]);
        imshow(RGB/max(RGB(:)));
        title(['Gabor image: ' num2str(n) '  amplitude scaling factor: ' num2str(max(RGB(:)))]); 
        pixval;
    else
        imshow(handles.outputimages(:,:,n),[]); pixval;
    end
else
    if ishandle(handles.imview_gab)
        close(handles.imview_gab);
        handles.imview_gab = -1;
    end
end
guidata(hObject, handles);





function randnr_edit_Callback(hObject, eventdata, handles)
% hObject    handle to randnr_edit (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 randnr_edit as text
%        str2double(get(hObject,'String')) returns contents of randnr_edit as a double
update_nr_output_vec(handles);

% --- Executes during object creation, after setting all properties.
function randnr_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to randnr_edit (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function deltaH_edit_Callback(hObject, eventdata, handles)
% hObject    handle to deltaH_edit (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 deltaH_edit as text
%        str2double(get(hObject,'String')) returns contents of deltaH_edit as a double
handles.grid = mkgrid(handles);
guidata(hObject, handles);
update_nr_output_vec(handles);

% --- Executes during object creation, after setting all properties.
function deltaH_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to deltaH_edit (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function deltaV_edit_Callback(hObject, eventdata, handles)
% hObject    handle to deltaV_edit (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 deltaV_edit as text
%        str2double(get(hObject,'String')) returns contents of deltaV_edit as a double
handles.grid = mkgrid(handles);
guidata(hObject, handles);
update_nr_output_vec(handles);


% --- Executes during object creation, after setting all properties.
function deltaV_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to deltaV_edit (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on button press in roi_button.
function roi_button_Callback(hObject, eventdata, handles)
% hObject    handle to roi_button (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 roi_button
update_nr_output_vec(handles);




% --- Executes on button press in roi_pushbutton.
function roi_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to roi_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
hdlg=helpdlg('Create a polygon enclosing the ROI','Roi Selection');
pause(3);
try, close(hdlg); catch, end;
hfig = figure; 

handles.roipoly = roipoly(handles.inputimage);
close(hfig);
guidata(hObject, handles);
update_nr_output_vec(handles);


% --- Executes on button press in save_gabimage_pushbutton.
function save_gabimage_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to save_gabimage_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FileName,PathName] = uiputfile({'*.jpg; *.tif; *.gif; *.png'});
if FileName==0, return; end;
outputfile=fullfile(PathName,FileName);
[dummy1,dummy2,ext] = fileparts(FileName);
if isempty(ext)
    outputfile = [outputfile '.jpg'];
end
if get(handles.one_gabimage_radiobutton,'Value')
    set(handles.save_gabimage_text,'String',FileName);
    n = get(handles.gabview_popup,'Value');
    if ndims(handles.inputimage)==3
        RGB = handles.outputimages(:,:,:,n);
        %        imshow((RGB-min(RGB(:)))/(max(RGB(:))-min(RGB(:))),[]);
        imwrite(RGB,outputfile);
    else
        try
            imwrite(handles.outputimages(:,:,n),outputfile);
        catch
            errordlg(lasterr,'','modal');
        end
    end
else
    [PathName,FileName,ext] = fileparts(outputfile);
    set(handles.save_gabimage_text,'String',[FileName '_*' ext]);
    nimages = size(handles.outputimages,ndims(handles.outputimages));
    for n=1:nimages
        outputfile = sprintf('%s\\%s_%d%s',PathName,FileName,n,ext);
        if ndims(handles.inputimage)==3
            RGB = handles.outputimages(:,:,:,n);
            %        imshow((RGB-min(RGB(:)))/(max(RGB(:))-min(RGB(:))),[]);
            imwrite(RGB,outputfile);
        else
            try
                imwrite(handles.outputimages(:,:,n),outputfile);
            catch
                errordlg(lasterr,'','modal');
            end
        end
    end
end


% --- Executes on button press in save_features_pushbutton.
function save_features_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to save_features_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FileName,PathName] = uiputfile('*.mat','output file',get(handles.save_vectors_edit,'String'));
if FileName==0, return; end;
outputfile=fullfile(PathName,FileName);
[dummy1,fname] = fileparts(outputfile);
set(handles.save_vectors_edit,'String',FileName);
nrand = str2num(get(handles.randnr_edit,'String'));
nrow = size(handles.inputimage,1);
ncol = size(handles.inputimage,2);
mask = ones(nrow,ncol);
poly_on = get(handles.roi_button,'Value');

selstate = get(handles.all_radiobutton,'Value') + ...
    2*get(handles.random_radiobutton,'Value') + ...
    4*get(handles.grid_radiobutton,'Value');
if poly_on
    mask = mask .* handles.roipoly;
end
if selstate==4
    mask = mask .* handles.grid;
end
ind = find(mask(:));
if selstate==2 & nrand<length(ind)
    pind= randperm(length(ind));
    ind = ind(pind);
    ind = ind(1:nrand);
end

save_state = get(handles.save_vec_im_radio,'Value') + ...
    2*get(handles.save_vec_gab_radio,'Value') + ...
    4*get(handles.save_vec_all_radio,'Value');
sizinput = size(handles.inputimage);
sizgab = size(handles.outputimages);
handles.inputimage = reshape(handles.inputimage,nrow*ncol,prod(sizinput)/(nrow*ncol));
handles.outputimages=reshape(handles.outputimages,nrow*ncol,prod(sizgab)/(nrow*ncol));

if save_state==1
    eval(sprintf('%s=handles.inputimage(ind,:);',fname));
end
if save_state==2
 %   vec=handles.outputimages(ind,:);
    eval(sprintf('%s=handles.outputimages(ind,:);',fname));
end
if save_state==4
%    vec = [handles.inputimage(ind,:) handles.outputimages(ind,:)];
    eval(sprintf('%s=[handles.inputimage(ind,:) handles.outputimages(ind,:)];',fname));
end
eval(sprintf('save  ''%s'' ''%s'';',outputfile,fname));
%imwrite(vec,outputfile,'tif');
handles.inputimage = reshape(handles.inputimage,sizinput);
handles.outputimages=reshape(handles.outputimages,sizgab);



function update_nr_output_vec(handles)
nrand = str2num(get(handles.randnr_edit,'String'));
nrow = size(handles.inputimage,1);
ncol = size(handles.inputimage,2);
mask = ones(nrow,ncol);
poly_on = get(handles.roi_button,'Value');
selstate = get(handles.all_radiobutton,'Value') + ...
    2*get(handles.random_radiobutton,'Value') + ...
    4*get(handles.grid_radiobutton,'Value');
if poly_on
    mask = mask .* handles.roipoly;
end
if selstate==4
    mask = mask .* handles.grid;
end
npixels = length(find(mask(:)));
if selstate==2
    npixels = min([npixels nrand]);
end
set(handles.nr_output_text,'String',sprintf('%0.2g',npixels));


% --- Executes on button press in all_radiobutton.
function all_radiobutton_Callback(hObject, eventdata, handles)
% hObject    handle to all_radiobutton (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 all_radiobutton
set(handles.all_radiobutton,'Value',1);
set(handles.random_radiobutton,'Value',0);
set(handles.grid_radiobutton,'Value',0);
update_nr_output_vec(handles);



% --- Executes on button press in random_radiobutton.
function random_radiobutton_Callback(hObject, eventdata, handles)
% hObject    handle to random_radiobutton (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 random_radiobutton
set(handles.all_radiobutton,'Value',0);
set(handles.random_radiobutton,'Value',1);
set(handles.grid_radiobutton,'Value',0);
update_nr_output_vec(handles);




% --- Executes on button press in grid_radiobutton.
function grid_radiobutton_Callback(hObject, eventdata, handles)
% hObject    handle to grid_radiobutton (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 grid_radiobutton
set(handles.all_radiobutton,'Value',0);
set(handles.random_radiobutton,'Value',0);
set(handles.grid_radiobutton,'Value',1);
update_nr_output_vec(handles);






% --- Executes on button press in save_vec_gab_radio.
function save_vec_gab_radio_Callback(hObject, eventdata, handles)
% hObject    handle to save_vec_gab_radio (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 save_vec_gab_radio
set(handles.save_vec_gab_radio,'Value',1);
set(handles.save_vec_im_radio,'Value',0);
set(handles.save_vec_all_radio,'Value',0);

% --- Executes on button press in save_vec_im_radio.
function save_vec_im_radio_Callback(hObject, eventdata, handles)
% hObject    handle to save_vec_im_radio (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 save_vec_im_radio
set(handles.save_vec_gab_radio,'Value',0);
set(handles.save_vec_im_radio,'Value',1);
set(handles.save_vec_all_radio,'Value',0);


% --- Executes on button press in save_vec_all_radio.
function save_vec_all_radio_Callback(hObject, eventdata, handles)
% hObject    handle to save_vec_all_radio (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 save_vec_all_radio
set(handles.save_vec_gab_radio,'Value',0);
set(handles.save_vec_im_radio,'Value',0);
set(handles.save_vec_all_radio,'Value',1);




% --- Executes on button press in help_pushbutton.
function help_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to help_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
HelpPath = which('ut_getgabor_help.htm');
web(HelpPath); 



% --- Executes on button press in one_gabimage_radiobutton.
function one_gabimage_radiobutton_Callback(hObject, eventdata, handles)
% hObject    handle to one_gabimage_radiobutton (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 one_gabimage_radiobutton
set(handles.all_gabimages_radiobutton,'Value',0);

% --- Executes on button press in all_gabimages_radiobutton.
function all_gabimages_radiobutton_Callback(hObject, eventdata, handles)
% hObject    handle to all_gabimages_radiobutton (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 all_gabimages_radiobutton
set(handles.one_gabimage_radiobutton,'Value',0);


% --- Executes on button press in saveroi_pusbutton.
function saveroi_pusbutton_Callback(hObject, eventdata, handles)
% hObject    handle to saveroi_pusbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FileName,PathName] = uiputfile({'*.jpg; *.tif; *.gif; *.png'},'roi filename',get(handles.saveroi_text,'String'));
if FileName==0, return; end;
outputfile=fullfile(PathName,FileName);
[dummy1,dummy2,ext] = fileparts(FileName);
if isempty(ext)
    outputfile = [outputfile '.jpg'];
end
set(handles.saveroi_text,'String',FileName);
try
    imwrite(handles.roipoly,outputfile);
catch
    errordlg(lasterr,'','modal');
end




% --- Creates and returns a handle to the GUI figure. 
function h1 = ut_getgabor_LayoutFcn(policy)
% policy - create a new figure or use a singleton. 'new' or 'reuse'.

persistent hsingleton;
if strcmpi(policy, 'reuse') & ishandle(hsingleton)
    h1 = hsingleton;
    return;
end

h1 = figure(...
'Units','characters',...
'PaperUnits',get(0,'defaultfigurePaperUnits'),...
'Color',[0.831372549019608 0.815686274509804 0.784313725490196],...
'Colormap',[0 0 0.5625;0 0 0.625;0 0 0.6875;0 0 0.75;0 0 0.8125;0 0 0.875;0 0 0.9375;0 0 1;0 0.0625 1;0 0.125 1;0 0.1875 1;0 0.25 1;0 0.3125 1;0 0.375 1;0 0.4375 1;0 0.5 1;0 0.5625 1;0 0.625 1;0 0.6875 1;0 0.75 1;0 0.8125 1;0 0.875 1;0 0.9375 1;0 1 1;0.0625 1 1;0.125 1 0.9375;0.1875 1 0.875;0.25 1 0.8125;0.3125 1 0.75;0.375 1 0.6875;0.4375 1 0.625;0.5 1 0.5625;0.5625 1 0.5;0.625 1 0.4375;0.6875 1 0.375;0.75 1 0.3125;0.8125 1 0.25;0.875 1 0.1875;0.9375 1 0.125;1 1 0.0625;1 1 0;1 0.9375 0;1 0.875 0;1 0.8125 0;1 0.75 0;1 0.6875 0;1 0.625 0;1 0.5625 0;1 0.5 0;1 0.4375 0;1 0.375 0;1 0.3125 0;1 0.25 0;1 0.1875 0;1 0.125 0;1 0.0625 0;1 0 0;0.9375 0 0;0.875 0 0;0.8125 0 0;0.75 0 0;0.6875 0 0;0.625 0 0;0.5625 0 0],...
'IntegerHandle','off',...
'InvertHardcopy',get(0,'defaultfigureInvertHardcopy'),...
'MenuBar','none',...
'Name','getgabor_gui',...
'NumberTitle','off',...
'PaperPosition',[-1.55985552083333 8.4866715625 24.111665 12.7167882291667],...
'PaperSize',[20.98404194812 29.67743169791],...
'PaperType',get(0,'defaultfigurePaperType'),...
'Position',[10 10.1538461538462 182.4 37],...
'Renderer',get(0,'defaultfigureRenderer'),...
'RendererMode','manual',...
'Resize','off',...
'HandleVisibility','callback',...
'Tag','get_gabordata',...
'UserData',[]);

setappdata(h1, 'GUIDEOptions',struct(...
'active_h', [], ...
'taginfo', struct(...
'figure', 2, ...
'axes', 4, ...
'pushbutton', 11, ...
'uipanel', 14, ...
'text', 49, ...
'edit', 12, ...
'togglebutton', 3, ...
'radiobutton', 12, ...
'popupmenu', 4, ...
'listbox', 2, ...
'frame', 12), ...
'override', 0, ...
'release', 13, ...
'resize', 'none', ...
'accessibility', 'callback', ...
'mfile', 1, ...
'callbacks', 1, ...
'singleton', 1, ...
'syscolorfig', 1, ...
'blocking', 0, ...
'lastSavedFile', 'C:\Documents and Settings\ferdi\My Documents\matlab\CE projects\unsupervised learning\getgabor_gui.m'));


h2 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'ListboxTop',0,...
'Position',[119.8 1.46153846153846 60.2 5.46153846153846],...
'String',{  '' },...
'Style','frame',...
'Tag','frame10');


h3 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'ListboxTop',0,...
'Position',[79.8 1.46153846153846 38.2 5.46153846153846],...
'String',{  '' },...
'Style','frame',...
'Tag','frame9');


h4 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'ListboxTop',0,...
'Position',[59.8 8.38461538461539 54 25.4615384615385],...
'String',{  '' },...
'Style','frame',...
'Tag','frame7');


h5 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'ListboxTop',0,...
'Position',[63.6 9.92307692307692 48.2 5.46153846153846],...
'String',{  '' },...
'Style','frame',...
'Tag','frame8');


h6 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'ListboxTop',0,...
'Position',[63.6 20.6923076923077 48.2 3.92307692307692],...
'String',{  '' },...
'Style','frame',...
'Tag','frame6');


h7 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'ListboxTop',0,...
'Position',[63.6 26.0769230769231 48.2 6.23076923076923],...
'String',{  '' },...
'Style','frame',...
'Tag','frame5');


h8 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'ListboxTop',0,...
'Position',[1.8 1.46153846153846 56.2 17.7692307692308],...
'String',{  '' },...
'Style','frame',...
'Tag','frame2');


h9 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'ListboxTop',0,...
'Position',[7.8 6.84615384615385 48.2 11],...
'String',{  '' },...
'Style','frame',...
'Tag','frame3');


h10 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'ListboxTop',0,...
'Position',[1.8 20.6923076923077 56.2 13.1538461538462],...
'String','input image',...
'Style','frame',...
'Tag','frame1');


h11 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''close_button_Callback'',gcbo,[],guidata(gcbo))',...
'ListboxTop',0,...
'Position',[164 34.7692307692308 13.8 1.76923076923077],...
'String','close',...
'Tag','close_button');


h12 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'FontSize',20,...
'ListboxTop',0,...
'Position',[8.6 34.6153846153846 127.4 2.46153846153846],...
'String','conversion of pixels to feature vectors',...
'Style','text',...
'Tag','text25');


h13 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[2.2 -0.230769230769231 20.4 1.23076923076923],...
'String','© F. van der Heijden',...
'Style','text',...
'Tag','text34');


h14 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''help_pushbutton_Callback'',gcbo,[],guidata(gcbo))',...
'ListboxTop',0,...
'Position',[148.4 34.7692307692308 13.8 1.76923076923077],...
'String','help',...
'Tag','help_pushbutton');


h15 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'CData',[],...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[5.8 30.9230769230769 37.8 1.53846153846154],...
'String','<empty>',...
'Style','text',...
'Tag','inputfile_text',...
'UserData',[]);


h16 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'CData',[],...
'HorizontalAlignment','right',...
'ListboxTop',0,...
'Position',[26.6 29.0769230769231 14.6 1.38461538461538],...
'String','image type:',...
'Style','text',...
'Tag','text2',...
'UserData',[]);


h17 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'CData',[],...
'HorizontalAlignment','right',...
'ListboxTop',0,...
'Position',[26 25.5384615384615 14.6 1.38461538461538],...
'String','nr. of rows:',...
'Style','text',...
'Tag','text4',...
'UserData',[]);


h18 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'CData',[],...
'HorizontalAlignment','right',...
'ListboxTop',0,...
'Position',[26 27.4615384615385 14.6 1.38461538461538],...
'String','nr. of columns:',...
'Style','text',...
'Tag','text5',...
'UserData',[]);


h19 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'CData',[],...
'HorizontalAlignment','right',...
'ListboxTop',0,...
'Position',[26 23.8461538461538 14.6 1.38461538461538],...
'String','nr.of pixels:',...
'Style','text',...
'Tag','text6',...
'UserData',[]);


h20 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'CData',[],...
'HorizontalAlignment','right',...
'ListboxTop',0,...
'Position',[26 22 14.6 1.38461538461538],...
'String','nr.of doubles:',...
'Style','text',...
'Tag','text7',...
'UserData',[]);


h21 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'CData',[],...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[41.2 29.0769230769231 14.6 1.38461538461538],...
'Style','text',...
'Tag','imtype_text',...
'UserData',[]);


h22 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'CData',[],...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[41.2 27.3076923076923 14.6 1.38461538461538],...
'Style','text',...
'Tag','imH_text',...
'UserData',[]);


h23 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'CData',[],...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[41.2 25.5384615384615 14.6 1.38461538461538],...
'Style','text',...
'Tag','imV_text',...
'UserData',[]);


h24 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'CData',[],...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[41.2 23.7692307692308 14.6 1.38461538461538],...
'Style','text',...
'Tag','impixels_text',...
'UserData',[]);


h25 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'CData',[],...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[41.2 22 14.6 1.38461538461538],...
'Style','text',...
'Tag','imdoubles_text',...
'UserData',[]);


h26 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''browse_button_Callback'',gcbo,[],guidata(gcbo))',...
'CData',[],...
'ForegroundColor',[1 0 0],...
'ListboxTop',0,...
'Position',[41 30.7692307692308 14.8 1.76923076923077],...
'String','open',...
'Tag','browse_button',...
'UserData',[]);


h27 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''imview_toggle_Callback'',gcbo,[],guidata(gcbo))',...
'CData',[],...
'Enable','off',...
'ListboxTop',0,...
'Position',[6 21.9230769230769 15.4 1.76923076923077],...
'String','view image',...
'Style','togglebutton',...
'Tag','imview_toggle',...
'UserData',[]);


h28 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','right',...
'ListboxTop',0,...
'Position',[79.8 30.6153846153846 21 1.15384615384615],...
'String','number of filters:',...
'Style','text',...
'Tag','text14');


h29 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','right',...
'ListboxTop',0,...
'Position',[65.8 28.0769230769231 35 1.23076923076923],...
'String','highest peak frequency  (0-1):',...
'Style','text',...
'Tag','text15');


h30 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','right',...
'ListboxTop',0,...
'Position',[64 29.2307692307692 36.8 1.38461538461538],...
'String','ratio of neighboring peak frequencies:',...
'Style','text',...
'Tag','text19');


h31 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','right',...
'ListboxTop',0,...
'Position',[66.2 26.9230769230769 34.6 1.15384615384615],...
'String','relative bandwidth (std. dev.):',...
'Style','text',...
'Tag','text18');


h32 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'Callback','ut_getgabor(''nfr_edit_Callback'',gcbo,[],guidata(gcbo))',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[101 30.6153846153846 9 1.38461538461538],...
'String','3',...
'Style','edit',...
'CreateFcn','ut_getgabor(''nfr_edit_CreateFcn'',gcbo,[],guidata(gcbo))',...
'Tag','nfr_edit');


h33 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'Callback','ut_getgabor(''ratio_edit_Callback'',gcbo,[],guidata(gcbo))',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[101 29.2307692307692 9 1.38461538461538],...
'String','0.5',...
'Style','edit',...
'CreateFcn','ut_getgabor(''ratio_edit_CreateFcn'',gcbo,[],guidata(gcbo))',...
'Tag','ratio_edit');


h34 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'Callback','ut_getgabor(''peakf_edit_Callback'',gcbo,[],guidata(gcbo))',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[101 27.9230769230769 9 1.38461538461538],...
'String','0.8',...
'Style','edit',...
'CreateFcn','ut_getgabor(''peakf_edit_CreateFcn'',gcbo,[],guidata(gcbo))',...
'Tag','peakf_edit');


h35 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'Callback','ut_getgabor(''stdevr_edit_Callback'',gcbo,[],guidata(gcbo))',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[101 26.6923076923077 9 1.38461538461538],...
'String','0.65',...
'Style','edit',...
'CreateFcn','ut_getgabor(''stdevr_edit_CreateFcn'',gcbo,[],guidata(gcbo))',...
'Tag','stdevr_edit');


h36 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','right',...
'ListboxTop',0,...
'Position',[78.6 22.7692307692308 22.6 1.30769230769231],...
'String','number of filters:',...
'Style','text',...
'Tag','text17');


h37 = axes(...
'Parent',h1,...
'Units','characters',...
'CameraPosition',[0.5 0.5 9.16025403784439],...
'CameraPositionMode',get(0,'defaultaxesCameraPositionMode'),...
'Color',get(0,'defaultaxesColor'),...
'ColorOrder',get(0,'defaultaxesColorOrder'),...
'Position',[115.8 8.38461538461539 64.2 24.6923076923077],...
'XColor',get(0,'defaultaxesXColor'),...
'YColor',get(0,'defaultaxesYColor'),...
'ZColor',get(0,'defaultaxesZColor'),...
'Tag','gaborplot');


h38 = get(h37,'title');

set(h38,...
'Parent',h37,...
'Color',[0 0 0],...
'HorizontalAlignment','center',...
'Position',[0.498397435897435 1.02272727272727 1.00005459937205],...
'VerticalAlignment','bottom',...
'HandleVisibility','off');

h39 = get(h37,'xlabel');

set(h39,...
'Parent',h37,...
'Color',[0 0 0],...
'HorizontalAlignment','center',...
'Position',[0.498397435897435 -0.0821678321678319 1.00005459937205],...
'VerticalAlignment','cap',...
'HandleVisibility','off');

h40 = get(h37,'ylabel');

set(h40,...
'Parent',h37,...
'Color',[0 0 0],...
'HorizontalAlignment','center',...
'Position',[-0.091346153846154 0.494755244755245 1.00005459937205],...
'Rotation',90,...
'VerticalAlignment','bottom',...
'HandleVisibility','off');

h41 = get(h37,'zlabel');

set(h41,...
'Parent',h37,...
'Color',[0 0 0],...
'HorizontalAlignment','right',...
'Position',[-1.80529595015576 1.15420560747664 1.00005459937205],...
'HandleVisibility','off',...
'Visible','off');

h42 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','right',...
'ListboxTop',0,...
'Position',[66.4 21.3846153846154 34.8 1.38461538461538],...
'String','relative bandwidth (std. dev.):',...
'Style','text',...
'Tag','text13');


h43 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'Callback','ut_getgabor(''nfa_edit_Callback'',gcbo,[],guidata(gcbo))',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[101.2 22.7692307692308 9 1.38461538461538],...
'String','6',...
'Style','edit',...
'CreateFcn','ut_getgabor(''nfa_edit_CreateFcn'',gcbo,[],guidata(gcbo))',...
'Tag','nfa_edit',...
'UserData',6);


h44 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'Callback','ut_getgabor(''stdeva_edit_Callback'',gcbo,[],guidata(gcbo))',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[101.2 21.4615384615385 9 1.38461538461538],...
'String','0.667',...
'Style','edit',...
'CreateFcn','ut_getgabor(''stdeva_edit_CreateFcn'',gcbo,[],guidata(gcbo))',...
'Tag','stdeva_edit');


h45 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'ListboxTop',0,...
'Position',[64 18.7692307692308 14.6 1.15384615384615],...
'String','nr. of images:',...
'Style','text',...
'Tag','text20');


h46 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''calculate_button_Callback'',gcbo,[],guidata(gcbo))',...
'Enable','off',...
'ForegroundColor',[1 0 0],...
'ListboxTop',0,...
'Position',[92 17 19.6 3.23076923076923],...
'String','calculate them!',...
'Tag','calculate_button');


h47 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[78.8 18.6153846153846 12.4 1.38461538461538],...
'String','18',...
'Style','text',...
'Tag','gabimages_text');


h48 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[78.8 17.0769230769231 12.4 1.38461538461538],...
'String','',...
'Style','text',...
'Tag','gabdoubles_text');


h49 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'ListboxTop',0,...
'Position',[64.2 17.3846153846154 14.6 1.15384615384615],...
'String','nr. of doubles:',...
'Style','text',...
'Tag','text24');


h50 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''gabview_toggle_Callback'',gcbo,[],guidata(gcbo))',...
'Enable','off',...
'ListboxTop',0,...
'Position',[66 12.3076923076923 29.4 1.76923076923077],...
'String','view Gabor image',...
'Style','togglebutton',...
'Tag','gabview_toggle');


h51 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'Callback','ut_getgabor(''gabview_popup_Callback'',gcbo,[],guidata(gcbo))',...
'Enable','off',...
'ListboxTop',0,...
'Position',[95 12.3076923076923 15 1.69230769230769],...
'String',{  '1'; '2'; '3'; '4'; '5'; '6' },...
'Style','popupmenu',...
'Value',1,...
'CreateFcn','ut_getgabor(''gabview_popup_CreateFcn'',gcbo,[],guidata(gcbo))',...
'Tag','gabview_popup');


h52 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''all_radiobutton_Callback'',gcbo,[],guidata(gcbo))',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[16.2 16.1538461538462 19.2 1.30769230769231],...
'String','all image pixels',...
'Style','radiobutton',...
'Value',1,...
'Tag','all_radiobutton');


h53 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''random_radiobutton_Callback'',gcbo,[],guidata(gcbo))',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[16.2 14.6923076923077 21.6 1.15384615384615],...
'String','random selection',...
'Style','radiobutton',...
'Tag','random_radiobutton');


h54 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Enable','off',...
'HorizontalAlignment','right',...
'ListboxTop',0,...
'Position',[31 13.1538461538462 12.6 1.23076923076923],...
'String','nr. of pixels:',...
'Style','text',...
'Tag','text26');


h55 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'Callback','ut_getgabor(''randnr_edit_Callback'',gcbo,[],guidata(gcbo))',...
'Enable','off',...
'ListboxTop',0,...
'Position',[44.4 12.8461538461538 11.2 1.61538461538462],...
'String','1000',...
'Style','edit',...
'CreateFcn','ut_getgabor(''randnr_edit_CreateFcn'',gcbo,[],guidata(gcbo))',...
'Tag','randnr_edit');


h56 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''grid_radiobutton_Callback'',gcbo,[],guidata(gcbo))',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[16.4 10.4615384615385 16.4 1.15384615384615],...
'String','regular grid',...
'Style','radiobutton',...
'Tag','grid_radiobutton');


h57 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Enable','off',...
'HorizontalAlignment','right',...
'ListboxTop',0,...
'Position',[31 7.76923076923077 12.6 1.23076923076923],...
'String','delta V:',...
'Style','text',...
'Tag','text29');


h58 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Enable','off',...
'HorizontalAlignment','right',...
'ListboxTop',0,...
'Position',[31 9.46153846153846 12.6 1.23076923076923],...
'String','delta H:',...
'Style','text',...
'Tag','text30');


h59 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'Callback','ut_getgabor(''deltaH_edit_Callback'',gcbo,[],guidata(gcbo))',...
'Enable','off',...
'ListboxTop',0,...
'Position',[44.4 9.30769230769231 11.2 1.61538461538462],...
'String','4',...
'Style','edit',...
'CreateFcn','ut_getgabor(''deltaH_edit_CreateFcn'',gcbo,[],guidata(gcbo))',...
'Tag','deltaH_edit');


h60 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'Callback','ut_getgabor(''deltaV_edit_Callback'',gcbo,[],guidata(gcbo))',...
'Enable','off',...
'ListboxTop',0,...
'Position',[44.2 7.53846153846154 11.4 1.69230769230769],...
'String','4',...
'Style','edit',...
'CreateFcn','ut_getgabor(''deltaV_edit_CreateFcn'',gcbo,[],guidata(gcbo))',...
'Tag','deltaV_edit');


h61 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''roi_button_Callback'',gcbo,[],guidata(gcbo))',...
'ListboxTop',0,...
'Position',[13.4 4.61538461538462 28.8 1.15384615384615],...
'String','only inside polygonal ROI',...
'Style','radiobutton',...
'Tag','roi_button');


h62 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[41.2 2.38461538461538 14.6 1.38461538461538],...
'String','',...
'Style','text',...
'Tag','nr_output_text');


h63 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','right',...
'ListboxTop',0,...
'Position',[12.4 2.61538461538462 28.6 1.15384615384615],...
'String','nr.of output feature vectors:',...
'Style','text',...
'Tag','text32');


h64 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''roi_pushbutton_Callback'',gcbo,[],guidata(gcbo))',...
'ForegroundColor',[1 0 0],...
'ListboxTop',0,...
'Position',[44.4 4.38461538461539 11.4 1.76923076923077],...
'String','define ROI',...
'Tag','roi_pushbutton');


h65 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''all_gabimages_radiobutton_Callback'',gcbo,[],guidata(gcbo))',...
'ListboxTop',0,...
'Position',[95.8 5.30769230769231 16.4 1.15384615384615],...
'String','all',...
'Style','radiobutton',...
'Tag','all_gabimages_radiobutton');


h66 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''one_gabimage_radiobutton_Callback'',gcbo,[],guidata(gcbo))',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[95.8 4.07692307692308 21.6 1.15384615384615],...
'String','only selected one',...
'Style','radiobutton',...
'Value',1,...
'Tag','one_gabimage_radiobutton');


h67 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''save_gabimage_pushbutton_Callback'',gcbo,[],guidata(gcbo))',...
'ListboxTop',0,...
'Position',[81.8 4.38461538461539 12.2 1.76923076923077],...
'String','save',...
'Tag','save_gabimage_pushbutton');


h68 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'ListboxTop',0,...
'Position',[82 2.23076923076923 24 1.61538461538462],...
'String','',...
'Style','text',...
'Tag','save_gabimage_text');


h69 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''save_vec_all_radio_Callback'',gcbo,[],guidata(gcbo))',...
'ListboxTop',0,...
'Position',[149.4 2.53846153846154 30.2 1.15384615384615],...
'String','input image + Gabor images',...
'Style','radiobutton',...
'Tag','save_vec_all_radio');


h70 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''save_vec_gab_radio_Callback'',gcbo,[],guidata(gcbo))',...
'ListboxTop',0,...
'Position',[149.4 3.92307692307692 29.8 1.15384615384615],...
'String','from Gabor images only',...
'Style','radiobutton',...
'Tag','save_vec_gab_radio');


h71 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''save_vec_im_radio_Callback'',gcbo,[],guidata(gcbo))',...
'ListboxTop',0,...
'Position',[149.4 5.23076923076923 29.8 1.15384615384615],...
'String','from input image only',...
'Style','radiobutton',...
'Value',1,...
'Tag','save_vec_im_radio');


h72 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''save_features_pushbutton_Callback'',gcbo,[],guidata(gcbo))',...
'ListboxTop',0,...
'Position',[121.8 4.38461538461539 12.2 1.76923076923077],...
'String','save',...
'Tag','save_features_pushbutton');


h73 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'ListboxTop',0,...
'Position',[121.8 2.23076923076923 26.2 1.61538461538462],...
'String',' ',...
'Style','text',...
'Tag','save_vectors_edit');


h74 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[4.2 33.7692307692308 13.6 1.15384615384615],...
'String',' input image:',...
'Style','text',...
'Tag','text37');


h75 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[3.8 19.0769230769231 18.2 1.15384615384615],...
'String',' selecton of pixels:',...
'Style','text',...
'Tag','text38');


h76 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[9.8 17.6923076923077 18.2 1.15384615384615],...
'String',' sampling method: ',...
'Style','text',...
'Tag','text39');


h77 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[61.6 33.6923076923077 21.8 1.15384615384615],...
'String',' radial Gabor filtering:  ',...
'Style','text',...
'Tag','text40');


h78 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[65.4 32.1538461538462 30.2 1.15384615384615],...
'String',' parameters in radial direction:  ',...
'Style','text',...
'Tag','text41');


h79 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[65.4 24.4615384615385 32.2 1.15384615384615],...
'String',' parameters in angular direction:  ',...
'Style','text',...
'Tag','text42');


h80 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[65.4 15.2307692307692 32.2 1.15384615384615],...
'String',' view and select Gabor image:  ',...
'Style','text',...
'Tag','text43');


h81 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[81.8 6.84615384615385 22.8 1.15384615384615],...
'String',' save Gabor image(s):  ',...
'Style','text',...
'Tag','text44');


h82 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[121.8 7 22.8 1.15384615384615],...
'String',' save feature vectors:  ',...
'Style','text',...
'Tag','text45');


h83 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[115.8 33.4615384615385 21.8 1.15384615384615],...
'String',' Gabor filter transfers:  ',...
'Style','text',...
'Tag','text46');


h84 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'ListboxTop',0,...
'Position',[59.8 1.46153846153846 18.2 5.46153846153846],...
'String',{  '' },...
'Style','frame',...
'Tag','frame11');


h85 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'BackgroundColor',[1 1 1],...
'ListboxTop',0,...
'Position',[61.8 2.23076923076923 14.2 1.61538461538462],...
'String',' ',...
'Style','text',...
'Tag','saveroi_text');


h86 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'Callback','ut_getgabor(''saveroi_pusbutton_Callback'',gcbo,[],guidata(gcbo))',...
'ListboxTop',0,...
'Position',[61.8 4.38461538461539 12.2 1.76923076923077],...
'String','save',...
'Tag','saveroi_pusbutton');


h87 = uicontrol(...
'Parent',h1,...
'Units','characters',...
'HorizontalAlignment','left',...
'ListboxTop',0,...
'Position',[61.8 6.53846153846154 11.2 1.15384615384615],...
'String',' save ROI:  ',...
'Style','text',...
'Tag','text48');



hsingleton = h1;


% --- Handles default GUIDE GUI creation and callback dispatch
function varargout = gui_mainfcn(gui_State, varargin)


%   GUI_MAINFCN provides these command line APIs for dealing with GUIs
%
%      UT_GETGABOR, by itself, creates a new UT_GETGABOR or raises the existing
%      singleton*.
%
%      H = UT_GETGABOR returns the handle to a new UT_GETGABOR or the handle to
%      the existing singleton*.
%
%      UT_GETGABOR('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in UT_GETGABOR.M with the given input arguments.
%
%      UT_GETGABOR('Property','Value',...) creates a new UT_GETGABOR or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before untitled_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to untitled_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".

%   Copyright 1984-2002 The MathWorks, Inc.
%   $Revision: 1.5 $ $Date: 2003/07/17 18:28:28 $

gui_StateFields =  {'gui_Name'
                    'gui_Singleton'
                    'gui_OpeningFcn'
                    'gui_OutputFcn'
                    'gui_LayoutFcn'
                    'gui_Callback'};
gui_Mfile = '';
for i=1:length(gui_StateFields)
    if ~isfield(gui_State, gui_StateFields{i})
        error('Could not find field %s in the gui_State struct in GUI M-file %s', gui_StateFields{i}, gui_Mfile);        
    elseif isequal(gui_StateFields{i}, 'gui_Name')
        gui_Mfile = [getfield(gui_State, gui_StateFields{i}), '.m'];
    end
end

numargin = length(varargin);

if numargin == 0
    % UT_GETGABOR
    % create the GUI
    gui_Create = 1;
elseif numargin > 3 & ischar(varargin{1}) & ishandle(varargin{2})
    % UT_GETGABOR('CALLBACK',hObject,eventData,handles,...)
    gui_Create = 0;
else
    % UT_GETGABOR(...)
    % create the GUI and hand varargin to the openingfcn
    gui_Create = 1;
end

if gui_Create == 0
    varargin{1} = gui_State.gui_Callback;
    if nargout
        [varargout{1:nargout}] = feval(varargin{:});
    else
        feval(varargin{:});
    end
else
    if gui_State.gui_Singleton
        gui_SingletonOpt = 'reuse';
    else
        gui_SingletonOpt = 'new';
    end
    
    % Open fig file with stored settings.  Note: This executes all component
    % specific CreateFunctions with an empty HANDLES structure.
    
    % Do feval on layout code in m-file if it exists
    if ~isempty(gui_State.gui_LayoutFcn)
        gui_hFigure = feval(gui_State.gui_LayoutFcn, gui_SingletonOpt);
    else
        gui_hFigure = local_openfig(gui_State.gui_Name, gui_SingletonOpt);            
        % If the figure has InGUIInitialization it was not completely created
        % on the last pass.  Delete this handle and try again.
        if isappdata(gui_hFigure, 'InGUIInitialization')
            delete(gui_hFigure);
            gui_hFigure = local_openfig(gui_State.gui_Name, gui_SingletonOpt);            
        end
    end
    
    % Set flag to indicate starting GUI initialization
    setappdata(gui_hFigure,'InGUIInitialization',1);

    % Fetch GUIDE Application options
    gui_Options = getappdata(gui_hFigure,'GUIDEOptions');
    
    if ~isappdata(gui_hFigure,'GUIOnScreen')
        % Adjust background color
        if gui_Options.syscolorfig 
            set(gui_hFigure,'Color', get(0,'DefaultUicontrolBackgroundColor'));
        end

        % Generate HANDLES structure and store with GUIDATA
        guidata(gui_hFigure, guihandles(gui_hFigure));
    end
    
    % If user specified 'Visible','off' in p/v pairs, don't make the figure
    % visible.
    gui_MakeVisible = 1;
    for ind=1:2:length(varargin)
        if length(varargin) == ind
            break;
        end
        len1 = min(length('visible'),length(varargin{ind}));
        len2 = min(length('off'),length(varargin{ind+1}));
        if ischar(varargin{ind}) & ischar(varargin{ind+1}) & ...
                strncmpi(varargin{ind},'visible',len1) & len2 > 1
            if strncmpi(varargin{ind+1},'off',len2)
                gui_MakeVisible = 0;
            elseif strncmpi(varargin{ind+1},'on',len2)
                gui_MakeVisible = 1;
            end
        end
    end
    
    % Check for figure param value pairs
    for index=1:2:length(varargin)
        if length(varargin) == index
            break;
        end
        try, set(gui_hFigure, varargin{index}, varargin{index+1}), catch, break, end
    end

    % If handle visibility is set to 'callback', turn it on until finished
    % with OpeningFcn
    gui_HandleVisibility = get(gui_hFigure,'HandleVisibility');
    if strcmp(gui_HandleVisibility, 'callback')
        set(gui_hFigure,'HandleVisibility', 'on');
    end
    
    feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
    
    if ishandle(gui_hFigure)
        % Update handle visibility
        set(gui_hFigure,'HandleVisibility', gui_HandleVisibility);
        
        % Make figure visible
        if gui_MakeVisible
            set(gui_hFigure, 'Visible', 'on')
            if gui_Options.singleton 
                setappdata(gui_hFigure,'GUIOnScreen', 1);
            end
        end

        % Done with GUI initialization
        rmappdata(gui_hFigure,'InGUIInitialization');
    end
    
    % If handle visibility is set to 'callback', turn it on until finished with
    % OutputFcn
    if ishandle(gui_hFigure)
        gui_HandleVisibility = get(gui_hFigure,'HandleVisibility');
        if strcmp(gui_HandleVisibility, 'callback')
            set(gui_hFigure,'HandleVisibility', 'on');
        end
        gui_Handles = guidata(gui_hFigure);
    else
        gui_Handles = [];
    end
    
    if nargout
        [varargout{1:nargout}] = feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
    else
        feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
    end
    
    if ishandle(gui_hFigure)
        set(gui_hFigure,'HandleVisibility', gui_HandleVisibility);
    end
end    

function gui_hFigure = local_openfig(name, singleton)
try
    gui_hFigure = openfig(name, singleton, 'auto');
catch
    % OPENFIG did not accept 3rd input argument until R13,
    % toggle default figure visible to prevent the figure
    % from showing up too soon.
    gui_OldDefaultVisible = get(0,'defaultFigureVisible');
    set(0,'defaultFigureVisible','off');
    gui_hFigure = openfig(name, singleton);
    set(0,'defaultFigureVisible',gui_OldDefaultVisible);
end

