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

% Last Modified by GUIDE v2.5 23-Oct-2012 19:30:07

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = vyhodnoceni_formularu3_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;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Po spuštění
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;
global vstup ;

% ukončení
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on button press in konec_vyhodnoceni.
function konec_vyhodnoceni_Callback(hObject, eventdata, handles)
% hObject    handle to konec_vyhodnoceni (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close;
clc;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% otevření testovaného souboru
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on button press in otevreni.
function otevreni_Callback(hObject, eventdata, handles)
% hObject    handle to otevreni (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
clear all;
global vstup vstup_formular cesta cislo_nahled;

cislo_nahled = 1;
% otevře nabídku pro výběr testovaného obrazu
[vstup_formular,cesta] = uigetfile({'*.*';'*.jpg';'*.png';'*.bmp'},'File Selector','VSTUP\','MultiSelect', 'on');

% zobrazí testovaný obraz
if length(vstup_formular) > 1
    [a,b,c] = size(vstup_formular);

    prvni_formular = char(vstup_formular);
    % pokud je vybrán jen jeden obraz
    if  b > 4 &&  strcmp(char(vstup_formular(end-3)), '.') == 1
        % pokud je vybráno víc obrazů
    else
        prvni_formular = char(vstup_formular(1));
    end
    vstup = im2double(imread(strcat(cesta,prvni_formular)));
    
    [a,b,c] = size(vstup);
    if (c > 2)
        vstup = rgb2gray(vstup);
    end
    
    imshow(vstup, [],'InitialMagnification','fit');
    
    set(findobj('tag','vystupni_text'),'string', char(prvni_formular));
    set(findobj('tag','vystupni_text'),'BackgroundColor', 'white');
else
    set(findobj('tag','vystupni_text'),'string', 'Nebyl vožen formulář');
    set(findobj('tag','vystupni_text'),'BackgroundColor', 'red');
    clear all;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% náhled na formulář
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on button press in nahled.
function nahled_Callback(hObject, eventdata, handles)
% hObject    handle to nahled (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global vstup_formular vstup cesta cislo_nahled;

[a pocet_souboru] = size(vstup_formular);


if  isempty(vstup_formular) == 1
    set(handles.vystupni_text,'String','Nebyly vloženy formuláře!');
    set(findobj('tag','vystupni_text'),'BackgroundColor', 'red');
    return;
end


if  pocet_souboru > 4 &&  strcmp(char(vstup_formular(end-3)), '.') == 1
    vstup = im2double(imread(strcat(cesta,char(vstup_formular)))); 
    
    [a,b,c] = size(vstup);
    if (c > 2)
        vstup = rgb2gray(vstup);
    end
    % nastaveni figuru pres celou obrazovku
    h = figure(1);
    set(h,'Units','normalized','OuterPosition',[0 0 1 1]);
    imshow(vstup,'InitialMagnification','fit');
    title(vstup_formular);    
elseif cislo_nahled <= pocet_souboru
    vstup = im2double(imread(strcat(cesta,char(vstup_formular(cislo_nahled)))));
        
    [a,b,c] = size(vstup);
    if (c > 2)
        vstup = rgb2gray(vstup);
    end
    % nastaveni figuru pres celou obrazovku
    h = figure(1);
    set(h,'Units','normalized','OuterPosition',[0 0 1 1]);
    imshow(vstup,'InitialMagnification','fit');
    title(vstup_formular(cislo_nahled));    
else
    set(handles.vystupni_text,'String','Nebyl vložen soubor');
    set(findobj('tag','vystupni_text'),'BackgroundColor', 'red');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%testování formulářů
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on button press in test.
function test_Callback(hObject, eventdata, handles)
% hObject    handle to test (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global vstup vstup_formular cesta;
global navrhy_cislo navrhy_pismeno;
global navrhy_obraz_cislo navrhy_obraz_pismeno;

global koeficient;

if  isempty(vstup_formular) == 1 %|| vstup_formular == 0
    set(handles.vystupni_text,'String','Nebyly vloženy formuláře!');
    set(findobj('tag','vystupni_text'),'BackgroundColor', 'red');
    return;
end

set(handles.vystupni_text,'String','Testování začalo');
set(findobj('tag','vystupni_text'),'BackgroundColor', 'blue');

tic;
[a,pocet_souboru] = size(vstup_formular);
prubeh = 100/pocet_souboru;

% vypis
disp('0%');
disp('-------------------');

% načtení informačního souboru ze stejné složky jako obrazy
% hledá podle koncovky .mat, v jedné složce může být jeden
koren = cd(cesta);
slozka = dir(cesta);
delka_slozka = length(slozka);
mat = zeros(1,delka_slozka);
for i = 1: delka_slozka
    jmeno = slozka(i).name;
    if length(jmeno) > 4
        mat(i) = strcmp(jmeno(end-3:end),'.mat');
    end
end
% nahrání .mat
if sum(mat) == 1
    jmeno = slozka(find(mat,'1')).name;    
    load (jmeno);
    cd(koren);
else
    set(handles.vystupni_text,'String','V adresáři může být jen jeden soubor ve formátu .mat');   
    cd(koren);
    clc;
    return;
end

prvni_formular = char(vstup_formular);
if  pocet_souboru > 4 &&  strcmp(char(vstup_formular(end-3)), '.') == 1
    pocet_souboru = 1;
    prubeh = 100;
end




%% zapsání názvů a voleb
nazvy = cell(1,1);
volba = cell(1,1);
excel_pozice = 2;
nazvy(1) = {'Soubor'};

velikost = size(text_kolonky);
if  velikost(1) >1
    for i = 1:velikost(1)
    nazvy(excel_pozice) = {text_kolonky(i,:)};
        excel_pozice = excel_pozice + 1;
    end
elseif velikost(1) == 1
    nazvy(excel_pozice) = {text_kolonky(1,:)};
    excel_pozice = excel_pozice + 1;
end

velikost = size(text_cara);
if  velikost(1) >1
    for i = 1:velikost(1)
        nazvy(excel_pozice) = {text_cara(i,:)};
        excel_pozice = excel_pozice + 1;
    end
elseif velikost(1) == 1
    nazvy(excel_pozice) = {text_cara(1,:)};
    excel_pozice = excel_pozice + 1;
end

velikost = size(text_zaskrt);
volba_pozice = 1;
volba_radek = 1;
if  velikost(1) >1
    for i = 1:velikost(1)
        if text_zaskrt(i,1) == '+'
            nazvy(excel_pozice) = {text_zaskrt(i,2:end)};
            excel_pozice = excel_pozice + 1;
            volba_pozice = 1;
            volba_radek = volba_radek + 1;
        else
            volba(volba_radek,volba_pozice) = {text_zaskrt(i,:)};
            volba_pozice = volba_pozice + 1;
        end
    end
elseif velikost(1) == 0
    nazvy(excel_pozice) = {text_zaskrt(1,:)};
    excel_pozice = excel_pozice + 1;
end

velikost = size(text_osa);
if  velikost(1) >1
    for i = 1:velikost(1)
        nazvy(excel_pozice) = {text_osa(i,:)};
        excel_pozice = excel_pozice + 1;
    end
elseif velikost(1) == 1
    nazvy(excel_pozice) = {text_osa(1,:)};
end

for soubor = 1: pocet_souboru
    % každý formulář se musí načítat odznovu, jinak je problém s
    % přepisováním
    
    clear vstup obrazek_2_prah obrazek_3_znacky obrazek_3_znacky_orez ...
        otoceny_obrazek obrazek_rotujici obrazek_rotujici_orez ...
        otoceny_obrazek otoceny_obrazek_bily obrazek_4_znacka_prah ...
        kontrola_pretoceni_kriz kontrola_pretoceni obrazek_3_mezi_znacky
    
    % pokud je vybrán jen jeden obraz
    if pocet_souboru == 1
        vstup = im2double(imread(strcat(cesta,char(prvni_formular))));
        [a,b,c] = size(vstup);
        if (c > 2)
            vstup = rgb2gray(vstup);
        end
    % pokud je vybráno víc obrazů
    else
        vstup = im2double(imread(strcat(cesta,char(vstup_formular(soubor)))));
        [a,b,c] = size(vstup);
        if (c > 2)
            vstup = rgb2gray(vstup);
        end
    end
    obrazek_1_vstup = vstup;
        
    %% prahování pro ořezání
    mez = 0.9;
    obrazek_2_prah = obrazek_1_vstup;
    obrazek_2_prah(obrazek_1_vstup > mez) = 0;
    obrazek_2_prah(obrazek_1_vstup <= mez) = 1;
    
    obrazek_2_prah = logical(obrazek_2_prah);
   %% tady bude hledání značky pootočení
    % nalezení dlouhé čáry
    aa = 3;
    bb = 15;
    detektor = logical(ones(aa,bb));

    [a,b] = size(obrazek_2_prah);
    obrazek_3_znacky = zeros(a,b);

    i = 1;
    k = 1;
    while i <= a-aa
        while k <= b-bb
            if obrazek_2_prah(i:i+aa-1,k:k+bb-1) == detektor;
                obrazek_3_znacky(i:i+aa-1,k:k+bb-1) = detektor;
            else
                obrazek_3_znacky(i:i+aa-1,k:k+bb-1) = 0;
            end
        k = k + bb;
        end  
        i = i + aa;
        k = 1;
    end
    
    cara_soucet = sum(obrazek_3_znacky,2);
    [max_cara max_pozice] = max(cara_soucet);
    cara = obrazek_3_znacky(max_pozice-20:max_pozice+20,:);
    
    [nic,poziceH_cara] = funkce_prumet(cara,'H');
    cara_orez = cara(:,poziceH_cara(2)+20:poziceH_cara(end-1)-20);

    %% otáčení čáry a zjištění správného úhlu
    uhel = -5;
    uhel_konec = - uhel;
    soucet_max = 0;
    sirka_max = inf;
    pozice_uhel = 0;
    while uhel <= uhel_konec
        
        obrazek_rotujici = imrotate(cara_orez,uhel);
        [nic,sirky] = funkce_prumet(obrazek_rotujici,'V');

        sirka( length(sirky) <= 4) = sirky(end-1)-sirky(2);
        if length(sirky) == 3
             if (sirky(end) - sirky(2)) > (sirky(2) - sirky(1))
                 sirka = sirky(end) - sirky(2);
             else
                 sirka = sirky(2) - sirky(1);
             end
        end
        
        if sirka < sirka_max
%             vysledny uhel
            pozice_uhel = uhel;
            sirka_max = sirka;            
        end
        if sirka_max < 5
            uhel = uhel_konec;
        end
        
        uhel = uhel + 0.2;
    end
        
    %     1 - a 1 - je tam protže při otočení se místa navíc vybarvují černě
    otoceny_obrazek_bily = 1-imrotate(1-obrazek_1_vstup,pozice_uhel,'bicubic');
    otoceny_obrazek = 1 - otoceny_obrazek_bily;
    
%     pozice_uhel
    mez = graythresh(otoceny_obrazek);
    obrazek_4_znacka_prah = otoceny_obrazek;
    obrazek_4_znacka_prah(otoceny_obrazek > mez) = 1;
    obrazek_4_znacka_prah(otoceny_obrazek <= mez) = 0;
    
   %% tady bude hledání značky pootočení znovu pro otočené
    % svislé hrany
    aa = 12;
    bb = 4;
    detektor = ones(aa,bb);
    [a,b] = size(obrazek_4_znacka_prah);
    obrazek_3_znacky1 = zeros(a,b);
    i = 1;
    k = 1;
    while i <= a-aa
        while k <= b-bb
            if obrazek_4_znacka_prah(i:i+aa-1,k:k+bb-1) == detektor;
                obrazek_3_znacky1(i:i+aa-1,k:k+bb-1) = detektor;
            else
                obrazek_3_znacky1(i:i+aa-1,k:k+bb-1) = 0;
            end
        k = k + bb;
        end  
        i = i + aa;
        k = 1;
    end
    
%     vodorovné hrany
    aa = 4;
    bb = 12;
    detektor = ones(aa,bb);
    [a,b] = size(obrazek_4_znacka_prah);
    obrazek_3_znacky2 = zeros(a,b);
    i = 1;
    k = 1;
    while i <= a-aa
        while k <= b-bb
            if obrazek_4_znacka_prah(i:i+aa-1,k:k+bb-1) == detektor;
                obrazek_3_znacky2(i:i+aa-1,k:k+bb-1) = detektor;
            else
                obrazek_3_znacky2(i:i+aa-1,k:k+bb-1) = 0;
            end
        k = k + bb;
        end  
        i = i + aa;
        k = 1;
    end
    % soucet
    obrazek_3_znacky = obrazek_3_znacky1 + obrazek_3_znacky2;
    
    %% oříznutí tak aby vrchní značka byla 0,0 a spodní značka konec 
    [nic,poziceH_obrazek_3_znacky] = funkce_prumet(obrazek_3_znacky,'H');
    [nic,poziceV_obrazek_3_znacky] = funkce_prumet(obrazek_3_znacky,'V');
    obrazek_3_mezi_znacky = otoceny_obrazek_bily(poziceV_obrazek_3_znacky(2):...
                                                 poziceV_obrazek_3_znacky(end-1),...
                                                 poziceH_obrazek_3_znacky(2):...
                                                 poziceH_obrazek_3_znacky(end-1));
    obrazek_3_mezi_znacky_oprava{soubor} = obrazek_3_mezi_znacky;

    %% Přepočet na originální velikost
    rozmer_a = celkova_velikost(1);
    rozmer_b = celkova_velikost(2);
    
    [celk_a celk_b] = size(obrazek_3_mezi_znacky);
    
    uhlopricka_rozmer = sqrt(rozmer_a^2 + rozmer_b^2);
    
    uhlopricka = sqrt(celk_a^2 + celk_b^2);
    koeficient = uhlopricka/uhlopricka_rozmer;   

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% tady začíná rozpoznání znaků v kolonkách, pokud jsou kolonky spojené
    % vždy vybere 4 souřadnice a provede výpočet, pokračuje dokud má
    % souřadnice
    % if  podmínka která zjištˇuje jestli jsou souřadnice
    stop = 0;
    pozice = 0;
    
    % první buňka s názvem souboru v excelu
    if prubeh == 100
        radek(1) = cellstr(vstup_formular);
    else
        radek(1) = cellstr(vstup_formular(soubor));
    end
    
    kolonky = 2;
    while stop == 0
        if length(souradnice_kolonky) > pozice
            
            x1 = round(souradnice_kolonky(1 + pozice)*koeficient);
            x2 = round(souradnice_kolonky(2 + pozice)*koeficient);
            y1 = round(souradnice_kolonky(3 + pozice)*koeficient);
            y2 = round(souradnice_kolonky(4 + pozice)*koeficient);
            pozice = 4 + pozice;
    
            obrazek_kolonky_1 = obrazek_3_mezi_znacky(x1:x2,y1:y2);
            
            % funkce vyhodí řetězec znaků a pro každý návrh na jiné znaky
            % pokud rozpoznání není jednoznačné, soubory jsou psané pod sebe
            [znak_pismeno navrhy obraz_pismeno] = funkce_znaky_kolonky(obrazek_kolonky_1);
            navrhy_pismeno{soubor,kolonky-1} =  navrhy;
            navrhy_obraz_pismeno{soubor,kolonky-1} =  obraz_pismeno;
            
            % přepis do pole
            if strcmp(znak_pismeno, '*') == 1
                radek(kolonky) = cellstr('nevyplněno');
            else
                radek(kolonky) = cellstr(znak_pismeno);
            end
            
            kolonky = kolonky + 1;
        else
            stop = 1;
        end
    end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% rozpoznání čísel napsaných na řádku        
    stop = 0;
    pozice = 0;
    kolonky2 = 1;
    while stop == 0
        if length(souradnice_cara) > pozice
            
            x1 = round(souradnice_cara(1 + pozice)*koeficient);
            x2 = round(souradnice_cara(2 + pozice)*koeficient);
            y1 = round(souradnice_cara(3 + pozice)*koeficient);
            y2 = round(souradnice_cara(4 + pozice)*koeficient);
            pozice = 4 + pozice;
            
            obrazek_cara_1 = obrazek_3_mezi_znacky(x1:x2,y1:y2);
            % funkce
            [znak_cislo navrhy obraz_cislo] = funkce_znaky_radek(obrazek_cara_1);
            navrhy_cislo{soubor,kolonky2} =  navrhy;
            navrhy_obraz_cislo{soubor,kolonky2} =  obraz_cislo;

            % zapis
            % pokud to nebude obsahovat tak napíše nevyplněno
            if strcmp(znak_cislo, '*') == 1
                radek(kolonky) = cellstr('nevyplněno');
            else
                radek(kolonky) = cellstr(znak_cislo);
            end    
            kolonky2 = kolonky2 + 1;
            kolonky = kolonky + 1;
        else
            stop = 1;
        end
    end

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% rozpoznání zaškrtlých políček   
    clear obrazek_zaskrt_1 % smazání z předchozího testu
    pocet_radku_volba = size(volba);
    for radek_volba = 1:pocet_radku_volba(1)
        % volba (potřebuju zjistit délku jednotlyvých voleb, z toho pak budu brát souřadnice)
        volba_vstup1 = volba(radek_volba,:);
        max_delka = length(volba_vstup1);
        volba_cell = max_delka;
        
        for i =  1: max_delka;
            a = isempty(char(volba_vstup1{i}));
            if a == 1 
                volba_cell = i-1;
                break;
            end
        end
        volba_vstup = volba_vstup1(1,1:volba_cell);
        % cyklus projíždí jedno políčko po druhém a ukládá do 3D matice
        for pozice = 0:4:(volba_cell-1)*4
            if pozice == 0
                x1 = round(souradnice_zaskrt(1 + pozice)*koeficient);
                x2 = round(souradnice_zaskrt(2 + pozice)*koeficient);
                y1 = round(souradnice_zaskrt(3 + pozice)*koeficient);
                y2 = round(souradnice_zaskrt(4 + pozice)*koeficient);
                xxx = x2 - x1;
                yyy = y2 - y1;
            else
                x1 = round(souradnice_zaskrt(1 + pozice)*koeficient);
                x2 = x1 + xxx;
                y1 = round(souradnice_zaskrt(3 + pozice)*koeficient);
                y2 = y1 + yyy;
            end
            obrazek_zaskrt_1(:,:,1+pozice/4) = obrazek_3_mezi_znacky(x1:x2,y1:y2);
        end
        
        % funkce
        vysledna_volba = funkce_skrtaci_pole(obrazek_zaskrt_1,volba_vstup);
        
        % zapis
        if strcmp(vysledna_volba, '*') == 1
            radek(kolonky) = cellstr('nevyplněno');
        else
            radek(kolonky) = cellstr(vysledna_volba);
        end
        
        kolonky = kolonky + 1;
    end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% určení počtů procent z osy
    stop = 0;
    pozice = 0;
    while stop == 0
        if length(souradnice_osa) > pozice
            
            x1 = round(souradnice_osa(1 + pozice)*koeficient);
            x2 = round(souradnice_osa(2 + pozice)*koeficient);
            y1 = round(souradnice_osa(3 + pozice)*koeficient);
            y2 = round(souradnice_osa(4 + pozice)*koeficient);
            pozice = 4 + pozice;
            
            obrazek_osa = obrazek_3_mezi_znacky(x1:x2,y1:y2);
            % funkce
            procenta = funkce_procenta_z_osy(obrazek_osa); 
            % zapis
            if strcmp(procenta, '*') == 1
                radek(kolonky) = cellstr('nevyplněno');
            else
                radek(kolonky) = cellstr(procenta);
            end
            
            kolonky = kolonky + 1;
        else
            stop = 1;
        end
    end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% vypis průběhu
    vypis = soubor * prubeh;
    disp(strcat(num2str(round(vypis)),'%'));
    disp(strcat(radek(1),' hotovo'));
    disp('-------------------');
    set(findobj('tag','vystupni_text'),'string', strcat(num2str(vypis),'%'));

    xlswrite('Vysledky.xls', radek, 1, strcat('A',num2str(soubor+1)));
           
end
save('data\obrazy.mat', 'obrazek_3_mezi_znacky_oprava');

xlswrite('Vysledky.xls', nazvy, 1, 'A1')

set(findobj('tag','vystupni_text'),'string', 'konec výpočtu');
set(findobj('tag','vystupni_text'),'BackgroundColor', 'white');

disp('vysledky jsou zapsany v excelu');
k = toc;
disp(strcat('Celková doba výpočtu: ',num2str(k)));

save('data\navrh.mat','navrhy_cislo', 'navrhy_pismeno', 'navrhy_obraz_cislo', 'navrhy_obraz_pismeno');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% zobrazení excelu
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on button press in vyber.
function vyber_Callback(hObject, eventdata, handles)
% hObject    handle to vyber (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% global vstup orez_vysledek;

winopen('Vysledky.xls');
set(handles.vystupni_text,'String','Excel otevřen');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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


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

% vyčistění comand window spuštění oprav
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on button press in cistic.
function cistic_Callback(hObject, eventdata, handles)
% hObject    handle to cistic (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
clc;
global  koeficient ;

if  isempty(koeficient) == 1 
    set(handles.vystupni_text,'String','Nebyly testovány formuláře!');
    set(findobj('tag','vystupni_text'),'BackgroundColor', 'red');
    return;
end

oprava5;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


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

global vstup vstup_formular cesta cislo_nahled;

% dekrementace
cislo_nahled = cislo_nahled - 1;

% počet souborů
[a,pocet_souboru] = size(vstup_formular);

% pokud je vybrán jen jeden obraz
if  pocet_souboru > 4 &&  strcmp(char(vstup_formular(end-3)), '.') == 1
    set(findobj('tag','vystupni_text'),'string', 'Byl vybrán jen jeden soubor');
    cislo_nahled = cislo_nahled + 1;
elseif pocet_souboru > 1
    % pokud je vybráno více obrazů
    if cislo_nahled < 1
        set(findobj('tag','vystupni_text'),'string', 'Toto je první soubor');
        cislo_nahled = cislo_nahled + 1;
    elseif cislo_nahled == pocet_souboru
        vstup = im2double(imread(strcat(cesta,char(vstup_formular(pocet_souboru)))));
        [a,b,c] = size(vstup);
        if (c > 2)
            vstup = rgb2gray(vstup);
        end
        imshow(vstup,'InitialMagnification','fit');
        set(findobj('tag','vystupni_text'),'string', char(vstup_formular(cislo_nahled)));
    else
        vstup = im2double(imread(strcat(cesta,char(vstup_formular(cislo_nahled)))));
        [a,b,c] = size(vstup);
        if (c > 2)
            vstup = rgb2gray(vstup);
        end
        imshow(vstup,'InitialMagnification','fit');
        set(findobj('tag','vystupni_text'),'string', char(vstup_formular(cislo_nahled)));
    end
else
    set(findobj('tag','vystupni_text'),'string', 'Nebyl vložen soubor');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

global vstup vstup_formular cesta cislo_nahled;

% inkrementace
cislo_nahled = cislo_nahled + 1;

% počet souborů
[a,pocet_souboru] = size(vstup_formular);

% pokud je vybrán jen jeden obraz
if  pocet_souboru > 4 &&  strcmp(char(vstup_formular(end-3)), '.') == 1
    set(findobj('tag','vystupni_text'),'string', 'Byl vybrán jen jeden soubor');
    cislo_nahled = cislo_nahled - 1;
elseif pocet_souboru > 1
    % pokud je vybráno více obrazů
    if cislo_nahled > pocet_souboru
        set(findobj('tag','vystupni_text'),'string', 'Toto je poslední soubor');
        cislo_nahled = cislo_nahled - 1;
    elseif cislo_nahled == pocet_souboru
        vstup = im2double(imread(strcat(cesta,char(vstup_formular(pocet_souboru))))); 
        [a,b,c] = size(vstup);
        if (c > 2)
            vstup = rgb2gray(vstup);
        end
        imshow(vstup,'InitialMagnification','fit');
        set(findobj('tag','info'),'string', char(vstup_formular(cislo_nahled)));
    else
        vstup = im2double(imread(strcat(cesta,char(vstup_formular(cislo_nahled)))));
        [a,b,c] = size(vstup);
        if (c > 2)
            vstup = rgb2gray(vstup);
        end
        imshow(vstup,'InitialMagnification','fit');
        set(findobj('tag','vystupni_text'),'string', char(vstup_formular(cislo_nahled)));
    end
else
    set(findobj('tag','vystupni_text'),'string', 'Nebyl vložen soubor');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
