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

% Last Modified by GUIDE v2.5 10-May-2011 12:23:25

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Subgui_davkove_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 excel_pushbutton_Callback(hObject, eventdata, handles)
[soubor,cesta,FilterIndex] = uigetfile( '*.xls;');

set(handles.excel_cesta_edit,'String',[cesta '' soubor]);
guidata(hObject, handles);


function vysledky_pushbutton_Callback(hObject, eventdata, handles)
cesta_v = uigetdir();
set(handles.vysledky_cesta_edit,'String',cesta_v);
guidata(hObject, handles);

function vysledky_cesta_edit_Callback(hObject, eventdata, handles)
guidata(hObject, handles);

function vysledky_cesta_edit_CreateFcn(hObject, eventdata, handles)

% 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 excel_cesta_edit_Callback(hObject, eventdata, handles)
guidata(hObject, handles)

function excel_cesta_edit_CreateFcn(hObject, eventdata, handles)

% 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 bifurkace_pushbutton_Callback(hObject, eventdata, handles)
cesta_vysl = get(handles.vysledky_cesta_edit,'String');
if cesta_vysl(end) ~= '\'
    cesta_vysl = [cesta_vysl '' '\'];
end

[num txt] = xlsread(get(handles.excel_cesta_edit,'String'));
rozsah = size(txt);

vse = 0;
for i = 1:rozsah(2)
    for j = 2:rozsah(1)
        if isempty(cell2mat(txt(j,i)))
            continue
        else
        vse = vse + 1;          

        end
    end
end
count = 0;

souradnice_vse = [];
k = 1;
souradnice_jmena = [];
souradnice_jmena = string(souradnice_jmena);

for i = 1:rozsah(2)
    for j = 1:rozsah(1)
        if isempty(cell2mat(txt(j,i)))
            continue
        else
                   

            soubor = cell2mat(txt(j,i));
            
            
            pozice=find(soubor=='\');
            nazev=soubor(pozice(end)+1:end);
            clear pozice
            
            
            count = count+1;

            progressbar(count/vse) % Update figure
            a = imread(soubor);

            [obr,obr_vysl_g,souradnice] = komplexni(a);
            
            imwrite(obr,[cesta_vysl '' nazev]);
            
            souradnice_vse (k:k+1,1:length(souradnice)) = souradnice;

            souradnice_jmena(k,1:length(nazev(1:end-4))) = nazev(1:end-4);
            souradnice_jmena(k+1,1:length(nazev(1:end-4))) = nazev(1:end-4);           
            
            
            
            k = k+2;
        end
    end
    uloz_sourad = [cesta_vysl '' souradnice_jmena(1,:) '' '_sourad.mat'];
    uloz_popis = [cesta_vysl '' souradnice_jmena(1,:) '' '_sourad_popis.mat'];
    save (uloz_sourad, 'souradnice_vse')
    save (uloz_popis, 'souradnice_jmena')
        
    souradnice_vse = [];
    souradnice_jmena = [];
    souradnice_jmena = string(souradnice_jmena);
    k = 1;
end

guidata(hObject, handles);


function napoveda_pushbutton_Callback(hObject, eventdata, handles)
text = {'Program slouží pro dávkové zpracování dat ze souboru excel.'; ...
        'V Excelu musí být v každé buňce adresa snímku, s tím že';...
        'v každém sloupci jsou různé snímky pro totéž oko. Tedy např:';'';...
        '   C:\Novák_levé1.jpg   C:\Novák_pravé1.jpg  C:\Pátý_levé1.jpg';...
        '   C:\Novák_levé2.jpg   C:\Novák_pravé2.jpg  C:\Pátý_levé2.jpg'};
msgbox(text,'Nápověda','help')
guidata(hObject, handles);


function registrace_faz_pushbutton_Callback(hObject, eventdata, handles)
cesta_vysl = get(handles.vysledky_cesta_edit,'String');
if cesta_vysl(end) ~= '\'
    cesta_vysl = [cesta_vysl '' '\'];
end

[num txt] = xlsread(get(handles.excel_cesta_edit,'String'));
rozsah = size(txt);

vse = 0;
for i = 1:rozsah(2)
    for j = 2:rozsah(1)
        if isempty(cell2mat(txt(j,i)))
            continue
        else
        vse = vse + 1;          

        end
    end
end
count = 0;

for i = 1:rozsah(2)
    for j = 2:rozsah(1)
        if isempty(cell2mat(txt(j,i)))
            continue
        else
            progressbar(count/vse) % Update figure  
            soubor1 = cell2mat(txt(1,i));
            soubor2 = cell2mat(txt(j,i));
            
            pozice=find(soubor2=='\');
            nazev=soubor2(pozice(end)+1:end);
            clear pozice
 
            oko1 = imread(soubor1);
            vel=size(oko1);
            if vel(1) > 1000 && vel(2) > 1500
                oko1 = imresize(oko1,[1000 1500]);
                clear vel
            end 

            oko2 = imread(soubor2);
            vel=size(oko2);
            if vel(1) > 1000 && vel(2) > 1500
                oko2 = imresize(oko2,[1000 1500]);
                clear vel
            end 

            [snimek1, snimek2] = fazova_korelace (oko1, oko2, 0, 3);
            sach = sachovnice(snimek1,snimek2);     
            
            imwrite(snimek2,[cesta_vysl '' nazev]);
            imwrite(uint8(sach),[cesta_vysl '' nazev(1:end-4) '' '_sach.jpg']);
            
            count = count+1;
            progressbar(count/vse) % Update figure            
            
        end
    end
end
guidata(hObject, handles);


function registrace_kvadr_pushbutton_Callback(hObject, eventdata, handles)
cesta_vysl = get(handles.vysledky_cesta_edit,'String');
if cesta_vysl(end) ~= '\'
    cesta_vysl = [cesta_vysl '' '\'];
end

[num txt] = xlsread(get(handles.excel_cesta_edit,'String'));
rozsah = size(txt);

vse = 0;
for i = 1:rozsah(2)
    for j = 2:rozsah(1)
        if isempty(cell2mat(txt(j,i)))
            continue
        else
        vse = vse + 1;          

        end
    end
end
count = 0;

for i = 1:rozsah(2)
    for j = 2:rozsah(1)
        if isempty(cell2mat(txt(j,i)))
            continue
        else
            progressbar(count/vse) % Update figure
            
            soubor1 = cell2mat(txt(1,i));
            soubor2 = cell2mat(txt(j,i));
            
            pozice=find(soubor2=='\');
            nazev=soubor2(pozice(end)+1:end);
            clear pozice
            
            oko1 = imread(soubor1);
            vel=size(oko1);
            if vel(1) > 1000 && vel(2) > 1500
                oko1 = imresize(oko1,[1000 1500]);
                clear vel
            end 

            oko2 = imread(soubor2);
            vel=size(oko2);
            if vel(1) > 1000 && vel(2) > 1500
                oko2 = imresize(oko2,[1000 1500]);
                clear vel
            end 

            [obr,obr_vysl_g,souradnice] = komplexni(oko1);
            
            oko2_kvadr=slicovani(oko1,oko2,souradnice);
            sach = sachovnice(oko1,oko2_kvadr);     
            
            imwrite(oko2_kvadr,[cesta_vysl '' nazev]);
            imwrite(uint8(sach),[cesta_vysl '' nazev(1:end-4) '' '_sach.jpg']);
            
            count = count+1;
            progressbar(count/vse) % Update figure            
            
        end
    end
end
guidata(hObject, handles);
