function varargout = v1(varargin)
% aplikacni rozhrani rozpoznani gest (hlavni spousteci funkce)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @v1_OpeningFcn, ...
                   'gui_OutputFcn',  @v1_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 v1 is made visible.
function v1_OpeningFcn(hObject, eventdata, handles, varargin)
% clc;
ladit=get(handles.slider_prah_segm,'Value');
set(handles.text_prah_segm,'string',num2str(ladit));
senzitivita=get(handles.hamming_sensitivita,'Value');
set(handles.text_sensitivita,'string',num2str(senzitivita));
set(handles.mira_orezu_hodnota,'string','1.000');
set(handles.mira_rotace_hodnota,'string','0.800');
set(handles.medfiltrace_pred_segm_okno,'string','3');
set(handles.med_filtrace_okno,'string','5');
set(handles.uzavreni_objekt,'string','5');
% 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 v1 (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = v1_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 nacti_fotku.
function nacti_fotku_Callback(hObject, eventdata, handles)
% nacteni fotky
cd data;
global obr_nacti  obr nFrames startFrames;
global zobraz_data1 zobraz_data2;
global limit_rozliseni;
global fotka video;
limit_rozliseni=1;

nFrames=1;
startFrames=1;
[cesta,hlaska]=imgetfile();
cd ..;
if hlaska
    msgbox(sprintf('fotku se nepodarilo nacist'),'Chyba nacitani')
    return;
end
obr_nacti = im2double(imread(cesta));
obr = obr_nacti;
axes(handles.figure_obr1);
imshow(obr);
num=num2str(100);
set(handles.nacteno_video,'String',num);

fotka=1;
video=0;
%nastaveni radiobuttonu
set(handles.obr1_puvodni_zobraz,'Value',1);
set(handles.obr2_nezobraz,'Value',1);
set(handles.obr1_nezobraz,'Value',0);
set(handles.obr1_segment_zobraz,'Value',0);
set(handles.obr2_segment_zobraz,'Value',0)
set(handles.obr2_zprac_zobraz,'Value',0)
axes(handles.figure_obr2);
imshow(zeros(10,10));
%nastaveni zobrazeni
zobraz_data1=1;
zobraz_data2=3;


% --- Executes on button press in nacti_video.
function nacti_video_Callback(hObject, eventdata, handles)
%nacteni videa
cd data;
global video_nacti startFrames nFrames stop;
global zobraz_data1 zobraz_data2;
global limit_rozliseni;
global fotka video;

limit_rozliseni=1;
stop=0;
startFrames=1;
[hlaska, cesta] = uigetfile('*.avi');
cesta=[cesta,hlaska];
cd ..;
% [cesta,hlaska]=imgetfile();
if hlaska==0
    msgbox(sprintf('video se nepodarilo nacist'),'Chyba nacitani')
    return;
end
Movie = VideoReader(cesta);     %nacteni objektu
%%  nacteni
nFrames = Movie.NumberOfFrames;
vidHeight = Movie.Height;   %vyska okna
vidWidth = Movie.Width;     %sirka okna
mov(1:nFrames) = struct('cdata', zeros(vidHeight, vidWidth, 'uint8')); %vytvoreni struktury pro video

% Nacteni vsech snimku s procesem nacitani v procentech
fotka=0;
video=1;
pom=0;
for k = 1 : nFrames
    nacteno=round(k/nFrames*100);
    if nacteno/10~=round(((k-1)/nFrames*100)/10)
        pom=0;
    end
    if ((nacteno/10)==round(nacteno/10) && pom==0);
        num=num2str(nacteno);
        set(handles.nacteno_video,'String',num);
        pause(0.0001); %pro zobrazeni hodnoty
        pom=1;
    end
    mov(k).cdata = read(Movie, k);
end

video_nacti=mov;
axes(handles.figure_obr1);
imshow(video_nacti(1).cdata);
%nastaveni radiobuttonu
set(handles.obr1_puvodni_zobraz,'Value',1);
set(handles.obr2_nezobraz,'Value',1);
set(handles.obr1_nezobraz,'Value',0);
set(handles.obr1_segment_zobraz,'Value',0);
set(handles.obr2_segment_zobraz,'Value',0)
set(handles.obr2_zprac_zobraz,'Value',0)
axes(handles.figure_obr2);
imshow(zeros(10,10));

%nastaveni zobrazeni
zobraz_data1=1;
zobraz_data2=3;



% --- Executes on button press in main_flir_lepton.
function main_flir_lepton_Callback(hObject, eventdata, handles)
global slozka; % promenne pro segmentaci;
slozka = 0;
%nastaveni doladeni prahu
set(handles.slider_prah_segm,'value',0.11);
set(handles.text_prah_segm,'string',num2str(0.11));

set(handles.mira_orezu,'value',1.0);
set(handles.mira_rotace,'value',0.8);

set(handles.medfiltrace_pred_segm,'value',0);
set(handles.nejvetsi_objekt,'value',0);
set(handles.med_filtrace,'value',0);
set(handles.uzavreni,'value',0);
set(handles.vyplneni,'value',0);
%vsechny dalsi radio buttony vynuluj
set(handles.main_flir_lepton,'Value',1)
set(handles.main_flir_one,'Value',0)
set(handles.main_vis,'Value',0)

% prepinani buttonu
set(handles.segm_ir,'Value',1)
set(handles.segm_r,'Value',0)
set(handles.segm_rgb,'Value',0)
set(handles.segm_cr,'Value',0)
set(handles.segm_cbcr,'Value',0)
set(handles.segm_rgb,'Value',0)
set(handles.segm_rcr,'Value',0)

% --- Executes on button press in main_flir_one.
function main_flir_one_Callback(hObject, eventdata, handles)
global slozka; % promenne pro segmentaci;
slozka = 0;
%nastaveni doladeni prahu
set(handles.slider_prah_segm,'value',0.12);
set(handles.text_prah_segm,'string',num2str(0.12));

set(handles.mira_orezu,'value',1.0);
set(handles.mira_rotace,'value',0.8);

set(handles.medfiltrace_pred_segm,'value',0);
set(handles.nejvetsi_objekt,'value',0);
set(handles.med_filtrace,'value',0);
set(handles.uzavreni,'value',0);
set(handles.vyplneni,'value',0);
%vsechny dalsi radio buttony vynuluj
set(handles.main_flir_lepton,'Value',0)
set(handles.main_flir_one,'Value',1)
set(handles.main_vis,'Value',0)

% prepinani buttonu
set(handles.segm_ir,'Value',1)
set(handles.segm_r,'Value',0)
set(handles.segm_rgb,'Value',0)
set(handles.segm_cr,'Value',0)
set(handles.segm_cbcr,'Value',0)
set(handles.segm_rgb,'Value',0)
set(handles.segm_rcr,'Value',0)

% --- Executes on button press in main_vis.
function main_vis_Callback(hObject, eventdata, handles)
global slozka; % promenne pro segmentaci;
slozka = 1;
%nastaveni doladeni prahu
set(handles.slider_prah_segm,'value',0.022);
set(handles.text_prah_segm,'string',num2str(0.022));

set(handles.mira_orezu,'value',1.0);
set(handles.mira_rotace,'value',0.8);
set(handles.nejvetsi_objekt,'value',1.0);
set(handles.medfiltrace_pred_segm,'value',0);
set(handles.med_filtrace,'value',0);
set(handles.uzavreni,'value',0);
set(handles.vyplneni,'value',0);

%vsechny dalsi radio buttony vynuluj
set(handles.main_flir_lepton,'Value',0)
set(handles.main_flir_one,'Value',0)
set(handles.main_vis,'Value',1)

% prepinani buttonu
set(handles.segm_ir,'Value',0)
set(handles.segm_r,'Value',0)
set(handles.segm_rgb,'Value',0)
set(handles.segm_cr,'Value',0)
set(handles.segm_cbcr,'Value',1)
set(handles.segm_rgb,'Value',0)
set(handles.segm_rcr,'Value',0)


% --- Executes on button press in kalibrace.
function kalibrace_Callback(hObject, eventdata, handles)
global obr_nacti video_nacti fotka video;
if video==1
imwrite(video_nacti(1).cdata,'kalibrace/kalibrace.bmp')
end
if fotka==1
imwrite(obr_nacti,'kalibrace/kalibrace.bmp');
end
gui_kalibrace




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


% --- Executes during object creation, after setting all properties.
function lim_rozliseni_CreateFcn(hObject, eventdata, handles)
% hObject    handle to lim_rozliseni (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 obr1_puvodni_zobraz.
function obr1_puvodni_zobraz_Callback(hObject, eventdata, handles)
% hObject    handle to obr1_puvodni_zobraz (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 obr1_puvodni_zobraz
global zobraz_data1;
zobraz_data1=1;

global zobraz_orig;
axes(handles.figure_obr1);
imshow(zobraz_orig);

%vsechny dalsi radio buttony vynuluj
set(handles.obr1_puvodni_zobraz,'Value',1)
set(handles.obr1_nezobraz,'Value',0)
set(handles.obr1_segment_zobraz,'Value',0)

% --- Executes on button press in obr1_segment_zobraz.
function obr1_segment_zobraz_Callback(hObject, eventdata, handles)
global zobraz_data1;
zobraz_data1=2;

global obr_seg;
axes(handles.figure_obr1);
imshow(obr_seg);
%vsechny dalsi radio buttony vynuluj
set(handles.obr1_segment_zobraz,'Value',1)
set(handles.obr1_puvodni_zobraz,'Value',0)
set(handles.obr1_nezobraz,'Value',0)


% Hint: get(hObject,'Value') returns toggle state of obr1_segment_zobraz


% --- Executes on button press in obr1_nezobraz.
function obr1_nezobraz_Callback(hObject, eventdata, handles)
global zobraz_data1;
zobraz_data1=3;

%vsechny dalsi radio buttony vynuluj
set(handles.obr1_nezobraz,'Value',1)
set(handles.obr1_puvodni_zobraz,'Value',0)
set(handles.obr1_segment_zobraz,'Value',0)

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

global zobraz_data2;
zobraz_data2=1;

% Hint: get(hObject,'Value') returns toggle state of obr2_segment_zobraz
global obr_seg;
axes(handles.figure_obr2);
imshow(obr_seg);

%vsechny dalsi radio buttony vynuluj
set(handles.obr2_segment_zobraz,'Value',1)
set(handles.obr2_nezobraz,'Value',0)
set(handles.obr2_zprac_zobraz,'Value',0)


% --- Executes on button press in obr2_zprac_zobraz.
function obr2_zprac_zobraz_Callback(hObject, eventdata, handles)
% hObject    handle to obr2_zprac_zobraz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global zobraz_data2;
zobraz_data2=2;
% Hint: get(hObject,'Value') returns toggle state of obr2_zprac_zobraz

global obr_zprac;
axes(handles.figure_obr2);
imshow(obr_zprac);

%vsechny dalsi radio buttony vynuluj
set(handles.obr2_segment_zobraz,'Value',0)
set(handles.obr2_nezobraz,'Value',0)
set(handles.obr2_zprac_zobraz,'Value',1)


% --- Executes on button press in obr2_nezobraz.
function obr2_nezobraz_Callback(hObject, eventdata, handles)
% hObject    handle to obr2_nezobraz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global zobraz_data2;
zobraz_data2=3;
% Hint: get(hObject,'Value') returns toggle state of obr2_nezobraz

%vsechny dalsi radio buttony vynuluj
set(handles.obr2_segment_zobraz,'Value',0)
set(handles.obr2_nezobraz,'Value',1)
set(handles.obr2_zprac_zobraz,'Value',0)



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


% --- Executes during object creation, after setting all properties.
function nacteno_video_CreateFcn(hObject, eventdata, handles)
% hObject    handle to nacteno_video (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 Spust.
function Spust_Callback(hObject, eventdata, handles)
% spust video

global obr_nacti video_nacti nFrames startFrames stop zobraz_data1 zobraz_data2 obr_seg zobraz_orig obr_zprac;
global slozka; % promenne pro segmentaci;

% mazani rychlosti a nastaveni zacatku mereni
set(handles.rychlost_seg_multi,'string','0');
set(handles.rychlost_upr_multi,'string','0');
set(handles.rychlost_zprac_multi,'string','0');
set(handles.rychlost_rozp_multi,'string','0');
set(handles.rychlost_suma_multi,'string','0');
% mazani uspesnosti
set(handles.snimku_nerozpoznano,'string','0');

%nastaveni pomocnych promennych
rychlost_segmentace_multi=0;
rychlost_zpracovani_multi=0;
rychlost_rozpoznani_multi=0;
rychlost_upravy_celkem_multi=0;
nerozpoznano=0;
nerozpoznano_prubeh=0;

pom_nerozpoznano=0;
pom_rychlost=0;
pom_zobraz_gesto=0;
zobraz_gesto_snimek=7;
rychlost_multi_pocet=str2double(get(handles.rychlost_snimku,'string'));
nerozpoznano_pocet=str2double(get(handles.rozpoznani_snimku,'string'));

%v pripade ze se vytvari databaze gest je nutne vytvorit databazi z
%dostupnych obrazku
nevytvorit_databazi=get(handles.vytvor_databazi,'value');
if nevytvorit_databazi==0
nacti_databazi; %nacteni databaze gest - vytvoreni souboru vzorky.mat
disp('databaze se vytvari z dostupnych snimku');
else
disp('databaze se nacita z jednoho souboru');    
end
load('databaze/vzory.mat');
max_rozmer=350; %maximalni rozmer sirky a nebo vysky
video_nacti_res=video_nacti;
zobraz_orig=obr_nacti;
for k = startFrames : nFrames
    limit_rozliseni=get(handles.lim_rozliseni,'Value');
    pom_zobraz_gesto=pom_zobraz_gesto+1;
    pom_nerozpoznano=pom_nerozpoznano+1;
    pom_rychlost=pom_rychlost+1;
    fps_start=tic; %fps
    if nFrames==1
        if limit_rozliseni==1
            max_osa=max(size(obr_nacti));
            if max_osa>max_rozmer %prevzorkovani
                pomer_res = max_rozmer./max_osa;
                zobraz_orig = imresize(obr_nacti,pomer_res);
            end
        end
        zobraz_orig=im2double(zobraz_orig);
    else
        if limit_rozliseni==1
            max_osa=max(size(video_nacti(k).cdata));
            if max_osa>max_rozmer %prevzorkovani
                pomer_res = max_rozmer./max_osa;
                video_nacti_res(k).cdata = imresize(video_nacti(k).cdata,pomer_res);
            end
        end
        zobraz_orig=im2double(video_nacti_res(k).cdata); % do double by se kvuli uspore casu dalo prevest pri nacitani, avsak velke pametove naroky
    end
    if stop==1
        startFrames=k;
        break;
    end
    % filtrace pred segmentaci
    filtrace_predsegm=get(handles.medfiltrace_pred_segm,'value');
    filtrace_predsegm_okno=str2double(get(handles.medfiltrace_pred_segm_okno,'string'));
    tic
    if filtrace_predsegm==1
        maska=[filtrace_predsegm_okno filtrace_predsegm_okno];
        obr_seg=[];
        obr_seg(:,:,1)=medfilt2(zobraz_orig(:,:,1),maska);
        obr_seg(:,:,2)=medfilt2(zobraz_orig(:,:,2),maska);
        obr_seg(:,:,3)=medfilt2(zobraz_orig(:,:,3),maska);
    else obr_seg=zobraz_orig;
    end
    rychlost_pred_upravy=toc;
    % segmentace
    ladit_prahy=get(handles.slider_prah_segm,'value');
    tic
    obr_seg = segmentace( obr_seg, slozka, ladit_prahy);
    %vypocet rychlosti
    rychlost_segmentace=toc;
    rychlost_segmentace_multi=rychlost_segmentace_multi+rychlost_segmentace;
    if pom_rychlost==rychlost_multi_pocet
        set(handles.rychlost_seg_multi,'string',num2str(rychlost_segmentace_multi/pom_rychlost,'%.4f'));
    end
    set(handles.rychlost_seg,'string',num2str(rychlost_segmentace,'%.4f'));
    % postsegmentacni upravy
    
    objekty=get(handles.nejvetsi_objekt,'value');
    filtrace=get(handles.med_filtrace,'value');
    vel_maska=str2double(get(handles.med_filtrace_okno,'string'));
    uzavreni=get(handles.uzavreni,'value');
    vel_objekt=str2double(get(handles.uzavreni_objekt,'string'));
    vyplneni=get(handles.vyplneni,'value');
    tic
    [ obr_seg ] = uprava_segmentace( obr_seg, objekty, filtrace, vel_maska, uzavreni, vel_objekt, vyplneni );
    rychlost_po_upravy=toc;
    rychlost_upravy_celkem=rychlost_pred_upravy+rychlost_po_upravy;
    rychlost_upravy_celkem_multi=rychlost_upravy_celkem_multi+rychlost_upravy_celkem;
    if pom_rychlost==rychlost_multi_pocet
        set(handles.rychlost_upr_multi,'string',num2str(rychlost_upravy_celkem_multi/pom_rychlost,'%.4f'));
    end
    set(handles.rychlost_upr,'string',num2str(rychlost_upravy_celkem,'%.4f'));
    % zpracovani obrazku
    orez_koef=get(handles.mira_orezu,'value');
    rotace_koef=get(handles.mira_rotace,'value');
    roz_hamming=220; %pozadovane rozliseni pro hamming - na ctverec
    tic
    if get(handles.erod_objekt_prevz,'value')==1
        erod_prevz=0.5;
    else
        erod_prevz=1;
    end
    
    [ obr_zprac ] = analyza_orez( obr_seg, erod_prevz, orez_koef, rotace_koef,roz_hamming );
    %vypocet rychlosti
    rychlost_zpracovani=toc;
    rychlost_zpracovani_multi=rychlost_zpracovani_multi+rychlost_zpracovani;
    set(handles.rychlost_zprac,'string',num2str(rychlost_zpracovani,'%.4f'));
    if pom_rychlost==rychlost_multi_pocet
        set(handles.rychlost_zprac_multi,'string',num2str(rychlost_zpracovani_multi/pom_rychlost,'%.4f'));
        
    end
    % analyza
    tic
    [ nejbl_vzorek, nejbl_vzorek_zobraz, min_vzorek ] = hamming_sit( obr_zprac, databaze_gest );
    rychlost_rozpoznani=toc;
    rychlost_rozpoznani_multi=rychlost_rozpoznani_multi+rychlost_rozpoznani;
    set(handles.rychlost_rozp,'string',num2str(rychlost_rozpoznani,'%.4f'));
    set(handles.rychlost_suma,'string',num2str(rychlost_segmentace+rychlost_upravy_celkem+rychlost_zpracovani+rychlost_rozpoznani,'%.4f'));
    
    if pom_zobraz_gesto==1
        axes(handles.figure_gesto_rozp);
        imshow(nejbl_vzorek_zobraz); %zobrazeni rozpoznaneho gesta
    end
    if pom_zobraz_gesto==zobraz_gesto_snimek
        pom_zobraz_gesto=0;
    end
    if pom_rychlost==rychlost_multi_pocet
        set(handles.rychlost_rozp_multi,'string',num2str(rychlost_rozpoznani_multi/pom_rychlost,'%.4f'));
        set(handles.rychlost_suma_multi,'string',num2str((rychlost_segmentace_multi/pom_rychlost)+(rychlost_upravy_celkem_multi/pom_rychlost)+(rychlost_zpracovani_multi/pom_rychlost)+(rychlost_rozpoznani_multi/pom_rychlost),'%.4f'));
        
        rychlost_segmentace_multi=0;
        rychlost_rozpoznani_multi=0;
        rychlost_zpracovani_multi=0;
        rychlost_upravy_celkem_multi=0;
        pom_rychlost=0;
    end
    
    set(handles.hamming_min, 'string', num2str(min_vzorek));
    hamm_sen=get(handles.hamming_sensitivita,'value');
    if hamm_sen>min_vzorek
        nerozpoznano=0;
        set(handles.rozpoznane_gesto, 'string', num2str(nejbl_vzorek));
    else
        nerozpoznano=1;
        set(handles.rozpoznane_gesto, 'string', 'nerozpoznano');
    end
    nerozpoznano_prubeh=nerozpoznano_prubeh+nerozpoznano;
    if pom_nerozpoznano==nerozpoznano_pocet
    set(handles.snimku_nerozpoznano,'string',num2str(nerozpoznano_prubeh));
    %nastaveni pomocnych promennych na nulu
    pom_nerozpoznano=0;
    nerozpoznano_prubeh=0;
    nerozpoznano=0;
    end
    
    if zobraz_data1==1;
        axes(handles.figure_obr1);
        imshow(zobraz_orig);
    end
    if zobraz_data1==2;
        axes(handles.figure_obr1);
        imshow(obr_seg);
    end
    if zobraz_data1==3;
        pause(0.000001);
    end
        if zobraz_data2==1;
        axes(handles.figure_obr2);
        imshow(obr_seg);
    end
    if zobraz_data2==2;
        axes(handles.figure_obr2);
        imshow(obr_zprac);
    end
    if zobraz_data2==3;
        pause(0.000001);
    end
    
fps=toc(fps_start);
fps=(1/fps);
set(handles.text53,'string',num2str(fps,'%.2f'));    
end
stop=0;
if k==nFrames
startFrames=1;

end


% --- Executes on button press in Stop.
function Stop_Callback(hObject, eventdata, handles)
global stop;
stop = 1;

% hObject    handle to Stop (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 uloz_data2.
function uloz_data2_Callback(hObject, eventdata, handles)
global obr_zprac obr_seg ;
global zobraz_data2;
    if zobraz_data2==3
    msgbox(sprintf('Pro ulozeni obrazku prosim zvolte zobrazeni dat: segmentovana nebo zpracovana '),'Chyba ukladani')
    return;
    end
[file,path] = uiputfile('*.bmp','Save Workspace As');
if path~=0
    if zobraz_data2==1
cesta_soubor=[path,file];
imwrite(obr_seg,cesta_soubor);
    end
    if zobraz_data2==2
cesta_soubor=[path,file];
imwrite(obr_zprac,cesta_soubor);
    end
end

% --- Executes on button press in uloz_data1.
function uloz_data1_Callback(hObject, eventdata, handles)

global zobraz_orig obr_seg ;
global zobraz_data1;
    if zobraz_data1==3
    msgbox(sprintf('Pro ulozeni obrazku prosim zvolte zobrazeni dat: segmentovana nebo zpracovana '),'Chyba ukladani')
    return;
    end
[file,path] = uiputfile('*.bmp','Save Workspace As');
if path~=0
    if zobraz_data1==1
cesta_soubor=[path,file];
imwrite(zobraz_orig,cesta_soubor);
    end
    if zobraz_data1==2
cesta_soubor=[path,file];
imwrite(obr_seg,cesta_soubor);
    end
end


% --- Executes on button press in segm_ir.
function segm_ir_Callback(hObject, eventdata, handles)
global slozka; % promenne pro segmentaci;
slozka = 0;

% prepinani buttonu
set(handles.segm_ir,'Value',1)
set(handles.segm_r,'Value',0)
set(handles.segm_rgb,'Value',0)
set(handles.segm_cr,'Value',0)
set(handles.segm_cbcr,'Value',0)
set(handles.segm_rgb,'Value',0)
set(handles.segm_rcr,'Value',0)

% --- Executes on button press in segm_r.
function segm_r_Callback(hObject, eventdata, handles)
global slozka; % promenne pro segmentaci;
slozka = 1;
% prepinani buttonu
set(handles.segm_ir,'Value',0)
set(handles.segm_r,'Value',1)
set(handles.segm_rgb,'Value',0)
set(handles.segm_cr,'Value',0)
set(handles.segm_cbcr,'Value',0)
set(handles.segm_rcr,'Value',0)


% --- Executes on button press in segm_rgb.
function segm_rgb_Callback(hObject, eventdata, handles)
global slozka; % promenne pro segmentaci;
slozka = 2;
% prepinani buttonu
set(handles.segm_ir,'Value',0)
set(handles.segm_r,'Value',0)
set(handles.segm_rgb,'Value',1)
set(handles.segm_cr,'Value',0)
set(handles.segm_cbcr,'Value',0)
set(handles.segm_rcr,'Value',0)


% --- Executes on button press in segm_cr.
function segm_cr_Callback(hObject, eventdata, handles)
global slozka; % promenne pro segmentaci;
slozka = 3;
% prepinani buttonu
set(handles.segm_ir,'Value',0)
set(handles.segm_r,'Value',0)
set(handles.segm_rgb,'Value',0)
set(handles.segm_cr,'Value',1)
set(handles.segm_cbcr,'Value',0)
set(handles.segm_rcr,'Value',0)


% --- Executes on button press in segm_cbcr.
function segm_cbcr_Callback(hObject, eventdata, handles)
% prepinani buttonu
global slozka; % promenne pro segmentaci;
slozka = 4;
set(handles.segm_ir,'Value',0)
set(handles.segm_r,'Value',0)
set(handles.segm_rgb,'Value',0)
set(handles.segm_cr,'Value',0)
set(handles.segm_cbcr,'Value',1)
set(handles.segm_rcr,'Value',0)


% --- Executes on button press in segm_rcr.
function segm_rcr_Callback(hObject, eventdata, handles)
global slozka; % promenne pro segmentaci;
slozka = 5;
% prepinani buttonu
set(handles.segm_ir,'Value',0)
set(handles.segm_r,'Value',0)
set(handles.segm_rgb,'Value',0)
set(handles.segm_cr,'Value',0)
set(handles.segm_cbcr,'Value',0)
set(handles.segm_rcr,'Value',1)

% --- Executes on slider movement.
function mira_orezu_Callback(hObject, eventdata, handles)
% hObject    handle to mira_orezu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.mira_orezu_hodnota,'string',num2str(get(hObject,'Value')));
% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


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


% --- Executes on slider movement.
function hamming_sensitivita_Callback(hObject, eventdata, handles)
% hObject    handle to hamming_sensitivita (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.text_sensitivita, 'string', num2str(fix(get(hObject,'Value'))));
% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


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


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


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


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


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


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


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


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


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


% --- Executes on slider movement.
function slider_prah_segm_Callback(hObject, eventdata, handles)
ladit=get(hObject,'Value');
set(handles.text_prah_segm,'string',num2str(ladit));
% hObject    handle to slider_prah_segm (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


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


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



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


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


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


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


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



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


% --- Executes during object creation, after setting all properties.
function med_filtrace_okno_CreateFcn(hObject, eventdata, handles)
% hObject    handle to med_filtrace_okno (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 uzavreni_objekt_Callback(hObject, eventdata, handles)
% hObject    handle to uzavreni_objekt (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 uzavreni_objekt as text
%        str2double(get(hObject,'String')) returns contents of uzavreni_objekt as a double


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


% --- Executes on slider movement.
function mira_rotace_Callback(hObject, eventdata, handles)
% hObject    handle to mira_rotace (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.mira_rotace_hodnota,'string',num2str(get(hObject,'Value')));
% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


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



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


% --- Executes during object creation, after setting all properties.
function rychlost_snimku_CreateFcn(hObject, eventdata, handles)
% hObject    handle to rychlost_snimku (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 rozpoznani_snimku_Callback(hObject, eventdata, handles)
% hObject    handle to rozpoznani_snimku (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 rozpoznani_snimku as text
%        str2double(get(hObject,'String')) returns contents of rozpoznani_snimku as a double


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