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

% Last Modified by GUIDE v2.5 07-Apr-2014 19:05:13

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = detekce_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% po stisknuti tlačítka je uživateli umožněno načíst snímky
delete(['*.tif']) % smazani vytvořených snímků
delete(['*.mhd'])
delete(['*.raw'])
set(handles.edit2,'string',' ') % vymazíní informací o teplotě
set(handles.edit4,'string',' ')
set(handles.edit5,'string',' ')
set(handles.listbox1,'string',' ')
global choice
global FileName;
global Newpath1;
global N;
global time;
global img1;
global TEPLOTA;
global Data;
global contents;
global index_selected;
choice=1;
contents=1;
% nastavení viditelnosti jednotlivých tlačítek a oken 
set(handles.radiobutton1, 'Value', 1);
set(handles.checkbox1, 'visible', 'off');
set(handles.checkbox2,'value',0)
set(handles.checkbox2,'visible','off')
set(handles.pushbutton6,'visible','off')
set(handles.uipanel2, 'visible', 'on');
set(handles.uipanel17, 'visible', 'off');
set(handles.error,'string',' ')
[FileName,Newpath1] = uigetfile('*.jpg','Vyberte snímky', 'MultiSelect', 'on');
N=length(FileName);
img1=[];
if isequal(FileName,0)|| isequal(Newpath1,0)
else
    if iscell(FileName)==0
        
        note='VYBERTE ALESPOŇ DVA SNÍMKY';
        k = warndlg(note); % varovná zpráva v případě, že je načten pouze jeden snímek
    else h=waitbar(0,'Načítání snímků'); % vytvoření progressbaru
        set(findobj(h,'type','patch'), ...
            'edgecolor','b','facecolor','b')
        for i=1:N
            system(['exiftool -b -RawThermalImage',' "', Newpath1, FileName{1,i},'"  -w %f.tif']); % získání surových dat z termografiských snímku pomocí externího programu exif tool
            waitbar(i/(2*N)); 
        end
        [time,FileName,TEPLOTA,Data]=time_delay(FileName,Newpath1,N); % funkce na vypoštení teplot a získání časových udaju snímku
        set(handles.listbox1,'string',FileName(1,:)); % zobrazení načtených snímků do GUI
        set(handles.text8, 'visible', 'off'); % nastavení viditelnosti prvků GUI
        set(handles.text7, 'visible', 'off');
        img=imread([Newpath1 '\' char(FileName(1,1))]); % načtení prvního anímku a..
        axes(handles.axes1); % jeho zobrazení
        imshow(img,handles.axes1); 
        index_selected=get(handles.listbox1,'value'); % uložení indexu vybraného snímku
        close(h) % zavřemí waitbaru
        set(handles.pushbutton4,'visible','on') % nastavení viditelnosti prvků GUI
    end
    
end


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

% --- Executes during object creation, after setting all properties.
function error_CreateFcn(hObject, eventdata, handles)
% hObject    handle to error (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global path1; % zavření programu
delete(['*.tif']); % smazání vytvořených snímků
delete(['*.mhd'])
delete(['*.raw'])
if exist([path1,'parametry'])>0 % pokud existuje vytvořená složka
    rmdir([path1,'parametry'],'s'); % smazaní vytvořené složky
    rmdir([path1,'transformace'],'s'); % smazání vytvořené složky v
end
clear all
close all

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

% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

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

% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% načtení globálních promněných
global FileName;
global Newpath1;
global N;
global Data;
global hranice;
global line;
global path1;
global head;

name=FileName{1,1}; % vybrání prvního snímku ze série
img=imread([name(1:end-3),'tif']); % jeho načtení
[m,n]=size(img); % zjištění velikosti 
path1=which('elastix.exe'); % nalezení cesty k programu elastix
path1=path1(1:end-length('elastix.exe')); 
elastix_export_raw_data(path1,FileName,N); % vytvoření mhd snímku, který umožnuje zpracování elastixem
runelastix(N,path1,FileName,m,n); % provedení slícování snímků
[X,Y,line,polovina1,polovina2,head,hranice]=cluster_analysis(FileName{1,1},Newpath1); % segmentace

[FileName]=metadata(FileName,N,path1,X,Y,Data); % výpočet teploty pro celého obličej
[FileName]=metadata(FileName,N,path1,polovina1(:,1),polovina1(:,2),Data); % výpočet teploty pro 1. polovinu obličej
[FileName]=metadata(FileName,N,path1,polovina2(:,1),polovina2(:,2),Data);% výpočet teploty pro 2. polovinu obličej

for i=1:N % výpočet průměrné hodnoty a odchylky z prohledávané oblasti
    t=FileName{2,i};
    FileName{8,i}=mean(t);
    FileName{9,i}=std(t,1);
end

for i=1:N
    t1=FileName{4,i}; % výpočet popisné statistky
    t2=FileName{6,i};
    FileName{10,i}=mean(t1);
    FileName{11,i}=mean(t2);
    FileName{12,i}=std(t1);
    FileName{13,i}=std(t2);
end
set(handles.pushbutton6,'visible','on') % nastavení viditelností prvků GUI
set(handles.pushbutton4,'visible','off')
set(handles.uipanel5,'visible','on')
set(handles.uipanel18,'visible','on')
set(handles.checkbox2,'visible','on')
set(handles.text8, 'visible', 'on');
set(handles.text7, 'visible', 'on');

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


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (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 listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% contents = cellstr(get(FileName(1,:),'String'))
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1
% načtění globálních konstant
global FileName;
global Newpath1;
global index_selected;
global contents
global oblast1;
global choice;
global img1;
set(handles.checkbox2,'value',0) % nastavení počátečních hodnot
index_selected = get(hObject,'Value');
if ~isempty(img1)&& index_selected==1
    axes(handles.axes1) % zobrazení snímku
    imshow(img1);
else
    img=imread([Newpath1 '\' char(FileName(1,index_selected))]);
    axes(handles.axes1)
    imshow(img); % zobrazení vybraného snímku
    if index_selected~=1
         set(handles.checkbox1,'value',0); % nastavení viditelnosti prvků GUI
         set(handles.checkbox2,'value',0);
    end
end
[c,~]=size(FileName); % zjištění velikosti soubrou obsahující informace o analyzovaných snímcích
if c>1 && choice==1 % zjištění nastavení analýzy
    switch contents % zvolena analýza v jedné oblasti 
        case 1
            t_extr=FileName{3,index_selected}; % celý obličej
            set(handles.edit2,'String',num2str(t_extr(1)));
            set(handles.edit4,'String',num2str(t_extr(2)));
            set(handles.edit5,'String',num2str(t_extr(3)));
            
        case 2
            t_extr=FileName{end-2,index_selected}; % oblast zvolená uživatelem
            set(handles.edit2,'String',num2str(t_extr(1))); % zobrazení popisných charakteristik
            set(handles.edit4,'String',num2str(t_extr(2)));
            set(handles.edit5,'String',num2str(t_extr(3)));
    end
elseif c>1 && choice==2 % výběr dvou oblastí
    
    switch contents
        case 1
            t_extr=FileName{5,index_selected}; % výběr polovin obličeje
            set(handles.edit10,'String',num2str(t_extr(1))); % zobrazení popiných charakteristik
            set(handles.edit11,'String',num2str(t_extr(2)));
            set(handles.edit12,'String',num2str(t_extr(3)));
            t_extr1=FileName{7,index_selected};
            set(handles.edit13,'String',num2str(t_extr1(1)));
            set(handles.edit14,'String',num2str(t_extr1(2)));
            set(handles.edit15,'String',num2str(t_extr1(3)));
            
        case 2
            if max(max(oblast1))>0 % výber oblastí uživatelem
                
                t_extr=FileName{end-6,index_selected}; % zobrazení popisných charakteristik
                set(handles.edit10,'String',num2str(t_extr(1)));
                set(handles.edit11,'String',num2str(t_extr(2)));
                set(handles.edit12,'String',num2str(t_extr(3)));
                t_extr1=FileName{end-4,index_selected};
                set(handles.edit13,'String',num2str(t_extr1(1)));
                set(handles.edit14,'String',num2str(t_extr1(2)));
                set(handles.edit15,'String',num2str(t_extr1(3)));
                
            end
            
    end
end

% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes when selected object is changed in uipanel5.
function uipanel5_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel5
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)
% načtení globálních promněných
global choice
global FileName;
global index_selected;
global Newpath1;
set(handles.checkbox2,'value',0)

switch get(eventdata.NewValue,'Tag') % Get Tag of selected object.
    case 'radiobutton1' % výběr jedna oblast
        choice=1;
        img1=[]; % vynulování promněné
        img=imread([Newpath1 '\' char(FileName(1,index_selected))]);
        axes(handles.axes1) % zobrazení snímku
        imshow(img);
        
    case 'radiobutton2'
        choice=2; % výběr dvou oblastí
        img1=[]; % vynulování promněné
        img=imread([Newpath1 '\' char(FileName(1,index_selected))]);
        axes(handles.axes1) % zobrazení vybraného snímku
        imshow(img);
end

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


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (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 during object creation, after setting all properties.
function pushbutton2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

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


% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% naštení globálních promněných
global line;
global index_selected;
global FileName;
global Newpath1;
osa=get(hObject,'Value'); % získej hodnotu osy
if osa==1 % pokud je osa obličeje zobrazená
    img=imread([Newpath1 '\' char(FileName(1,1))]); % načti první snímek
    set(handles.listbox1,'Value',1); % označ jej v listboxu
    index_selected=1; % snímek jedna je vybrán
    t_extr=FileName{5,index_selected}; % pošli extrémní hodnoty snímku  do oken
    set(handles.edit10,'String',num2str(t_extr(1)));
    set(handles.edit11,'String',num2str(t_extr(2)));
    set(handles.edit12,'String',num2str(t_extr(3)));
    t_extr1=FileName{7,index_selected};
    set(handles.edit13,'String',num2str(t_extr1(1)));
    set(handles.edit14,'String',num2str(t_extr1(2)));
    set(handles.edit15,'String',num2str(t_extr1(3)));
    for i=1:length(line(:,1))
        img(round(line(i,1)),round(line(i,2)),:)=0; % vykreslení přímky
        
    end
    axes(handles.axes1)
    imshow(img); % zobrazit snímek
elseif osa==0 % pokud není vybraná
    img=imread([Newpath1 '\' char(FileName(1,index_selected))]);
    axes(handles.axes1)
    imshow(img); % zobarzí se vybraný snímek
end
% Hint: get(hObject,'Value') returns toggle state of checkbox1



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

% Hints: get(hObject,'String') returns contents of edit9 as text
%        str2double(get(hObject,'String')) returns contents of edit9 as a double


% --- Executes during object creation, after setting all properties.
function edit9_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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

% Hints: get(hObject,'String') returns contents of edit8 as text
%        str2double(get(hObject,'String')) returns contents of edit8 as a double


% --- Executes during object creation, after setting all properties.
function edit8_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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

% Hints: get(hObject,'String') returns contents of edit7 as text
%        str2double(get(hObject,'String')) returns contents of edit7 as a double


% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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

% Hints: get(hObject,'String') returns contents of edit10 as text
%        str2double(get(hObject,'String')) returns contents of edit10 as a double


% --- Executes during object creation, after setting all properties.
function edit10_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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

% Hints: get(hObject,'String') returns contents of edit11 as text
%        str2double(get(hObject,'String')) returns contents of edit11 as a double


% --- Executes during object creation, after setting all properties.
function edit11_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit11 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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


% --- Executes during object creation, after setting all properties.
function edit12_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit12 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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


% --- Executes during object creation, after setting all properties.
function edit13_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit13 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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


% --- Executes during object creation, after setting all properties.
function edit14_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit14 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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


% --- Executes during object creation, after setting all properties.
function edit15_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit15 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in edit16.
function edit16_Callback(hObject, eventdata, handles)
% hObject    handle to edit16 (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 edit16
% a=handles.a;
global prah
prah=round(get(hObject,'Value')*100)/100
set(handles.edit17,'String',num2str(prah))

% --- Executes during object creation, after setting all properties.
function edit16_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit16 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

set(gcbo,'Value',0.5);

% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton2.
function pushbutton2_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% delete(['*.tif']);
% rmdir([path1,'parametry'],'s')
% rmdir([path1,'transformace'],'s')
% close(handles.tagOfTheGui)



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


% --- Executes during object creation, after setting all properties.
function edit17_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit17 (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 during object creation, after setting all properties.
function mean_value_CreateFcn(hObject, eventdata, handles)
% hObject    handle to mean_value (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate mean_value
% set(gca,'Tag','myplotaxes');



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


% --- Executes during object creation, after setting all properties.
function edit18_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit18 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in togglebutton3.
function togglebutton3_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton3 (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 togglebutton3
% načtení globálních promněných
global index_selected;
global TEPLOTA;
if isempty(TEPLOTA)~=1; % pokud je zmáčknuté tlačítko zobrazit teplotu
    [A]=get(hObject,'Value'); %
    [T1]=TEPLOTA{1,index_selected}; % načíst teploty daného snímku
    [m,n]=size(T1); % zjstit velikost
    while A==1
        axes(handles.axes1);
        [p1,p2,button]=ginput(1); % zobrazit kurzor a po kliknutí získat pozici
        
        
        if button~=1 || p1>n  || p2>m || p1<0 || p2<0 % pokud nebylo zmčknuto levé tlačítko myši či bylo kliknuto mimo snímek
            set(handles.togglebutton3,'Value',0) % vypnutí tlačítka
            set(handles.edit18,'String',' ') % vynulování okna
            break % zastav měření 
        end
        T=T1(round(p2),round(p1)); % najdi teplotu na získané pozici
        set(handles.edit18,'String',num2str(T)); % pšli do zobrazovácího okna
        
    end
end


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over togglebutton3.
function togglebutton3_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to togglebutton3 (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 pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% načtení globálních promněných
global choice
global FileName;
global time;
global index_selected;
global prah;
global contents;
global path1;
global img1;
global oblast;
global oblast1
global head;
global Newpath1;
global N;
global Data;
[c,~]=size(FileName); % zjištění velikosti pole ve kterém jsou informace o zpracovávaných snímcích
contents=get(handles.popupmenu2,'value'); % zjisti zda je vybrána možnost celý obličej nebo výber oblasti
prah=get(handles.edit16,'value');
parameter=get(handles.popupmenu3,'Value');
set(handles.checkbox2,'value',0)
switch choice % Get Tag of selected object.  
    case 1 % výber celého obličeje
        set(handles.edit10,'String',' ');
        set(handles.edit11,'String',' ');
        set(handles.edit12,'String',' ');
        set(handles.edit13,'String',' ');
        set(handles.edit14,'String',' ');
        set(handles.edit15,'String',' ');
        set(handles.uipanel2, 'visible', 'on'); % nastavení viditelnosti prvků GUI
        set(handles.uipanel17, 'visible', 'off');
        set(handles.checkbox1, 'visible', 'off');
        set(handles.checkbox2,'value',0)
        if c>1 % pokud jsou jiř vypočtené teploty a je provedeno zarovnní snímku
            set(handles.mean_value, 'visible', 'on');
            set(handles.std_value, 'visible', 'on');
            y1=[];         
            switch contents 
                
                case 1 % a je zvolena jedna oblast
                    img1=[];
                    img=imread([Newpath1 '\' char(FileName(1,index_selected))]);
                    axes(handles.axes1)
                    imshow(img);
                    y2=[FileName{9,:}];               % načtení hodnoty směrodatných odchylek
                    switch parameter % zvolení sledovaného parametru
                        case 1
                              y1=[FileName{8,:}]; % prumerná teplota
                              
                            diff=max(y1)-min(y1); % rozdíl
                        case 2
                            for h=1:N
                                t=FileName{3,h}; % maximální teplota
                                y1=[y1,t(1)];
                            end
                            diff=max(y1)-min(y1); % rozdíl
                        case 3
                            for h=1:N   
                                t=FileName{3,h}; % minimální teplota
                                y1=[y1,t(2)];
                            end
                            diff=max(y1)-min(y1); % rozdíl
                    end
                    if abs(diff)>prah  % testování zda byl překročen práh
                        note='PŘEKROČENÍ PRAHU - U SLEDOVANÉ OSOBY DOŠLO K VÝRAZNÉ ZMĚNĚ TEPLOTY V OBLIČEJI'; % vyhodnocení
                        set(handles.error, 'String', note);
                        set(handles.error,'BackgroundColor','red');
                    else
                        note='PRÁH NEBYL PŘEKROČEN - U SLEDOVANÉ OSOBY NEDOŠLO K VÝRAZNÉ ZMĚNĚ TEPLOTY V OBLIČEJI'; % vyhodnocení
                        set(handles.error, 'String', note);
                    end
                    t_extr=FileName{3,index_selected}; % vypsání extremu ve snímku
                    set(handles.edit2,'String',num2str(t_extr(1)));
                    set(handles.edit4,'String',num2str(t_extr(2)));
                    set(handles.edit5,'String',num2str(t_extr(3)));
                    
                    set(handles.text8, 'visible', 'on');
                    set(handles.text7, 'visible', 'on');
                    axes(handles.mean_value)
                    plot(time(2,:),y1,'b*'); % vykreslení grafu 

                    xlabel('čas od sejmutí prvníhu snímku [s]')
                    ylabel('[°C]')
                    a1=min(y1);
                    a2=max(y1);
                    axis([0,time(end),(a1-(a2-a1))-0.2,a2+(a2-a1)+0.2]); % nastavení os
                    
                    axes(handles.std_value)
                    plot(time(2,:),y2,'b*'); % vykreslení grafu
                    
                    
                    a3=min([FileName{9,:}]);
                    a4=max([FileName{9,:}]);
                    axis([0,time(end),a3-(a4-a3)-0.1,a4+(a4-a3)+0.1]) % nastavení os
                    xlabel('čas od sejmutí prvníhu snímku [s]')
                    ylabel('[°C]')
                case 2 % zvolení možnosti výběr uživatelem
                    [x_1,y_1]=find(ceil(head)&ceil(oblast)); % nalezení masky zvolené oblasti
                    if ~isempty(x_1) % pokud není prázdná
                    [FileName]=metadata(FileName,N,path1,x_1,y_1,Data); % výpočet teplot v teto oblasti
                    A=cell(1,N); % pomocná promněná pro ukládání
                    A1=cell(1,N);
                    d1=[];
                    d2=[];
                    for i=1:N
                        t=FileName{end-1,i}; % uložení teploty
                        A1{1,i}=mean(t);
                        A2{1,i}=std(t,1);
                    end
                    FileName=[FileName;A1];
                    FileName=[FileName;A2];
                    y2=[FileName{end,:}];
                    diff=max(y1)-min(y1); % výpočet zda byl překročen práh
                    
                    switch parameter % výber parametru
                        case 1
                             y1=[FileName{end-1,:}]; % prumer
                             diff=max(y1)-min(y1); % testování zda došlo k překročení prahu
                       case 2
                            for h=1:N
                                t=FileName{end-2,h}; % maximum
                                y1=[y1,t(1)];
                            end
                            diff=max(y1)-min(y1); % testování zda došlo k překročení prahu
                        case 3
                            for h=1:N
                                t=FileName{end-2,h}; % minimum
                                y1=[y1,t(2)];
                            end
                            diff=max(y1)-min(y1); % testování zda došlo k překročení prahu
                    end
                    if abs(diff)>prah % vyhodnoceníí
                        note='PŘEKROČENÍ PRAHU - U SLEDOVANÉ OSOBY DOŠLO K VÝRAZNÉ ZMĚNĚ TEPLOTY V OBLIČEJI';
                        set(handles.error, 'String', note);
                        set(handles.error,'BackgroundColor','red');
                    else
                        note='PRÁH NEBYL PŘEKROČEN - U SLEDOVANÉ OSOBY NEDOŠLO K VÝRAZNÉ ZMĚNĚ TEPLOTY V OBLIČEJI';
                        set(handles.error, 'String', note);
                    end
                    t_extr=FileName{end-2,index_selected}; % vypsání popisných charakteristik
                    set(handles.edit2,'String',num2str(t_extr(1)));
                    set(handles.edit4,'String',num2str(t_extr(2)));
                    set(handles.edit5,'String',num2str(t_extr(3)));
                    
                    set(handles.text8, 'visible', 'on');
                    set(handles.text7, 'visible', 'on');
                    axes(handles.mean_value)
                    plot(time(2,:),y1,'b*'); % vtvoření grafu
                    xlabel('čas od sejmutí prvníhu snímku [s]')
                    ylabel('[°C]')
                    a1=min(y1);
                    a2=max(y1);
                    axis([0,time(end),(a1-(a2-a1))-0.2,a2+(a2-a1)+0.2]); % vypočet rozsahu osy
                    
                    axes(handles.std_value)
                    plot(time(2,:),y2,'b*'); % vytvoření grafu
                    
                    
                    a3=min([FileName{end,:}]);
                    a4=max([FileName{end,:}]);
                    axis([0,time(end),a3-(a4-a3)-0.1,a4+(a4-a3)+0.1]) % nastavení os
                    xlabel('čas od sejmutí prvníhu snímku [s]')
                    ylabel('[°C]')
                    else 
                        note='ZVOLENÁ OBLAST SE NECHAZÍ V OBLASTI ZÁJMU, PROSÍM ZVOLTE JINOU '; % pokud nebyla vybrána žádna oblast , vypíše se chybová hlásška
                        set(handles.error, 'String', note);
                        set(handles.popupmenu2, 'value', 0);
                    end
            end
        end
    case 2; % výber 2 oblasti
        set(handles.edit2,'String',' ');  % nastavení viditelnsti prvku GUI
        set(handles.edit4,'String',' ');
        set(handles.edit5,'String',' ');
        set(handles.uipanel2, 'visible', 'off');
        set(handles.uipanel17, 'visible', 'on');
        
        if c>1 % &&
            switch contents
                case 1 % celý obličej analogicky k předchozímu
                    img1=[];
                    img=imread([Newpath1 '\' char(FileName(1,index_selected))]);
                    axes(handles.axes1)
                    imshow(img);
                    set(handles.checkbox1, 'visible', 'on');
                    set(handles.checkbox2,'value',0)
                    set(handles.checkbox1, 'value', 0);

                    y11=[FileName{12,:}];
                    y22=[FileName{13,:}];
                    y1=[];
                    y2=[];
                    set(handles.text8, 'visible', 'on');
                    set(handles.text7, 'visible', 'on');
                    parameter=get(handles.popupmenu3,'Value');
                    switch parameter
                        case 1
                            y1=[FileName{10,:}];
                            y2=[FileName{11,:}];
                            diff=(min(y2)-min(y1)); % zda v kterémkoliv čase došlo k překročení prahu
                        case 2
                            for h=1:N
                                 t1=FileName{5,h};
                                 t2=FileName{7,h};
                                 y1=[y1,t1(1)];
                                 y2=[y2,t2(1)];
                            end                             
                               diff=max(abs(y1-y2)); % zda v kterémkoliv čase došlo k překročení prahu
                        case 3
                             for h=1:N
                                 t1=FileName{5,h};
                                 t2=FileName{7,h};
                                 y1=[y1,t1(2)];
                                 y2=[y2,t2(2)];
                             end

                               diff=max(abs(y1-y2)); % zda v kterémkoliv čase došlo k překročení prahu
                    end
                     
                     if abs(diff)>prah % vyhodnocení
                         note='PŘEKROČENÍ PRAHU - U SLEDOVANÉ OSOBY DOŠLO K VÝRAZNÉ ZMĚNĚ TEPLOTY V OBLIČEJI';
                         set(handles.error, 'String', note,'BackgroundColor','red');
                        
                    else
                        note='PRÁH NEBYL PŘEKROČEN - U SLEDOVANÉ OSOBY NEDOŠLO K VÝRAZNÉ ZMĚNĚ TEPLOTY V OBLIČEJI';
                        set(handles.error, 'String', note);
                    end
                    t_extr=FileName{5,index_selected}; % vypsání hodnot
                    set(handles.edit10,'String',num2str(t_extr(1)));
                    set(handles.edit11,'String',num2str(t_extr(2)));
                    set(handles.edit12,'String',num2str(t_extr(3)));
                    t_extr1=FileName{7,index_selected};
                    set(handles.edit13,'String',num2str(t_extr1(1)));
                    set(handles.edit14,'String',num2str(t_extr1(2)));
                    set(handles.edit15,'String',num2str(t_extr1(3)));
                    
                    axes(handles.mean_value) % vykreslení grafu
                    plot(time(2,:),y1,'b*');
                    hold on
                    plot(time(2,:),y2,'r*');
                    
                    a1=min(min(y1),min(y2));
                    a2=max(max(y1),max(y2));
                    axis([0,time(end),a1-(a2-a1)-0.2,a2+(a2-a1)+0.2]); % nastvaená rozsahu
                    legend('tvář vlevo','tvář vpravo','location','southeast');
                    xlabel('čas od sejmutí prvníhu snímku [s]')
                    ylabel('[°C]')
                    hold off
                    
                    axes(handles.std_value)
                    plot(time(2,:),y11,'b*');
                    hold on
                    plot(time(2,:),y22,'r*');
                    
                    legend('tvář vlevo','tvář vpravo','location','SouthEast');
                    lh=findall(gcf,'tag','legend');
                    set(lh,'location','northeastoutside')
                    xlabel('čas od sejmutí prvníhu snímku [s]')
                    ylabel('[°C]')
                    a3=min(min([FileName{12,:}]),min([FileName{13,:}]));
                    a4=max(max([FileName{12,:}]),max([FileName{13,:}]));
                    axis([0,time(end),a3-(a4-a3)-0.1,a4+(a4-a3)+0.1]) % nastavení rozsahu
                    hold off
                    
                case 2 % výber oblastí uživatelem
                    
                    if max(max(oblast1))>0 % pokud je vybrána nejaká oblast
                        [x_1,y_1]=find(ceil(head)&ceil(oblast)); % nalezená masky pro 1. oblast
                        [x_2,y_2]=find(ceil(head)&ceil(oblast1)); % nalezení masky pro 2. oblast
                        if ~isempty(x_1)&& ~isempty(x_2) % pokud neni prazdné
                            [FileName]=metadata(FileName,N,path1,x_1,y_1,Data); % výpočet teplot
                            [FileName]=metadata(FileName,N,path1,x_2,y_2,Data); % výpočet teplot
                            A=cell(1,N);
                            A1=cell(1,N);
                            A2=cell(1,N);
                            A3=cell(1,N);
                            y1=[];
                            y2=[];
                            for i=1:N % uložení vypočtených teplot
                                t=FileName{end-3,i};
                                A{1,i}=mean(t);
                                A1{1,i}=std(t,1);
                                t1=FileName{end-1,i};
                                A2{1,i}=mean(t1);
                                A3{1,i}=std(t1,1);
                            end
                            FileName=[FileName;A];
                            FileName=[FileName;A1];
                            FileName=[FileName;A2];
                            FileName=[FileName;A3];

                            y11=[FileName{end-2,:}];
                            y22=[FileName{end,:}];
                            set(handles.text8, 'visible', 'on');
                            set(handles.text7, 'visible', 'on');
                            parameter=get(handles.popupmenu3,'Value'); % výber parametru
                            switch parameter
                                case 1
                                    y1=[FileName{end-3,:}];
                                    y2=[FileName{end-1,:}];
                                     diff=max(abs(y1-y2));
                                case 2
                                    for h=1:N
                                        t1=FileName{end-6,h};
                                        t2=FileName{end-4,h};
                                        y1=[y1,t1(1)];
                                        y2=[y2,t2(1)];
                                    end
                                     diff=max(abs(y1-y2));
                                case 3
                                    for h=1:N
                                        t1=FileName{end-6,h};
                                        t2=FileName{end-4,h};
                                        y1=[y1,t1(2)];
                                        y2=[y2,t2(2)];
                                    end
                                     diff=max(abs(y1-y2));
                            end
                            
                            if abs(diff)>prah
                                note='PŘEKROČENÍ PRAHU - U SLEDOVANÉ OSOBY DOŠLO K VÝRAZNÉ ZMĚNĚ TEPLOTY V OBLIČEJI';
                                set(handles.error, 'String', note);

                            else
                                note='PRÁH NEBYL PŘEKROČEN - U SLEDOVANÉ OSOBY NEDOŠLO K VÝRAZNÉ ZMĚNĚ TEPLOTY V OBLIČEJI';
                                set(handles.error, 'String', note);
                              
                                
                            end
                            t_extr=FileName{end-6,index_selected}; % zobrazených popisných charakteristik
                            set(handles.edit10,'String',num2str(t_extr(1)));
                            set(handles.edit11,'String',num2str(t_extr(2)));
                            set(handles.edit12,'String',num2str(t_extr(3)));
                            t_extr1=FileName{end-4,index_selected};
                            set(handles.edit13,'String',num2str(t_extr1(1)));
                            set(handles.edit14,'String',num2str(t_extr1(2)));
                            set(handles.edit15,'String',num2str(t_extr1(3)));
                            % vykreslení grafů
                            axes(handles.mean_value)
                            plot(time(2,:),y1,'b*');
                            hold on
                            plot(time(2,:),y2,'r*');
                            
                            a1=min(min(y1),min(y2));
                            a2=max(max(y1),max(y2));
                            axis([0,time(end),a1-(a2-a1)-0.2,a2+(a2-a1)+0.2]);
                            legend('oblast 1','oblast 2','location','southeast');
                            xlabel('čas od sejmutí prvníhu snímku [s]')
                            ylabel('[°C]')
                            hold off
                            
                            axes(handles.std_value)
                            plot(time(2,:),y11,'b*');
                            hold on
                            plot(time(2,:),y22,'r*');
                            
                            legend('oblast 1','oblast 2','location','SouthEast');
                            lh=findall(gcf,'tag','legend');
                            set(lh,'location','northeastoutside')
                            xlabel('čas od sejmutí prvníhu snímku [s]')
                            ylabel('[°C]')
                            a3=min(min(y11),min(y22));
                            a4=max(max(y11),max(y22));
                            axis([0,time(end),a3-(a4-a3)-0.1,a4+(a4-a3)+0.1])
                            hold off
                        else
                            note='ALESPOŇ JEDNA ZVOLENÁ OBLAST SE NENACHAZÍ V OBLASTI ZÁJMU, PROSÍM ZVOLTE JINOU '; % v případě umístění oblastí mimo obličejovou oblast
                            set(handles.error, 'String', note);
                            set(handles.popupmenu2, 'value', 0);
                        end
                    end
            end
        end
end
set(handles.popupmenu2,'Value',1) % nastavení výberu na default


% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (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 popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2
% nasčzení globálních parametrů
global choice;
global Newpath1;
global FileName;
global oblast;
global img1;
global oblast1;
global contents;

velikost_oblasti=round(get(handles.slider6,'Value'))/2; % na polovinu kvuli hledání středního bodu
contents = get(hObject,'Value');
img1=[];
img1=imread([Newpath1 '\' char(FileName(1,1))]); % vykreslení 1. snímku
[m,n]=size(img1); % zjištění velikosti
k=zeros(size(rgb2gray(img1))); % pomocná matice
k1=zeros(size(rgb2gray(img1))); % druhá pomocná matice
switch contents
    case 1 % celý obličej
        
    case 2 % výbe oblasti uživatelem
        set(handles.checkbox1,'visible','off') % nastaneví viditelnosti prvků GUI
        set(handles.checkbox2,'value',0)
                
        index_selected=1; % 
        axes(handles.axes1)
        imshow(img1);
        set(handles.listbox1,'value',1); % nastavení v listboxu
        
        if choice==1 %
            color=zeros(1+2*velikost_oblasti,1,3); % pomocná matice pro vykreslení
            color(:,1,3)=255; % nastavení barvy
            while 1<2
                A=round(ginput(1));
                if (A(1)<n  && A(2)<m && A(1)>0 && A(2)>0) % vyber oblasti neleží mimo snímek
                    img1(A(2)-velikost_oblasti:A(2)+velikost_oblasti,A(1)-velikost_oblasti,:)=color; % vykreslení čtverece do snímku
                    img1(A(2)-velikost_oblasti:A(2)+velikost_oblasti,A(1)+velikost_oblasti,:)=color;
                    img1(A(2)+velikost_oblasti,A(1)-velikost_oblasti:A(1)+velikost_oblasti,:)=color;
                    img1(A(2)-velikost_oblasti,A(1)-velikost_oblasti:A(1)+velikost_oblasti,:)=color;
                    k(A(2)-velikost_oblasti:A(2)+velikost_oblasti,A(1)-velikost_oblasti:A(1)+velikost_oblasti)=1; % zakreslení oblasti do pomocného snímku
                    axes(handles.axes1)
                    imshow(img1); % překreslení
                    set(handles.listbox1,'Value',1)
                    break % zastavení cyklu
                end
            end
            
        elseif choice==2 % výber dvou oblastí
            
            while 1<2
                A=round(ginput(1)); % výber první oblasti
                if (A(1)<n  && A(2)<m && A(1)>0 && A(2)>0) % oblast leží uvnitř snímku
                    color=zeros(velikost_oblasti*2+1,1,3); % pomocná matice
                    color(:,1,3)=255; % nastavení barvy
                    img1(A(2)-velikost_oblasti:A(2)+velikost_oblasti,A(1)-velikost_oblasti,:)=color; % vykreslení čtverce do snímku
                    img1(A(2)-velikost_oblasti:A(2)+velikost_oblasti,A(1)+velikost_oblasti,:)=color;
                    img1(A(2)+velikost_oblasti,A(1)-velikost_oblasti:A(1)+velikost_oblasti,:)=color;
                    img1(A(2)-velikost_oblasti,A(1)-velikost_oblasti:A(1)+velikost_oblasti,:)=color;
                    k(A(2)-velikost_oblasti:A(2)+velikost_oblasti,A(1)-velikost_oblasti:A(1)+velikost_oblasti)=1; % vykreslení masky
                    axes(handles.axes1)
                    imshow(img1);
                    break
                end
            end
            while 1<2
                A=round(ginput(1));
                if (A(1)<n  && A(2)<m && A(1)>0 && A(2)>0) % pokud vybraná oblast leží uvnintř snímku
                    color=zeros(velikost_oblasti*2+1,1,3); % pomocná amtice pro vykrslení barvy
                    color(:,1,1)=255; % nastavení barvy
                    img1(A(2)-velikost_oblasti:A(2)+velikost_oblasti,A(1)-velikost_oblasti,:)=color; % vykreslení
                    img1(A(2)-velikost_oblasti:A(2)+velikost_oblasti,A(1)+velikost_oblasti,:)=color;
                    img1(A(2)+velikost_oblasti,A(1)-velikost_oblasti:A(1)+velikost_oblasti,:)=color;
                    img1(A(2)-velikost_oblasti,A(1)-velikost_oblasti:A(1)+velikost_oblasti,:)=color;
                    k1(A(2)-velikost_oblasti:A(2)+velikost_oblasti,A(1)-velikost_oblasti:A(1)+velikost_oblasti)=1; % vykrslení pomocné masky
                    axes(handles.axes1) % překreslení
                    imshow(img1);
                    break
                end
            end
        end
end
oblast1=ceil(imresize(k1,[150,200])); % zmenšení do velikosti metadat. snímku
oblast=ceil(imresize(k,[150,200])); % zmenšení do velikosti metadat. snímku



% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (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 pushbutton6.
function pushbutton6_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (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 popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (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 popupmenu3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu3


% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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


% --- Executes during object creation, after setting all properties.
function edit19_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit19 (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
set(gcbo,'Value',5);


% --- Executes on slider movement.
function slider6_Callback(hObject, eventdata, handles)
% hObject    handle to slider6 (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global velikost_oblasti
velikost_oblasti=round(get(hObject,'Value'));
set(handles.edit21,'String',num2str(velikost_oblasti))

% --- Executes during object creation, after setting all properties.
function slider6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
%     handles.slider6 = gcbo;
set(gcbo,'Value',10);
guidata(hObject,handles);


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


% --- Executes during object creation, after setting all properties.
function edit21_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit21 (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
set(gcbo,'Value',5);


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

% načtení globálních promněných
global Newpath1;
global FileName;
global hranice;

hr=get(hObject,'Value');
if hr==1
    img=imread([Newpath1 '\' char(FileName(1,1))]); % načtení prvního snímku
    for i=1:length(hranice(:,1))
        img(hranice(i,1),hranice(i,2),:)=0; %vykreslení kontury segmentované oblasti
    end
    set(handles.listbox1,'Value',1); % v listboxu zobrazit první snímek
    index_selected=1;
    axes(handles.axes1) % vykreslit
    imshow(img);
else
    img=imread([Newpath1 '\' char(FileName(1,1))]);
    set(handles.listbox1,'Value',1);
    index_selected=1;
    axes(handles.axes1) % vykreslit snímek bez kontury
    imshow(img);
end
