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

% Last Modified by GUIDE v2.5 08-May-2015 14:38:21

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = gui_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 TLvyber_obrazu.
function TLvyber_obrazu_Callback(hObject, eventdata, handles)
%Definice globalnich promennych
global obr_vyrez;
global obr_bilocerny;
global obr;
% Po kliknuti ziska cestu obrazu
[nazev,cesta] = uigetfile('*.*','Vyberte obraz');
soubor=fullfile(cesta,nazev);
% Vypsani nazvu obrazu
set(handles.obraz_nazev,'String',nazev);
%Nacteni obrazu
[obr,map]=imread(soubor);
if ~isempty(map)
    obr = ind2rgb(obr,map);
end
% Normalizace a prevedeni do sedotonoveho obrazu
obr=im2double(obr);
obr = rgb2gray(obr(:,:,1:3));
%Prevod na cerne prouzky na bilem pozadi
if mean2(obr)<0.5
     obr_bilocerny=1-obr;
else
    obr_bilocerny=obr;         
end
% Zjisteni varianty orezu obrazu
contents = get(handles.orez_vyber,'String'); 
vyber = contents{get(handles.orez_vyber,'Value')};
% Zjisteni, jestli je zapnuté predzpracovani
predzpr_hodnota = get(handles.predzpracovani, 'Value');
switch predzpr_hodnota
    case 1
        % Volani fce orezani obrazu
        obr_vyrez=orez(obr_bilocerny,vyber);
    case 0
        obr_vyrez=obr_bilocerny;
end
% Pridani bilych okraju pro zlepseni detekce linii
[~,sirka]=size(obr_vyrez);
a=ones(10,sirka);
obr_vyrez=[a; obr_vyrez; a];
% Vykresleni do grafu
axes(handles.vstupni_obraz);
imshow(obr);

% hObject    handle to TLvyber_obrazu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



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


% --- Executes during object creation, after setting all properties.
function vstup_pocet_linii_CreateFcn(hObject, eventdata, handles)
% hObject    handle to vstup_pocet_linii (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 orez_vyber.
function orez_vyber_Callback(hObject, eventdata, handles)
% hObject    handle to orez_vyber (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Globalni promenne
global obr_vyrez;
global obr_bilocerny;
global obr;
% Zjisteni varianty orezu obrazu
contents = get(handles.orez_vyber,'String'); 
vyber = contents{get(handles.orez_vyber,'Value')};
% Zjisteni, jestli je zapnuté predzpracovani
predzpr_hodnota = get(handles.predzpracovani, 'Value');
switch predzpr_hodnota
    case 1
        % Volani fce orezani obrazu
        obr_vyrez=orez(obr_bilocerny,vyber);
    case 0
        obr_vyrez=obr_bilocerny;
end
% Vykresleni do grafu
axes(handles.vstupni_obraz);
imshow(obr);

% Hints: contents = cellstr(get(hObject,'String')) returns orez_vyber contents as cell array
%        contents{get(hObject,'Value')} returns selected item from orez_vyber


% --- Executes during object creation, after setting all properties.
function orez_vyber_CreateFcn(hObject, eventdata, handles)
% hObject    handle to orez_vyber (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


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over orez_vyber.
function orez_vyber_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to orez_vyber (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in klasifikace.
function klasifikace_Callback(hObject, eventdata, handles)
% Nacteni globalni promenne
global obr_vyrez;
% Zjisteni, jestli je zapnuté predzpracovani
predzpr_hodnota = get(handles.predzpracovani, 'Value');
switch predzpr_hodnota
    case 1
        % Predzpracovani obrazu
        obr_zprac=predzpracovani(obr_vyrez);
    case 0
        obr_zprac=obr_vyrez;
end
% Zjisteni poctu linii
contents = get(handles.vstup_pocet_linii,'String'); 
pocet_linii=str2num(contents);
% Detekce linii
polohy_linie=detekce_linii(obr_zprac,pocet_linii);
% Prevedeni linii na median
obr_med=prevedeni_median(obr_zprac,pocet_linii,polohy_linie);
% Vykresleni predzpracovaneho obrazu
axes(handles.predzpracovany_obraz);
imshow(obr_med);
% Zjisteni vyberu metody distanci
contents = get(handles.vyber_distance,'String'); 
metoda_distance = contents{get(handles.vyber_distance,'Value')};
% Zjisteni vyberu metody shlukovani
contents = get(handles.vyber_shluky,'String'); 
metoda_shluk = contents{get(handles.vyber_shluky,'Value')};
% Zjisteni vyberu priznaku a vybrani prislusne metody
contents = get(handles.vyber_priznaky,'String'); 
vyber_priznaku = contents{get(handles.vyber_priznaky,'Value')};
% Vypocet prislusne metody
switch vyber_priznaku
    case 'Median linii jako 1D signal'
        % Vypocet metody
        [shluk,spravnost,prah]=priznak_median(obr_med,pocet_linii,polohy_linie, metoda_distance, metoda_shluk);
        % Vykresleni
        axes(handles.vystupni_obraz);
        dendrogram(shluk,20,'colorthreshold',prah);
        xlabel('Cisla linii') 
        ylabel('Vzdalenosti') 
        %Vypsani hodnoty spravnosti klasifikace
        spravnost=num2str(spravnost);
        set(handles.spravnost_zobrazeni,'String',spravnost);

    case 'Parametry prouzku'
        % Detekce prouzku a jejich parametru
        [polohy_prouzky,prouzky_zacatky,prouzky_konce]=detekce_prouzku(polohy_linie,obr_med);
        % Vypocet metody
        [shluk,spravnost,prah]=priznak_prouzky(obr_vyrez, polohy_prouzky,prouzky_zacatky,prouzky_konce,polohy_linie,metoda_distance, metoda_shluk);
        % Vykresleni
        axes(handles.vystupni_obraz);
        dendrogram(shluk,20,'colorthreshold',prah);
        xlabel('Cisla linii') 
        ylabel('Vzdalenosti') 
        %Vypsani hodnoty spravnosti klasifikace
        spravnost=num2str(spravnost);
        set(handles.spravnost_zobrazeni,'String',spravnost);
       
end



% hObject    handle to klasifikace (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on selection change in vyber_distance.
function vyber_distance_Callback(hObject, eventdata, handles)
% hObject    handle to vyber_distance (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns vyber_distance contents as cell array
%        contents{get(hObject,'Value')} returns selected item from vyber_distance


% --- Executes during object creation, after setting all properties.
function vyber_distance_CreateFcn(hObject, eventdata, handles)
% hObject    handle to vyber_distance (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


% --- Executes on selection change in vyber_shluky.
function vyber_shluky_Callback(hObject, eventdata, handles)
% hObject    handle to vyber_shluky (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns vyber_shluky contents as cell array
%        contents{get(hObject,'Value')} returns selected item from vyber_shluky


% --- Executes during object creation, after setting all properties.
function vyber_shluky_CreateFcn(hObject, eventdata, handles)
% hObject    handle to vyber_shluky (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


% --- Executes on selection change in vyber_priznaky.
function vyber_priznaky_Callback(hObject, eventdata, handles)
% hObject    handle to vyber_priznaky (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns vyber_priznaky contents as cell array
%        contents{get(hObject,'Value')} returns selected item from vyber_priznaky


% --- Executes during object creation, after setting all properties.
function vyber_priznaky_CreateFcn(hObject, eventdata, handles)
% hObject    handle to vyber_priznaky (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


% --- Executes on button press in vsechny_metody.
function vsechny_metody_Callback(hObject, eventdata, handles)
% Nacteni globalni promenne
global obr_vyrez;
% Zjisteni, jestli je zapnuté predzpracovani
predzpr_hodnota = get(handles.predzpracovani, 'Value');
switch predzpr_hodnota
    case 1
        % Predzpracovani obrazu
        obr_zprac=predzpracovani(obr_vyrez);
    case 0
        % Bez predzpracovani
        obr_zprac=obr_vyrez;
end
% Zjisteni poctu linii
contents = get(handles.vstup_pocet_linii,'String'); 
pocet_linii=str2num(contents);
% Detekce linii
polohy_linie=detekce_linii(obr_zprac,pocet_linii);
% Prevedeni linii na median
obr_med=prevedeni_median(obr_zprac,pocet_linii,polohy_linie);
% Vykresleni predzpracovaneho obrazu
axes(handles.predzpracovany_obraz);
imshow(obr_med);
% Zjisteni vyberu priznaku a vybrani prislusne metody
contents = get(handles.vyber_priznaky,'String'); 
vyber_priznaku = contents{get(handles.vyber_priznaky,'Value')};
% Vypocet prislusne metody
switch vyber_priznaku
    case 'Median linii jako 1D signal'
        % Vypocet metody
        [vzdalenosti,shluky,spravnost]=priznak_median_vsechny(obr_med,pocet_linii,polohy_linie);
        %Zjisteni nejlepsi hodnoty
        [nejlepsi_hodnota poloha] = min(spravnost(:));
        [x,y] = ind2sub(size(spravnost),poloha);
        nejlepsi_hodnota=[nejlepsi_hodnota vzdalenosti(x) shluky(y)];
        %Vypsani hodnoty a metod spravnosti klasifikace
        set(handles.spravnost_zobrazeni,'String',nejlepsi_hodnota(1));
        set(handles.vypis_vzdalenosti,'String',nejlepsi_hodnota(2));
        set(handles.vypis_shluk,'String',nejlepsi_hodnota(3));
        % Vykresleni
        axes(handles.vystupni_obraz);
        bar3(spravnost);
        xlabel('Metody vypoctu shluku') 
        ylabel('Metody vypoctu vzdalenosti')
        zlabel('Hodnota spravnosti')
        h=rotate3d;
        set(h,'Enable','on');
        set(gca,'YTickLabel',vzdalenosti,'XTickLabel',shluky);
    case 'Parametry prouzku'
        % Detekce prouzku a jejich parametru
        [polohy_prouzky,prouzky_zacatky,prouzky_konce]=detekce_prouzku(polohy_linie,obr_med);
        % Vypocet metody
        [vzdalenosti,shluky,spravnost]=priznak_prouzky_vsechny(obr_vyrez, polohy_prouzky,prouzky_zacatky,prouzky_konce,polohy_linie);
        %Zjisteni nejlepsi hodnoty
        [nejlepsi_hodnota poloha] = min(spravnost(:));
        [x,y] = ind2sub(size(spravnost),poloha);
        nejlepsi_hodnota=[nejlepsi_hodnota vzdalenosti(x) shluky(y)];
        %Vypsani hodnoty a metod spravnosti klasifikace
        set(handles.spravnost_zobrazeni,'String',nejlepsi_hodnota(1));
        set(handles.vypis_vzdalenosti,'String',nejlepsi_hodnota(2));
        set(handles.vypis_shluk,'String',nejlepsi_hodnota(3));
        % Vykresleni
        axes(handles.vystupni_obraz);
        bar3(spravnost);
        xlabel('Metody vypoctu shluku') 
        ylabel('Metody vypoctu vzdalenosti')
        zlabel('Hodnota spravnosti')
        h=rotate3d;
        set(h,'Enable','on');
        set(gca,'YTickLabel',vzdalenosti,'XTickLabel',shluky);
end
% hObject    handle to vsechny_metody (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


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