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

% Last Modified by GUIDE v2.5 22-May-2012 23:47:07

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

set(handles.uipanel_hlavni,'Position',[15 15 1114 692]);
set(handles.uipanel_Make_Mask,'Position',[2015 15 1114 692]);
set(handles.uipanel_adv_info,'Position',[6015 15 1114 692]);
set(handles.menu_show_make,'Visible','on');

%handles.vstup_obraz = imread('obrazky/lena.png');
handles.vstup_obraz = imread('pictures/amy_lee.png');

%mask = imread('obrazky/lena_maska_text.png');
mask = imread('pictures/amy_lee_maska_01.png');

handles.maska = cast(mask,'logical');

handles.vystupni_maska = cast(handles.maska,'uint8');

vystupR = handles.vstup_obraz(:,:,1).*handles.vystupni_maska;
vystupG = handles.vstup_obraz(:,:,2).*handles.vystupni_maska;
vystupB = handles.vstup_obraz(:,:,3).*handles.vystupni_maska;

handles.oblast_Phi = cat(3, vystupR, vystupG, vystupB);
handles.vystup_obraz = cat(3, vystupR, vystupG, vystupB);

%axes(handles.axes_platno1);


imshow(handles.vystup_obraz,'Parent',handles.axes_vystup);
imshow(handles.vystup_obraz,'Parent',handles.axes4);





handles.n = 0;

handles.prvni_tvorba_masky = 0;
handles.mapa_koef_uroven = cell(1,2); % zobrazeni koeficientu
handles.mapa_koef_uroven{1,1} = 0;      % aktualni vytvorena uroven
handles.mapa_koef_uroven{1,2} = 0;      % zda byla pouzita colormapa jet
handles.ochrana_listbox_snizovani = 0;
handles.level_framelet = 4;
handles.level_contourlet = [1 2 4 4];
%imshow(maska);




% globalne definovane filtry:

handles.filtry = cell(5,1);

% banka filtru 1
h0=(sqrt(2)/32)*[-1,0,9,16,9,0,-1];
h1=(sqrt(2)/32)*[-1,-2,7,0,-7,2,1];
h2=[0.0104677975, 0.0370823430, 0.0651503417, 0.0897493772, -0.575618139, 0.3731682798, 0];
h3=[0, 0.0104677975, 0.0370823430, 0.0651503417, 0.0897493772, -0.575618139, 0.3731682798];

filters = cell(4,1);
filters{1,1} = h0;
filters{2,1} = h1;
filters{3,1} = h2;
filters{4,1} = h3;

handles.filtry{1,1} = filters;

% banka filtru 2
h0=(sqrt(2)/256)*[-5,-7,35,105,105,35,-7,-5];
h1=(sqrt(2)/256)*[-5,23,-13,-41,41,13,-23,5];
h2=[0.0422028276,0.0784808462,0.0274495253,-0.1272844093,-0.4611848140,0.5488035631,-0.1084675382, 0];
h3=[0, 0.0422028276,0.0784808462,0.0274495253,-0.1272844093,-0.4611848140,0.5488035631,-0.1084675382];

filters = cell(4,1);
filters{1,1} = h0;
filters{2,1} = h1;
filters{3,1} = h2;
filters{4,1} = h3;

handles.filtry{2,1} = filters;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % výběr zobrazení pomocí radiobutton
% set(handles.uipanel_vyber,'SelectionChangeFcn',@uipanel_vyber_SelectionChangeFcn);
% set(handles.radiobutton_obraz,'Visible','off');
% set(handles.radiobutton_maska,'Visible','off');
% set(handles.radiobutton_Phi,'Visible','off');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



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

% Update handles structure
guidata(hObject, handles);
% UIWAIT makes aplikace_metody wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = aplikace_metody_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 radiobutton_obraz.
function radiobutton_obraz_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton_obraz (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 radiobutton_obraz



% --- 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)


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


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


% --- Executes when selected object is changed in uipanel_vyber.
% function uipanel_vyber_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel_vyber 
% 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)
% 
% handles = guidata(hObject); 
% 
% switch get(eventdata.NewValue,'Tag')
%     case 'radiobutton_obraz'
%       %execute this code when fontsize08_radiobutton is selected
%       imshow(handles.vstup_obraz,'Parent',handles.axes_platno1);
% 
%     case 'radiobutton_maska'
%       %execute this code when fontsize12_radiobutton is selected
%       imshow(handles.maska,'Parent',handles.axes_platno1);
% 
%     case 'radiobutton_Phi'
%       %execute this code when fontsize16_radiobutton is selected
%       imshow(handles.oblast_Phi,'Parent',handles.axes_platno1);
%       
%     otherwise
%        % Code for when there is no match.
% 
% end
% %updates the handles structure
% guidata(hObject, handles);


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


% --------------------------------------------------------------------
function Untitled_2_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_2 (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 btn_vyber_masku.
function btn_vyber_masku_Callback(hObject, eventdata, handles)
% hObject    handle to btn_vyber_masku (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
menu_vyb_mask_Callback(hObject, eventdata, handles);




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


%velikost_obrazku = size(handles.vstup_obraz);
%velikost_masky = size(handles.maska);

%handles.n = handles.n + 1;
%set(handles.output_line1,'String',num2str(handles.n));
%guidata(hObject, handles);
 
%set(handles.btn_spustit_vypocet,'Visible','off');
%set(handles.axes_vystup,'Position',[1083, 4, 100, 42]);
%set(handles.btn_vyber_masku,'Visible','off');
%set(handles.btn_vyber_obrazek,'Visible','off');



%velikost_obrazku = size(handles.vstup_obraz);
%handles.cislo = 2;
%set(handles.output_line1,'String',num2str(velikost_obrazku(1)));


%f = figure('Visible','off','Position',[360 500 800 600]);
% f = figure('Visible','off','Position',[360,500,450,285]);
% 
%    
%    %  Construct the components.
%    hsurf = uicontrol('Style','pushbutton','String','Surf',...
%            'Position',[315,220,70,25]);
%    hmesh = uicontrol('Style','pushbutton','String','Mesh',...
%            'Position',[315,180,70,25]);
%    hcontour = uicontrol('Style','pushbutton',...
%            'String','Countour',...
%            'Position',[315,135,70,25]); 
%    htext = uicontrol('Style','text','String','Select Data',...
%            'Position',[325,90,60,15]);
%    hpopup = uicontrol('Style','popupmenu',...
%            'String',{'Peaks','Membrane','Sinc'},...
%            'Position',[300,50,100,25]);
%    ha = axes('Units','Pixels','Position',[50,60,200,185]); 
%    align([hsurf,hmesh,hcontour,htext,hpopup],'Center','None');
%    
%    %Make the GUI visible.
%    set(f,'Visible','on')
% hold on;
% oblast = mousetrack2;
% set(handles.output_line1,'String',num2str(oblast));

menu_make_mask_Callback(hObject, eventdata, handles);









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


vypis = cell(5,1);
vypis{2,1} = 'Výpočet zahájen, prosím o trpělivost.';
vypis{4,1} = 'Doba trvání výpočtu závisí na počtu iterací';
vypis{5,1} = 'a na použitém algoritmu.';
set(handles.output_line1,'String',vypis);


handles.firstRed = handles.vstup_obraz(:,:,1).*handles.vystupni_maska;
handles.firstGreen = handles.vstup_obraz(:,:,2).*handles.vystupni_maska;
handles.firstBlue = handles.vstup_obraz(:,:,3).*handles.vystupni_maska;

handles.pokracovani_vypoctu = 0;

guidata(hObject, handles);

spustit_vypocet(hObject, eventdata, handles);





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

vypis = cell(6,1);
vypis{2,1} = 'Bylo zahájeno pokračování výpočtu, prosím o trpělivost.';
vypis{4,1} = 'Doba trvání výpočtu závisí na počtu iterací';
vypis{5,1} = 'a na použitém algoritmu.';
set(handles.output_line1,'String',vypis);

handles.firstRed = handles.vystup_obraz(:,:,1);
handles.firstGreen = handles.vystup_obraz(:,:,2);
handles.firstBlue = handles.vystup_obraz(:,:,3);

handles.pokracovani_vypoctu = 1;

guidata(hObject, handles);

spustit_vypocet(hObject, eventdata, handles);



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


% pro moznost pokracovat ve vypoctu
inRed = handles.firstRed;
inGreen = handles.firstGreen;
inBlue = handles.firstBlue;

thre = str2double(get(handles.edit_thre,'String'));
pocet_iteraci = str2double(get(handles.edit_iterace,'String'));
snizovani_thre =get(handles.checkbox_snizovani,'Value');
handles.vystupni_obraz = handles.vystup_obraz;



% zaznamenan prubeh algoritmu z hlediska snizovani prahu
% vysledek bude nakonec zobrazen v listboxu formulare Pokrocile informace
handles.prubeh_snizovani_thre = zeros(pocet_iteraci,1);
prubeh_thre = cell(pocet_iteraci,1);
handles.pozice_prubehu_snizovani = 1;
handles.vysledky_obraz_snizovani = cell(pocet_iteraci,1);
delta_thre = str2double(get(handles.edit_snizovani_prahu,'String'));
jestli_snizit = 0; % snizi se thre az po x stejnych PSNR

PSNR_data = zeros(pocet_iteraci+1,1); % pro ukladani PSNR vsech iteraci
PSNR_data(1,1) = 0; % kvuli prvni iteraci

ochrana_contourlet = 0;

guidata(hObject, handles);



framelet = get(handles.radiobtn_framelet,'Value');
if framelet == 1
    typ_algoritmu = 1;
else
    typ_algoritmu = 2;
end


tic;


% vyber ze dvou aplikovanych zpusobu vypoctu
%   1. frameletova transformace
%   2. contourlet transformace pomoci Contourlet_Toolbox
switch typ_algoritmu
    case 1       
       
        banka_filtru = get(handles.popup_filtry,'Value');
        filters = handles.filtry{banka_filtru,1};
        


% %         h0=(1/16)*[1,4,6,4,1];
% %         h1=(1/8)*[1,2,0,-2,-1];
% %         h2=(sqrt(6)/16)*[-1,0,2,0,-1];
% %         h3=(1/8)*[-1,2,0,-2,1];
% %         h4=(1/16)*[1,-4,6,-4,1];
% % 
% %         filters = cell(5,1);
% %         filters{1,1} = h0;
% %         filters{2,1} = h1;
% %         filters{3,1} = h2;
% %         filters{4,1} = h3;
% %         filters{5,1} = h4;

        progress = waitbar(0, 'Vyckejte prosim ...');

        pocet_filtru = length(filters);
        ur_trans = str2double(get(handles.edit_level,'String'));


        for i=1:pocet_iteraci

            koeficienty = rozklad(inRed,filters,ur_trans);
            [vektor_pred,Struct] = koeficient_na_vektor(koeficienty,ur_trans,pocet_filtru);
            Cr = vektor_pred/max(max(vektor_pred));
            y = threshold(Cr,thre,'soft');
            vektor_po = y.*(max(max(vektor_pred)));
            upr_koeficienty = vektor_na_koeficient(vektor_po,Struct,pocet_filtru);
            XR = kompozice(inRed,upr_koeficienty,filters,ur_trans);

            koeficienty = rozklad(inGreen,filters,ur_trans);
            [vektor_pred,Struct] = koeficient_na_vektor(koeficienty,ur_trans,pocet_filtru);
            Cr = vektor_pred/max(max(vektor_pred));
            y = threshold(Cr,thre,'soft');
            vektor_po = y.*(max(max(vektor_pred)));
            upr_koeficienty = vektor_na_koeficient(vektor_po,Struct,pocet_filtru);
            XG = kompozice(inGreen,upr_koeficienty,filters,ur_trans);

            koeficienty = rozklad(inBlue,filters,ur_trans);
            [vektor_pred,Struct] = koeficient_na_vektor(koeficienty,ur_trans,pocet_filtru);
            Cr = vektor_pred/max(max(vektor_pred));
            y = threshold(Cr,thre,'soft');
            vektor_po = y.*(max(max(vektor_pred)));
            upr_koeficienty = vektor_na_koeficient(vektor_po,Struct,pocet_filtru);
            XB = kompozice(inBlue,upr_koeficienty,filters,ur_trans);




            missingR = (uint8(XR)).*(uint8(~handles.vystupni_maska));
            missingG = (uint8(XG)).*(uint8(~handles.vystupni_maska));
            missingB = (uint8(XB)).*(uint8(~handles.vystupni_maska));

             % puvodni zpusob
            vystupR = handles.vstup_obraz(:,:,1).*handles.vystupni_maska + missingR;
            vystupG = handles.vstup_obraz(:,:,2).*handles.vystupni_maska + missingG;
            vystupB = handles.vstup_obraz(:,:,3).*handles.vystupni_maska + missingB;       

            handles.vystup_obraz = cat(3, vystupR, vystupG, vystupB);
            guidata(hObject, handles);

            inRed = vystupR;
            inGreen = vystupG;
            inBlue = vystupB;
            
            
            % nasleduje nastaveni postupneho snizovani thre
            if snizovani_thre == 1
            
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
                % vypocet PSNR
%                 Ar = handles.vstup_obraz(:,:,1)/255;
%                 Br = handles.vystup_obraz(:,:,1)/255;
% 
%                 Ag = handles.vstup_obraz(:,:,2)/255;
%                 Bg = handles.vystup_obraz(:,:,2)/255;
% 
%                 Ab = handles.vstup_obraz(:,:,3)/255;
%                 Bb = handles.vystup_obraz(:,:,3)/255;
%
%                 mean_sq_err = mean(mean(((Ar + Ag + Ab - Br -Bg - Bb)/3).^2));
                mean_sq_err = mean(mean(((handles.vstup_obraz(:,:,1)/255 + handles.vstup_obraz(:,:,2)/255 ...
                    + handles.vstup_obraz(:,:,3)/255 - handles.vystup_obraz(:,:,1)/255 - handles.vystup_obraz(:,:,2)/255 ...
                    - handles.vystup_obraz(:,:,3)/255)/3).^2));
                
                psnr_dB = 20*log10(1/sqrt(mean_sq_err));

                % konec vypoctu PSNR
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                
                PSNR_data(i,1) = psnr_dB;
                
                if i == 1
                    prubeh_thre{handles.pozice_prubehu_snizovani,1} = [num2str(i), ... 
                        '                       ',num2str(thre),'                       ', num2str(psnr_dB)];
                    
                    handles.prubeh_snizovani_thre(handles.pozice_prubehu_snizovani,1) = i;
                    handles.prubeh_snizovani_thre(handles.pozice_prubehu_snizovani,2) = thre;
                    handles.prubeh_snizovani_thre(handles.pozice_prubehu_snizovani,3) = psnr_dB;
                    handles.vysledky_obraz_snizovani{handles.pozice_prubehu_snizovani,1} = handles.vystup_obraz;
                    guidata(hObject, handles);
                    handles.pozice_prubehu_snizovani = handles.pozice_prubehu_snizovani + 1;
                    pred_thre = thre;

                else
                             
                    pred = PSNR_data(i-1,1);
                    ted = PSNR_data(i,1);
                    
                    if pred == ted
                        
                        if ted > 0.0001
                        
                            mezivypocet = thre/delta_thre;
                            pred_thre = thre;
                            thre = thre - mezivypocet;

                            handles.prubeh_snizovani_thre(handles.pozice_prubehu_snizovani,1) = i;
                            handles.prubeh_snizovani_thre(handles.pozice_prubehu_snizovani,2) = thre;
                            handles.prubeh_snizovani_thre(handles.pozice_prubehu_snizovani,3) = psnr_dB;

    %                         handles.prubeh_snizovani_thre{pozice_prubehu_snizovani+1,1} = [num2str(i),num2str(thre), num2str(psnr_dB)];
                            prubeh_thre{handles.pozice_prubehu_snizovani,1} = [num2str(i), ... 
                                '                       ',num2str(thre),'                       ', num2str(psnr_dB)];

                            handles.vysledky_obraz_snizovani{handles.pozice_prubehu_snizovani,1} = handles.vystup_obraz;

                            handles.pozice_prubehu_snizovani = handles.pozice_prubehu_snizovani + 1;
                            guidata(hObject, handles);
                            
                            jestli_snizit = 0;
                                                
                        
                        elseif ted <= 0.001 && jestli_snizit == 5   % thre bude snizeno, bude li 6x za sebou PSNR stejne
                            mezivypocet = thre/delta_thre;
                            pred_thre = thre;
                            thre = thre - mezivypocet;

                            handles.prubeh_snizovani_thre(handles.pozice_prubehu_snizovani,1) = i;
                            handles.prubeh_snizovani_thre(handles.pozice_prubehu_snizovani,2) = thre;
                            handles.prubeh_snizovani_thre(handles.pozice_prubehu_snizovani,3) = psnr_dB;

    %                         handles.prubeh_snizovani_thre{pozice_prubehu_snizovani+1,1} = [num2str(i),num2str(thre), num2str(psnr_dB)];
                            prubeh_thre{handles.pozice_prubehu_snizovani,1} = [num2str(i), ... 
                                '                       ',num2str(thre),'                       ', num2str(psnr_dB)];

                            handles.vysledky_obraz_snizovani{handles.pozice_prubehu_snizovani,1} = handles.vystup_obraz;

                            handles.pozice_prubehu_snizovani = handles.pozice_prubehu_snizovani + 1;
                            guidata(hObject, handles);
                            
                            jestli_snizit = 0;
                        
                        else
                            jestli_snizit = jestli_snizit + 1;
                        end
                    else
                        jestli_snizit = 0;                        
                    end
                end
                    
                handles.ochrana_listbox_snizovani = 1;  
            else
                
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
                % vypocet PSNR

                mean_sq_err = mean(mean(((handles.vstup_obraz(:,:,1)/255 + handles.vstup_obraz(:,:,2)/255 ...
                    + handles.vstup_obraz(:,:,3)/255 - handles.vystup_obraz(:,:,1)/255 - handles.vystup_obraz(:,:,2)/255 ...
                    - handles.vystup_obraz(:,:,3)/255)/3).^2));
                
                psnr_dB = 20*log10(1/sqrt(mean_sq_err));

                % konec vypoctu PSNR
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                pred_thre = thre;
               
                
            end
            
            
            imshow(handles.vystup_obraz,'Parent',handles.axes_vystup);
        
            % vypis informaci o prubehu vypoctu
            if handles.pokracovani_vypoctu == 0
                
                vypis = cell(6,1);
                vypis{1,1} = 'Probíhá výpočet, prosím o trpělivost.';
                vypis{3,1} = 'Framelet transformace zabírá více času, doba trvání výpočtu také závisí na počtu iterací';
                vypis{5,1} = 'Aktuální informace:';
                vypis{6,1} = ['Iterace = ',num2str(i),',    PSNR = ', num2str(psnr_dB),' dB,    práh = ', num2str(pred_thre)];           

                set(handles.output_line1,'String',vypis);                 
                
            else   
                vypis = cell(6,1);
                vypis{1,1} = 'Probíhá pokračování předchozího výpočtu, prosím o trpělivost.';
                vypis{3,1} = 'Framelet transformace zabírá více času, doba trvání výpočtu také závisí na počtu iterací';
                vypis{5,1} = 'Aktuální informace:';
                vypis{6,1} = ['Iterace = ',num2str(i),',    PSNR = ', num2str(psnr_dB),' dB,    práh = ', num2str(pred_thre)];
                
                set(handles.output_line1,'String',vypis);        

            end

            waitbar(i/pocet_iteraci,progress);
        end
        close(progress);
        
        
    case 2
        path(path,[cd,'/contourlet_toolbox_tight']);
        Pyr_mode = 2;
        dfilt = 'pkva';
        smooth_func = @rcos;
        
        %[navysku,nasirku] = size(inRed);
        navysku = 1;
        nasirku = 1;
        
        
        if navysku == nasirku
            ochrana_contourlet = 0;
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            % nacteni nlev_SD
              xx = get(handles.edit_level,'String');
              z = cast(xx,'double');
              delka = length(z);
              pozice = 1;
              cisel = 0;

              for j = 1:delka
                  if z(1,j) > 47 && z(1,j) < 58
                      cisel = cisel + 1;
                  end
              end


              xxx = zeros(1,cisel);

              for j = 1:delka
                  if z(1,j) > 47 && z(1,j) < 58
                      xxx(1,pozice) = z(1,j)-48;
                      pozice = pozice + 1;
                  end
              end

             % konec bloku nacteni nlev_SD
             %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

            nlev_SD = xxx;
            %nlev_SD = [5 4 4 3 3];
            %nlev_SD = [3 3 3 3];
            %nlev_SD = [1 2 4 4];


            progress = waitbar(0, 'Vyckejte prosim ...');

            for i=1:pocet_iteraci

                yy = ContourletSDDec(double(inRed), nlev_SD, Pyr_mode, smooth_func, dfilt); % dekompozice pomoci transformace ContourletSD
                [d, S] = pdfb2vec(yy);  % Prevedeni matic koeficientu do sloupcoveho vektoru
                Cr = d/max(max(d));
                y = threshold(Cr,thre,'soft');
                CSS = y.*(max(max(d)));
                yyy = vec2pdfb(CSS, S);
                XR = ContourletSDRec(yyy, Pyr_mode, smooth_func, dfilt);

                yy = ContourletSDDec(double(inGreen), nlev_SD, Pyr_mode, smooth_func, dfilt);
                [d, S] = pdfb2vec(yy); 
                Cr = d/max(max(d));
                y = threshold(Cr,thre,'soft');
                CSS = y.*(max(max(d)));
                yyy = vec2pdfb(CSS, S);
                XG = ContourletSDRec(yyy, Pyr_mode, smooth_func, dfilt);

                yy = ContourletSDDec(double(inBlue), nlev_SD, Pyr_mode, smooth_func, dfilt);
                [d, S] = pdfb2vec(yy);
                Cr = d/max(max(d));
                y = threshold(Cr,thre,'soft');
                CSS = y.*(max(max(d)));
                yyy = vec2pdfb(CSS, S);
                XB = ContourletSDRec(yyy, Pyr_mode, smooth_func, dfilt);


                missingR = (uint8(XR)).*(uint8(~handles.vystupni_maska));
                missingG = (uint8(XG)).*(uint8(~handles.vystupni_maska));
                missingB = (uint8(XB)).*(uint8(~handles.vystupni_maska));

                 % puvodni zpusob
                vystupR = handles.vstup_obraz(:,:,1).*handles.vystupni_maska + missingR;
                vystupG = handles.vstup_obraz(:,:,2).*handles.vystupni_maska + missingG;
                vystupB = handles.vstup_obraz(:,:,3).*handles.vystupni_maska + missingB;       

                handles.vystup_obraz = cat(3, vystupR, vystupG, vystupB);
                guidata(hObject, handles);

                inRed = vystupR;
                inGreen = vystupG;
                inBlue = vystupB;

                if snizovani_thre == 1

                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
                    % vypocet PSNR

                    mean_sq_err = mean(mean(((handles.vstup_obraz(:,:,1)/255 + handles.vstup_obraz(:,:,2)/255 ...
                        + handles.vstup_obraz(:,:,3)/255 - handles.vystup_obraz(:,:,1)/255 - handles.vystup_obraz(:,:,2)/255 ...
                        - handles.vystup_obraz(:,:,3)/255)/3).^2));

                    psnr_dB = 20*log10(1/sqrt(mean_sq_err));

                    % konec vypoctu PSNR
                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

                    PSNR_data(i,1) = psnr_dB; 

                    if i == 1
                        prubeh_thre{handles.pozice_prubehu_snizovani,1} = [num2str(i), ... 
                            '                       ',num2str(thre),'                       ', num2str(psnr_dB)];

                        handles.prubeh_snizovani_thre(handles.pozice_prubehu_snizovani,1) = i;
                        handles.prubeh_snizovani_thre(handles.pozice_prubehu_snizovani,2) = thre;
                        handles.prubeh_snizovani_thre(handles.pozice_prubehu_snizovani,3) = psnr_dB;
                        handles.vysledky_obraz_snizovani{handles.pozice_prubehu_snizovani,1} = handles.vystup_obraz;
                        pred_thre = thre;
                        guidata(hObject, handles);
                        handles.pozice_prubehu_snizovani = handles.pozice_prubehu_snizovani + 1;

                    else

                        pred = PSNR_data(i-1,1);
                        ted = PSNR_data(i,1);

                        if pred == ted
                            
                            if jestli_snizit == 5

                                mezivypocet = thre/delta_thre;
                                pred_thre = thre;
                                thre = thre - mezivypocet;

                                handles.prubeh_snizovani_thre(handles.pozice_prubehu_snizovani,1) = i;
                                handles.prubeh_snizovani_thre(handles.pozice_prubehu_snizovani,2) = thre;
                                handles.prubeh_snizovani_thre(handles.pozice_prubehu_snizovani,3) = psnr_dB;

        %                         handles.prubeh_snizovani_thre{pozice_prubehu_snizovani+1,1} = [num2str(i),num2str(thre), num2str(psnr_dB)];
                                prubeh_thre{handles.pozice_prubehu_snizovani,1} = [num2str(i), ... 
                                    '                       ',num2str(thre),'                       ', num2str(psnr_dB)];

                                handles.vysledky_obraz_snizovani{handles.pozice_prubehu_snizovani,1} = handles.vystup_obraz;

                                handles.pozice_prubehu_snizovani = handles.pozice_prubehu_snizovani + 1;
                                guidata(hObject, handles);
                                jestli_snizit = 0;
                            else
                                jestli_snizit = jestli_snizit + 1;
                            end
                        else
                            jestli_snizit = 0;
                        end
                    end                

                    handles.ochrana_listbox_snizovani = 1;

                else

                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
                    % vypocet PSNR

                    mean_sq_err = mean(mean(((handles.vstup_obraz(:,:,1)/255 + handles.vstup_obraz(:,:,2)/255 ...
                        + handles.vstup_obraz(:,:,3)/255 - handles.vystup_obraz(:,:,1)/255 - handles.vystup_obraz(:,:,2)/255 ...
                        - handles.vystup_obraz(:,:,3)/255)/3).^2));

                    psnr_dB = 20*log10(1/sqrt(mean_sq_err));

                    % konec vypoctu PSNR
                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                    pred_thre = thre;



                end


                imshow(handles.vystup_obraz,'Parent',handles.axes_vystup);

                % vypis informaci o prubehu vypoctu
                if handles.pokracovani_vypoctu == 0

                    vypis = cell(6,1);
                    vypis{1,1} = 'Probíhá výpočet, prosím o trpělivost.';
                    vypis{3,1} = 'Contourlet transformace je vcelku rychlá, doba trvání výpočtu závisí na počtu iterací';
                    vypis{5,1} = 'Aktuální informace:';
                    vypis{6,1} = ['Iterace = ',num2str(i),',    PSNR = ', num2str(psnr_dB),' dB,    práh = ', num2str(pred_thre)];           

                    set(handles.output_line1,'String',vypis);                 

                else   
                    vypis = cell(6,1);
                    vypis{1,1} = 'Probíhá pokračování předchozího výpočtu, prosím o trpělivost.';
                    vypis{3,1} = 'Contourlet transformace je vcelku rychlá, doba trvání výpočtu závisí na počtu iterací';       
                    vypis{5,1} = 'Aktuální informace:';
                    vypis{6,1} = ['Iterace = ',num2str(i),',    PSNR = ', num2str(psnr_dB),' dB,    práh = ', num2str(pred_thre)];

                    set(handles.output_line1,'String',vypis);        

                end       

                waitbar(i/pocet_iteraci,progress);            
            end

            close(progress);
            
        else
            
            ochrana_contourlet = 1;
            
        end
        
    otherwise
end
        
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
    % vypocet PSNR
    Ar = handles.vstup_obraz(:,:,1)/255;
    Br = handles.vystup_obraz(:,:,1)/255;

    Ag = handles.vstup_obraz(:,:,2)/255;
    Bg = handles.vystup_obraz(:,:,2)/255;

    Ab = handles.vstup_obraz(:,:,3)/255;
    Bb = handles.vystup_obraz(:,:,3)/255;
    
    mean_sq_err = mean(mean(((Ar + Ag + Ab - Br -Bg - Bb)/3).^2));
    psnr_dB = 20*log10(1/sqrt(mean_sq_err));
    
    % konec vypoctu PSNR
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if ochrana_contourlet == 0    
    
    doba_vypoctu = toc;
    sekundy = rem(doba_vypoctu,60);
    minuty = (doba_vypoctu-sekundy)/60;
    vypis = cell(6,1);
    vypis{2,1} = 'Výpočet dokončen.';
    vypis{4,1} = ['Doba výpočtu:  ', num2str(minuty), ' min.  ', num2str(sekundy), ' sec.'];
    format bank;
    vypis{6,1} = ['PSNR = +', num2str(psnr_dB), ' dB'];
    set(handles.btn_pokracovat_vypocet,'Enable','on');
    set(handles.btn_uloz_vysledek,'Enable','on');
    set(handles.output_line1,'String',vypis);
    set(handles.listbox_snizovani,'String',prubeh_thre);
    set(handles.btn_uloz_vypis,'Enable','on');
    set(handles.btn_uloz_vypis_obr,'Enable','on');
    
else
    vypis = cell(6,1);
    vypis{3,1} = 'Contourlet toolbox nyní nepodporuje obdélníkové obrazy.';       
    vypis{4,1} = 'Vložte prosím čtvercový vstupní obraz a k němu pasující masku.';
    vypis{6,1} = 'Výpočet byl přerušen!';

    set(handles.output_line1,'String',vypis); 
end
   



%imshow(missingR,'Parent',handles.axes_platno1);
imshow(handles.vystup_obraz,'Parent',handles.axes_vystup);
imshow(handles.vystup_obraz,'Parent',handles.axes4);

set(handles.pop_zobrazeni,'Value',4.0);
%imshow(vystupR,'Parent',handles.axes_vystup);

%set(handles.output_line1,'String','done');

guidata(hObject, handles);



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Hlavní Menu

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


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


% --------------------------------------------------------------------
function menu_vyb_obr_Callback(hObject, eventdata, handles)
% hObject    handle to menu_vyb_obr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%set(handles.output_line1,'String','done');
%handle = @btn_vyber_obrazek_Callback;

[file_name, mach_path] = uigetfile({'*.png','obrazky ve formatu PNG';...
    '*.jpg','obrazky ve formatu JPG';'*.*','vsechny soubory'},'Select File');

 if isequal([file_name,mach_path],[0,0])
        return
        
 % Otherwise construct the fullfilename and Check and load the file
 else
     
        
        % pred overenim shody rozmeru masky
        set(handles.btn_pokracovat_vypocet,'Enable','off');
        set(handles.btn_spustit_vypocet,'Enable','off');
     
     
        delka_nazvu = length(file_name);
        
       
        for i=1:delka_nazvu
            nazev_souboru(i)=file_name(1,i);
        end

        delka_cesty = length(mach_path);

        for i=1:delka_cesty
            nazev_cesty(i) = mach_path(1,i);
        end
        
        soubor_obrazku = strcat(nazev_cesty,nazev_souboru);
        
      %  set(handles.output_line1,'String',soubor_obrazku);
        
        handles.vstup_obraz = imread(soubor_obrazku);
        guidata(hObject, handles);
        
        
        set(handles.pop_zobrazeni,'Value',1.0);
        imshow(handles.vstup_obraz,'Parent',handles.axes_vystup);
        
        
        % nasleduje prepocitani oblasti Phi
        
        velikost_obrazku = size(handles.vstup_obraz);
        velikost_masky = size(handles.maska);

        if isequal([velikost_obrazku(1),velikost_obrazku(2)],[velikost_masky(1),velikost_masky(2)])
           
            
            vystupR = handles.vstup_obraz(:,:,1).*handles.vystupni_maska;
            vystupG = handles.vstup_obraz(:,:,2).*handles.vystupni_maska;
            vystupB = handles.vstup_obraz(:,:,3).*handles.vystupni_maska;

            handles.oblast_Phi = cat(3, vystupR, vystupG, vystupB);
            handles.vystup_obraz = handles.oblast_Phi;
            
            set(handles.output_line1,'String','Vstupní obraz a maska mají shodnou velikost.');
            
            % maska se rozmerove shoduje s obrazem, povolen vypocet
            set(handles.btn_spustit_vypocet,'Enable','on'); 
            
            imshow(handles.oblast_Phi,'Parent',handles.axes_vystup);
            imshow(handles.oblast_Phi,'Parent',handles.axes4);

        else
            [row,col] = size(handles.vstup_obraz(:,:,1));
            vypis = cell(5,1);
            vypis{1,1} = 'Rozměry vstupního obrazu a masky se liší.';
            vypis{3,1} = 'Prosím vložte masku.';
            vypis{5,1} = ['Rozměry vstupního obrazu jsou: ',num2str(col),'x',num2str(row)];
            set(handles.output_line1,'String',vypis);

        end
        
        guidata(hObject, handles);
   
        set(handles.uipanel_hlavni,'Position',[15 15 1114 692]);
        set(handles.uipanel_Make_Mask,'Position',[2015 15 1114 692]);
        set(handles.uipanel_adv_info,'Position',[6015 15 1114 692]);       
 end
 
 guidata(hObject, handles);

%Create date and time labels
%guidata(h,handles);
%file_name=handles.fn;
%filename=file_name;
%length_fn=length(filename);
%st_pt=length_fn+1-4;
%file_ext=filename(st_pt:length_fn);
%mpath=mach_path;
%handles.pathloc=[mpath file_name];

%set(handles.output_line1,'String',handles.pathloc');



% --------------------------------------------------------------------
function menu_vyb_mask_Callback(hObject, eventdata, handles)
% hObject    handle to menu_vyb_mask (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[file_name, mach_path] = uigetfile({'*.png','obrazky ve formatu PNG';...
    '*.*','vsechny soubory'},'Select File');

 if isequal([file_name,mach_path],[0,0])
        return
        
 % Otherwise construct the fullfilename and Check and load the file
 else
     
     
        % pred overenim shody rozmeru masky
        set(handles.btn_pokracovat_vypocet,'Enable','off');
        set(handles.btn_spustit_vypocet,'Enable','off');
     
     
     
        delka_nazvu = length(file_name);
        
        set(handles.output_line1,'String',file_name');

        for i=1:delka_nazvu
            nazev_souboru(i)=file_name(1,i);
        end

        delka_cesty = length(mach_path);

        for i=1:delka_cesty
            nazev_cesty(i) = mach_path(1,i);
        end
        
        soubor_obrazku = strcat(nazev_cesty,nazev_souboru);
        
        set(handles.output_line1,'String',soubor_obrazku);
        
       % nacte se nova maska 
        mask = imread(soubor_obrazku);
        
        handles.maska = cast(mask,'logical');

        handles.vystupni_maska = cast(handles.maska,'uint8');
        
        guidata(hObject, handles);
        
        
       % prepne zobrazeni na masku 
        %set(handles.pop_zobrazeni,'Value',2.0);
        %imshow(handles.maska,'Parent',handles.axes_vystup);
        
        
       % nasleduje prepocitani oblasti Phi       
        velikost_obrazku = size(handles.vstup_obraz);
        velikost_masky = size(handles.maska);

        if isequal([velikost_obrazku(1),velikost_obrazku(2)],[velikost_masky(1),velikost_masky(2)])
           
            
            vystupR = handles.vstup_obraz(:,:,1).*handles.vystupni_maska;
            vystupG = handles.vstup_obraz(:,:,2).*handles.vystupni_maska;
            vystupB = handles.vstup_obraz(:,:,3).*handles.vystupni_maska;

            handles.oblast_Phi = cat(3, vystupR, vystupG, vystupB);
            handles.vystup_obraz = handles.oblast_Phi;

            set(handles.output_line1,'String','Maska sedi');
            
            % maska se rozmerove shoduje s obrazem, povolen vypocet
            set(handles.btn_spustit_vypocet,'Enable','on'); 
            
            set(handles.pop_zobrazeni,'Value',3.0);
            imshow(handles.oblast_Phi,'Parent',handles.axes_vystup);
            imshow(handles.oblast_Phi,'Parent',handles.axes4);

        else
            [row,col] = size(handles.vstup_obraz(:,:,1));
            vypis = cell(5,1);
            vypis{1,1} = 'Rozměry vstupního obrazu a masky se liší.';
            vypis{3,1} = 'Prosím vložte masku.';
            vypis{5,1} = ['Rozměry vstupního obrazu jsou: ',num2str(col),'x',num2str(row)];
            set(handles.output_line1,'String',vypis);

        end
       
        
        
        guidata(hObject, handles);
        
    set(handles.uipanel_hlavni,'Position',[15 15 1114 692]);
    set(handles.uipanel_Make_Mask,'Position',[2015 15 1114 692]);
    set(handles.uipanel_adv_info,'Position',[6015 15 1114 692]);
 end
 

% --------------------------------------------------------------------
function menu_make_mask_Callback(hObject, eventdata, handles)              % menu tvorba masky - nova maska
% hObject    handle to menu_make_mask (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%%% set(handles.menu_show_make,'Visible','on');
handles.prvni_tvorba_masky = 1;
set(handles.outline_2,'String','Počet oblastí:  0');
% zobrazi se panel tvorby masky a nacte se vychozi obrazek
set(handles.uipanel_hlavni,'Position',[2015 15 1114 692]);
set(handles.uipanel_Make_Mask,'Position',[15 15 1114 692]);
imshow(handles.vstup_obraz,'Parent',handles.axes_M_Mask);

% jedna se o novou masku, proto je potreba osetrit tlacitka
set(handles.btn_MM_zpet,'Enable','off');
set(handles.btn_MMmake_mask,'Enable','off');
set(handles.btn_MM_uloz,'Enable','off');
set(handles.btn_MM_pouzij,'Enable','off');

handles.M_M_oblasti = cell(100,1);
handles.M_M_mask = cell(100,1);
handles.M_M_count = 0;
hold on;   
guidata(hObject, handles);


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


% --------------------------------------------------------------------
function menu_execute_Callback(hObject, eventdata, handles)
% hObject    handle to menu_execute (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
btn_spustit_vypocet_Callback(hObject, eventdata, handles);
set(handles.uipanel_hlavni,'Position',[15 15 1114 692]);
set(handles.uipanel_Make_Mask,'Position',[2015 15 1114 692]);
set(handles.uipanel_adv_info,'Position',[6015 15 1114 692]);


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

set(handles.uipanel_Make_Mask,'Position',[2015 15 1114 692]);
set(handles.uipanel_adv_info,'Position',[6015 15 1114 692]);
set(handles.uipanel_hlavni,'Position',[15 15 1114 692]);


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

if handles.prvni_tvorba_masky == 0
    menu_make_mask_Callback(hObject, eventdata, handles);
else

set(handles.uipanel_hlavni,'Position',[2015 15 1114 692]);
set(handles.uipanel_adv_info,'Position',[6015 15 1114 692]);
set(handles.uipanel_Make_Mask,'Position',[15 15 1114 692]);
end


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

set(handles.uipanel_hlavni,'Position',[4015 15 1114 692]);
set(handles.uipanel_Make_Mask,'Position',[2015 15 1114 692]);
set(handles.uipanel_adv_info,'Position',[15 15 1114 692]);


%%% konec Menu
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes on selection change in pop_zobrazeni.
function pop_zobrazeni_Callback(hObject, eventdata, handles)
% hObject    handle to pop_zobrazeni (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 pop_zobrazeni contents as cell array
%        contents{get(hObject,'Value')} returns selected item from pop_zobrazeni
item = get(handles.pop_zobrazeni,'Value');

switch item
    case 1
      % zobrazí původní obraz  
      imshow(handles.vstup_obraz,'Parent',handles.axes_vystup);
      %imshow(handles.vstup_obraz,'Parent',handles.axes_platno1);

    case 2
      % zobrazí masku  
      imshow(handles.maska,'Parent',handles.axes_vystup);
      %imshow(handles.maska,'Parent',handles.axes_platno1);

    case 3
      % zobrazí známou oblast
      imshow(handles.oblast_Phi,'Parent',handles.axes_vystup);
      %imshow(handles.oblast_Phi,'Parent',handles.axes_platno1);
      
    case 4
      % zobrazí výstupní doplněný obraz
      imshow(handles.vystup_obraz,'Parent',handles.axes_vystup);
      %imshow(handles.vystup_obraz,'Parent',handles.axes_platno1);
      
    otherwise
       % Code for when there is no match.

end

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

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


% --- Executes on button press in btn_M_M_dalsi_bod.
%function btn_M_M_dalsi_bod_Callback(hObject, eventdata, handles)
% hObject    handle to btn_M_M_dalsi_bod (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%set(handles.output_line1,'String',num2str(handles.M_M_count));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Zacatek funkci pro tvorbu masky. 
%%% Pristup k nim je skrz menu->Vytvořit masku


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

% deaktivace tlacitek uloz a pouzij
set(handles.btn_MM_uloz,'Enable','off');
set(handles.btn_MM_pouzij,'Enable','off');

% uvedeni axes_M_Mask do puvodniho stavu aby se nehromadily vrstvy
cla(handles.axes_M_Mask);
imshow(handles.vstup_obraz,'Parent',handles.axes_M_Mask);
hold on;

% nasledne se vykresli vsechny dosavadni oblasti
if handles.M_M_count > 0
    for i = 1:handles.M_M_count % jednotlive oblasti
    
        delka = length(handles.M_M_oblasti{i,1});
        x = handles.M_M_oblasti{i,1}(1:delka);
        y = handles.M_M_oblasti{i,1}([delka+1]:[2*delka]);
    
        for j = 1:delka     % jednotlive body v oblasti
            plot(x(j), y(j), 'b.', 'MarkerSize', 10); 
        end
        
        
        plot(x,y,'-b','LineWidth',3);   % vykresli se cara mezi body
    end
end

handles.M_M_count = handles.M_M_count + 1;
handles.oblast = sledovani_mysi; % zapne funkci kresleni
handles.M_M_oblasti{handles.M_M_count,1} = handles.oblast;
napis = ['Počet oblastí:  ',num2str(handles.M_M_count)];
set(handles.outline_2,'String',napis);
if handles.M_M_count > 0
set(handles.btn_MM_zpet,'Enable','on');
set(handles.btn_MMmake_mask,'Enable','on');
end
guidata(hObject, handles);



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

% nastaveni axes do vychoziho stavu
cla(handles.axes_M_Mask);
imshow(handles.vstup_obraz,'Parent',handles.axes_M_Mask);
hold on;

% snizuje se hodnota M_M_count, tedy se odebira posledni oblast masky
if handles.M_M_count > 0
    if handles.M_M_count == 1
       handles.M_M_count = handles.M_M_count - 1; 
       set(handles.btn_MM_zpet,'Enable','off');
       set(handles.btn_MMmake_mask,'Enable','off');
       set(handles.btn_MM_uloz,'Enable','off');
       set(handles.btn_MM_pouzij,'Enable','off');
    else   
       handles.M_M_count = handles.M_M_count - 1;
       set(handles.btn_MM_uloz,'Enable','off');
       set(handles.btn_MM_pouzij,'Enable','off');
    end

end

guidata(hObject, handles);
napis = ['Počet oblastí:  ',num2str(handles.M_M_count)];
set(handles.outline_2,'String',napis);

if handles.M_M_count > 0
    for i = 1:handles.M_M_count % jednotlive oblasti
    
        delka = length(handles.M_M_oblasti{i,1});
        x = handles.M_M_oblasti{i,1}(1:delka);
        y = handles.M_M_oblasti{i,1}([delka+1]:[2*delka]);
    
        for j = 1:delka
            plot(x(j), y(j), 'b.', 'MarkerSize', 10); 
        end
        
        
        plot(x,y,'-b','LineWidth',3);
    end
end


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

[sirka,vyska]=size(handles.vstup_obraz(:,:,1));
handles.MM_maska = ones(vyska,sirka);
guidata(hObject, handles);
for i = 1:handles.M_M_count
   delka = length(handles.M_M_oblasti{i,1});
   x = handles.M_M_oblasti{i,1}(1:delka);
   y = handles.M_M_oblasti{i,1}([delka+1]:[2*delka]);
   handles.M_M_mask{i,1} = poly2mask(x,y,sirka,vyska);
   guidata(hObject, handles);
end 

handles.MM_maska = handles.M_M_mask{1,1};
 
if handles.M_M_count > 1
for i = 1:handles.M_M_count
   handles.MM_maska = handles.MM_maska + handles.M_M_mask{i,1}(:,:);
   guidata(hObject, handles);
end
else
end

maska1 = logical(handles.MM_maska);
maska2 = 1-maska1;
handles.MM_maska = maska2;
guidata(hObject, handles);

set(handles.btn_MM_uloz,'Enable','on');
set(handles.btn_MM_pouzij,'Enable','on');
imshow(handles.MM_maska,'Parent',handles.axes_M_Mask);


% --- Executes on button press in btn_MM_uloz.
function btn_MM_uloz_Callback(hObject, eventdata, handles)
% hObject    handle to btn_MM_uloz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[file_name, mach_path] = uiputfile({'*.png','obrazky ve formatu PNG';...
    '*.*','vsechny soubory'},'Select File');

 if isequal([file_name,mach_path],[0,0])
        return
 else
     ImageData = cast(handles.MM_maska*255,'uint8');
     imwrite(ImageData,fullfile(mach_path,file_name),'png');
 end

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

handles.maska = handles.MM_maska;
handles.vystupni_maska = cast(handles.maska,'uint8');
guidata(hObject, handles);

set(handles.uipanel_hlavni,'Position',[15 15 1114 692]);
set(handles.uipanel_Make_Mask,'Position',[2015 15 1114 692]);


vystupR = handles.vstup_obraz(:,:,1).*handles.vystupni_maska;
vystupG = handles.vstup_obraz(:,:,2).*handles.vystupni_maska;
vystupB = handles.vstup_obraz(:,:,3).*handles.vystupni_maska;

handles.oblast_Phi = cat(3, vystupR, vystupG, vystupB);
handles.vystup_obraz = handles.oblast_Phi;

set(handles.pop_zobrazeni,'Value',4);
imshow(handles.oblast_Phi,'Parent',handles.axes_vystup);
imshow(handles.oblast_Phi,'Parent',handles.axes4);
set(handles.btn_spustit_vypocet,'Enable','on');

guidata(hObject, handles);


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

set(handles.uipanel_hlavni,'Position',[15 15 1114 692]);
set(handles.uipanel_Make_Mask,'Position',[2015 15 1114 692]);

%%%%% Konec tvorby masky
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% --- Executes on mouse press over axes background.
function axes_vystup_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes_vystup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



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


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


% --- Executes during object creation, after setting all properties.
function edit_iterace_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_iterace (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 btn_uloz_vysledek.
function btn_uloz_vysledek_Callback(hObject, eventdata, handles)
% hObject    handle to btn_uloz_vysledek (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[file_name, mach_path] = uiputfile({'*.png','obrazky ve formatu PNG';...
    '*.*','vsechny soubory'},'Select File');

 if isequal([file_name,mach_path],[0,0])
        return
 else
     ImageData = cast(handles.vystup_obraz,'uint8');
     imwrite(ImageData,fullfile(mach_path,file_name),'png');
 end



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


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

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


% --- Executes on selection change in Lbox_snizovani.
function Lbox_snizovani_Callback(hObject, eventdata, handles)
% hObject    handle to Lbox_snizovani (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 Lbox_snizovani contents as cell array
%        contents{get(hObject,'Value')} returns selected item from Lbox_snizovani


% --- Executes during object creation, after setting all properties.
function Lbox_snizovani_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Lbox_snizovani (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 on selection change in listbox_snizovani.
function listbox_snizovani_Callback(hObject, eventdata, handles)
% hObject    handle to listbox_snizovani (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 listbox_snizovani contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox_snizovani
radek = get(handles.listbox_snizovani,'Value');

if handles.ochrana_listbox_snizovani == 1
    if radek >= handles.pozice_prubehu_snizovani
    else
        imshow(handles.vysledky_obraz_snizovani{radek,1},'Parent',handles.axes4);
    end
end

% --- Executes during object creation, after setting all properties.
function listbox_snizovani_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox_snizovani (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 on button press in checkbox_snizovani.
function checkbox_snizovani_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_snizovani (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 checkbox_snizovani

hodnota = get(handles.checkbox_snizovani,'Value');

if hodnota == 0
    set(handles.edit_snizovani_prahu,'Enable','off');
    set(handles.edit_snizovani_prahu,'BackgroundColor',[0.702 0.702 0.702]);
elseif hodnota == 1
    set(handles.edit_snizovani_prahu,'Enable','on');
    set(handles.edit_snizovani_prahu,'BackgroundColor',[0.702 0.702 0.702]);
end




% --- Executes on button press in btn_uloz_vypis.
function btn_uloz_vypis_Callback(hObject, eventdata, handles)
% hObject    handle to btn_uloz_vypis (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[file_name, mach_path] = uiputfile({'*.xls','Office Open XML';...
    '*.*','vsechny soubory'},'Select File');

 if isequal([file_name,mach_path],[0,0])
        return
 else
     data = handles.prubeh_snizovani_thre;
     xlswrite(fullfile(mach_path,file_name),data);
 end


% --- Executes on button press in btn_uloz_vypis_obr.
function btn_uloz_vypis_obr_Callback(hObject, eventdata, handles)
% hObject    handle to btn_uloz_vypis_obr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[file_name, mach_path] = uiputfile({'*.png','obrazky ve formatu PNG';...
    '*.*','vsechny soubory'},'Select File');

 if isequal([file_name,mach_path],[0,0])
        return
 else
     
     radek = get(handles.listbox_snizovani,'Value');
     if radek >= handles.pozice_prubehu_snizovani
     else
         radek = get(handles.listbox_snizovani,'Value');
         ImageData = cast(handles.vysledky_obraz_snizovani{radek,1},'uint8');
         imwrite(ImageData,fullfile(mach_path,file_name),'png');
     end
 end



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


% --- Executes during object creation, after setting all properties.
function edit_uroven_zobr_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_uroven_zobr (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 btn_zobr_koef.
function btn_zobr_koef_Callback(hObject, eventdata, handles)
% hObject    handle to btn_zobr_koef (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
urovne = str2double(get(handles.edit_uroven_zobr,'String'));
colormapa = get(handles.checkbox_colormap,'Value');

if handles.mapa_koef_uroven{1,1} == urovne && handles.mapa_koef_uroven{1,2} == colormapa
    
    if colormapa == 1
        imshow(handles.obraz_koeficientu,'Colormap',colormap('jet'),'Parent',handles.axes4);
    else
        imshow(handles.obraz_koeficientu,'Parent',handles.axes4);
    end
    
    
else
    filters = handles.filtry{1,1};
    obraz_r = double(handles.vstup_obraz(:,:,1))/255;
    urovne = str2double(get(handles.edit_uroven_zobr,'String'));
    koeficienty = rozklad(obraz_r,filters,urovne);
    handles.obraz_koeficientu = zobraz_koeficienty(koeficienty,urovne,colormapa);
    handles.mapa_koef_uroven{1,1} = urovne;
    handles.mapa_koef_uroven{1,2} = colormapa;
    guidata(hObject, handles);

    if colormapa == 1
        imshow(handles.obraz_koeficientu,'Colormap',colormap('jet'),'Parent',handles.axes4);
    else
        imshow(handles.obraz_koeficientu,'Parent',handles.axes4);
    end

    set(handles.btn_uloz_obr_koef,'Enable','on');
end

% --- Executes on button press in btn_uloz_obr_koef.
function btn_uloz_obr_koef_Callback(hObject, eventdata, handles)
% hObject    handle to btn_uloz_obr_koef (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[file_name, mach_path] = uiputfile({'*.png','obrazky ve formatu PNG';...
    '*.*','vsechny soubory'},'Select File');

 if isequal([file_name,mach_path],[0,0])
        return
 else
%       ImageData = handles.oblast_Phi;
    %ImageData = getimage(handles.axes4);    
%     Cr = handles.obraz_koeficientu;
%     ImageData = (Cr/max(max(Cr)))*255;
    ImageData = handles.obraz_koeficientu;
    imwrite(ImageData,fullfile(mach_path,file_name),'png');
    %imwrite(ImageData,colormap('jet'),fullfile(mach_path,file_name),'png');

 end



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


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

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


% --- Executes on selection change in popup_filtry.
function popup_filtry_Callback(hObject, eventdata, handles)
% hObject    handle to popup_filtry (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 popup_filtry contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popup_filtry


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

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


% --- Executes when selected object is changed in uipanel_algoritmus.
function uipanel_algoritmus_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel_algoritmus 
% 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)

switch get(eventdata.NewValue,'Tag')
    case 'radiobtn_framelet'
      %execute this code when fontsize08_radiobutton is selected
      set(handles.popup_filtry,'Enable','on');
      
      xx = get(handles.edit_level,'String');
      z = cast(xx,'double');
      delka = length(z);
      pozice = 1;
      cisel = 0;
      
      for j = 1:delka
          if z(1,j) > 47 && z(1,j) < 58
              cisel = cisel + 1;
          end
      end


      xxx = zeros(1,cisel);
      
      for j = 1:delka
          if z(1,j) > 47 && z(1,j) < 58
              xxx(1,pozice) = z(1,j)-48;
              pozice = pozice + 1;
          end
      end
      
      handles.level_contourlet = xxx;
      set(handles.edit_level,'String',num2str(handles.level_framelet)); 
      
        vypis = cell(6,1);
        set(handles.output_line1,'String',vypis);  

    case 'radiobtn_contourlet'
      %execute this code when fontsize12_radiobutton is selected
      set(handles.popup_filtry,'Enable','off');
      handles.level_framelet = str2double(get(handles.edit_level,'String'));
      set(handles.edit_level,'String',num2str(handles.level_contourlet)); 
      
        vypis = cell(7,1);
        vypis{1,1} = 'Contourlet Toolbox dovoluje rozkládat jen obrazy, jejichž rozměry jsou dělitelné číslem faktoru podvzorkování pro všechny úrovně rozkladu.';
        vypis{3,1} = 'Ujistěte se, že máte zkompilovaný mex soubor resampc.c';
        vypis{4,1} = 'Postup:';
        vypis{5,1} = '1) V Matlabu najeďte do složky contourlet_toolbox_tight ';
        vypis{6,1} = '2) napište: mex resampc.c';
        vypis{7,1} = '3) vyberte kompilátor';
        set(handles.output_line1,'String',vypis);  

      
    otherwise
       % Code for when there is no match.

end
%updates the handles structure
guidata(hObject, handles);


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