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

% Last Modified by GUIDE v2.5 29-May-2008 18:50:14

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @DetekceObj_OpeningFcn, ...
                   'gui_OutputFcn',  @DetekceObj_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 DetekceObj is made visible.
function DetekceObj_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);

%-----------------------------------------------------------------------
function varargout = DetekceObj_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output;
%-----------------------------------------------------------------------

function DetektorMenu_Callback(hObject, eventdata, handles)
index=get(handles.DetektorMenu,'Value');
if index==1
    set(handles.DetektorMenu,'UserData','sobel');
    set(handles.TextPrah2,'Enable','off');
    set(handles.TextPrah2,'String','0');
    set(handles.AutPrah,'Value',0);
    set(handles.PrahSlider,'Enable','on');
elseif index==2
    set(handles.DetektorMenu,'UserData','prewitt');
    set(handles.TextPrah2,'Enable','off');
    set(handles.TextPrah2,'String','0');
    set(handles.AutPrah,'Value',0);
    set(handles.PrahSlider,'Enable','on');
elseif index==3
    set(handles.DetektorMenu,'UserData','canny');
    set(handles.TextPrah2,'Enable','on');
    prah=get(handles.PrahSlider,'Value');
    set(handles.TextPrah2,'String',prah*0.4);
    set(handles.AutPrah,'Value',0);
    set(handles.PrahSlider,'Enable','on');
end

%-----------------------------------------------------------------------
function DetektorMenu_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


%-----------------------------------------------------------------------
function DetekceButton_Callback(hObject, eventdata, handles)
Obr=get(handles.OpenFile,'UserData');
if Obr ~= 0
    index=get(handles.DetektorMenu,'Value');
    if index==1
        set(handles.DetektorMenu,'UserData','sobel');
    elseif index==2
        set(handles.DetektorMenu,'UserData','prewitt');
    elseif index==3
        set(handles.DetektorMenu,'UserData','canny');
    end
    Detektor=get(handles.DetektorMenu,'Userdata');
    indexSum=get(handles.SumMenu,'Value');
    if indexSum==1
        set(handles.SumMenu,'UserData','salt & pepper');
    elseif indexSum==2
        set(handles.SumMenu,'UserData','gaussian');
    end
    typSumu=get(handles.SumMenu,'UserData');
    Prah=get(handles.PrahSlider,'Value');
    Sum=get(handles.SumSlider,'Value');
    Velikost=get(handles.VelikostSlider,'Value');
    guidata(hObject, handles);
    [c,hist]=detekce(Obr,Detektor,Prah,Sum,Velikost,typSumu);
    set(handles.DetekceButton,'UserData',c);
else
    set(handles.text11,'String','Chyba: Není načten obraz','ForegroundColor','red');
end


% --------------------------------------------------------------------
function OpenFile_Callback(hObject, eventdata, handles)
FileOpen = uigetfile( ...
 {'*.jpg','*.jpg';,'*.bmp','*.bmp';,'*.png','*.png';},...
  'Nacti obrazek', ...
  'MultiSelect', 'on'); 
if isequal(FileOpen, 0)
    return
end
imshow(FileOpen); 
set(handles.OpenFile,'UserData',FileOpen);
set(handles.text11,'String','');

% --------------------------------------------------------------------
function SaveFile_Callback(hObject, eventdata, handles)
[filename, pathname] = uiputfile(...
    {'*.jpg','*.jpg';,'*.bmp','*.bmp';,'*png','*.png';},...
     'Ulož obrázek');
if isequal(filename,0) || isequal(pathname,0)
    return
end
newFig = figure;
axes_units = get(handles.axes1,'Units');
axes_pos = get(handles.axes1,'Position');
axesObject2 = copyobj(handles.axes1,newFig);
set(newFig,'Units',axes_units);
set(newFig,'Position',[axes_pos(1) axes_pos(2) axes_pos(3) axes_pos(4)]);
Colormap(gray);
saveas(newFig,fullfile(pathname, filename),'jpg'); 
close(newFig);

    
% --------------------------------------------------------------------
function CloseMenu_Callback(hObject, eventdata, handles)
selection = questdlg('Opravdu chcete ukončit aplikaci ?',' ','Ano','Ne','Ano');
switch selection,
   case 'Ano',
   clear all;
   close all;
   case 'Ne'
     return
end



% --------------------------------------------------------------------
function FileMenu_Callback(hObject, eventdata, handles)



%-----------------------------------------------------------------------
function PrahSlider_Callback(hObject, eventdata, handles)
index=get(handles.DetektorMenu,'Value');
if index==1
    HodnotaPrahu=get(handles.PrahSlider,'Value');  
    set(handles.TextPrah,'String',HodnotaPrahu);
    set(handles.TextPrah2,'String','0');
elseif index==2
    HodnotaPrahu=get(handles.PrahSlider,'Value');  
    set(handles.TextPrah,'String',HodnotaPrahu);
    set(handles.TextPrah2,'String','0');
elseif index==3
    HodnotaPrahu=get(handles.PrahSlider,'Value');
    set(handles.TextPrah,'String',HodnotaPrahu);
    set(handles.TextPrah2,'String',HodnotaPrahu*0.4);   
end

%-----------------------------------------------------------------------
function PrahSlider_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end



%-----------------------------------------------------------------------
function HistButton_Callback(hObject, eventdata, handles)
I=get(handles.OpenFile,'UserData');
if I ~= 0
    I=imread(I);
    I=rgb2gray(I);
    figure('Name','Histogram','NumberTitle','off');
    imhist(I);
else
    set(handles.text11,'String','Chyba: Není načten obraz','ForegroundColor','red');
end


%-----------------------------------------------------------------------
function SumMenu_Callback(hObject, eventdata, handles)

%-----------------------------------------------------------------------
function SumMenu_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%-----------------------------------------------------------------------
function SumSlider_Callback(hObject, eventdata, handles)
HodnotaSumu=get(handles.SumSlider,'Value');  
set(handles.TextSum,'String',HodnotaSumu*100);
set(handles.AutPrah,'Value',0);

%-----------------------------------------------------------------------
function SumSlider_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

%-----------------------------------------------------------------------
function SumButton_Callback(hObject, eventdata, handles)
Obr=get(handles.OpenFile,'UserData');
if Obr ~= 0
    Obr=imread(Obr);
    Obr=rgb2gray(Obr);
    sum=get(handles.SumSlider,'Value');
    typ=get(handles.SumMenu,'Value');
    if typ==1
        Obr=imnoise(Obr,'salt & pepper',sum);
    elseif typ==2
        Obr=imnoise(Obr,'gaussian',sum,0.01);
    end
    figure('name','Zašumělý obraz','NumberTitle','off');
    imshow(Obr);
else
    set(handles.text11,'String','Chyba: Není načten obraz','ForegroundColor','red');
end

%-----------------------------------------------------------------------
function PrahButton_Callback(hObject, eventdata, handles)
Obr=get(handles.OpenFile,'UserData');
if Obr ~= 0
    Obr=imread(Obr);
    Obr=rgb2gray(Obr);
    sum=get(handles.SumSlider,'Value');
    typ=get(handles.SumMenu,'Value');
    if typ==1
        Obr=imnoise(Obr,'salt & pepper',sum);
    elseif typ==2
        Obr=imnoise(Obr,'gaussian',sum);
    end    
    Obr=medfilt2(Obr);
    prah=get(handles.PrahSlider,'Value');
    index=get(handles.DetektorMenu,'Value');
    if index==1
        set(handles.DetektorMenu,'UserData','sobel');
    elseif index==2
        set(handles.DetektorMenu,'UserData','prewitt');
    elseif index==3
        set(handles.DetektorMenu,'UserData','canny');   
    end
    detektor=get(handles.DetektorMenu,'Userdata');
    Obr=edge(Obr,detektor,prah);
    figure('name','Vyhranovaný obraz','NumberTitle','off');
    imshow(Obr);
else
    set(handles.text11,'String','Chyba: Není načten obraz','ForegroundColor','red');
end


%-----------------------------------------------------------------------
function AutPrah_Callback(hObject, eventdata, handles)
Obr=get(handles.OpenFile,'UserData');
if (get(handles.AutPrah,'Value'))==1
        set(handles.PrahSlider,'Enable','off');
        set(handles.PrahSlider,'Visible','on');
else
        set(handles.PrahSlider,'Enable','on');
        set(handles.PrahSlider,'Visible','on');
end

if Obr ~= 0
    if (get(handles.AutPrah,'Value'))==1
        Obr=imread(Obr);
        Obr=rgb2gray(Obr);
        sum=get(handles.SumSlider,'Value');
        Obr=imnoise(Obr,'salt & pepper',sum);
        Obr=medfilt2(Obr);
        index=get(handles.DetektorMenu,'Value');
        if index==1
            set(handles.DetektorMenu,'UserData','sobel');            
        elseif index==2
            set(handles.DetektorMenu,'UserData','prewitt');           
        elseif index==3
            set(handles.DetektorMenu,'UserData','canny');
        end
        detektor=get(handles.DetektorMenu,'Userdata');
        [Obr,prah]=edge(Obr,detektor);
        if index == 3
            set(handles.TextPrah,'String',prah(2));
            set(handles.PrahSlider,'Value',prah(2));
            set(handles.AutPrah,'UserData',prah(2));
            set(handles.TextPrah2,'String',prah(1));
        else
        set(handles.TextPrah,'String',prah);
        set(handles.PrahSlider,'Value',prah);
        set(handles.AutPrah,'UserData',prah);
        end
    elseif (get(handles.AutPrah,'Value'))==0
    set(handles.PrahSlider,'Visible','on');
    set(handles.PrahSlider,'Enable','on');
    end
else
    set(handles.text11,'String','Chyba: Není načten obraz','ForegroundColor','red');
end

%-----------------------------------------------------------------------
function VelikostSlider_Callback(hObject, eventdata, handles)
VelikostObj=get(handles.VelikostSlider,'Value');
set(handles.VelikostText,'String',VelikostObj);

%-----------------------------------------------------------------------
function VelikostSlider_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

%-----------------------------------------------------------------------
function OblastiButton_Callback(hObject, eventdata, handles)

%-----------------------------------------------------------------------
function PuvodniButton_Callback(hObject, eventdata, handles)
Obr=get(handles.OpenFile,'UserData');
if Obr ~= 0
    imshow(Obr);
else
    set(handles.text11,'String','Chyba: Není načten obraz','ForegroundColor','red');
end
