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

% Last Modified by GUIDE v2.5 15-May-2009 03:32:02

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = findSignificantColors_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile({'*.jpg'}, 'Otevrit jpg soubor');
set(handles.edit1,'String',[pathname,filename]);
set(gcf,'CurrentAxes',handles.axes1);
axes1=imshow([pathname,filename]);


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


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

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


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

mainHandle = gcf;

vstupniObraz  = double(imread(get(handles.edit1,'String'), 'jpg')+1); % nacteni vstupniho obrazu 1
[height width z] = size(vstupniObraz);



set(gcf,'CurrentAxes',handles.axes1);
axes1=imshow(get(handles.edit1,'String'));

if (get(handles.radiobutton1,'Value'))
    Obraz1 = getSCFromRGB(vstupniObraz);
else
  if(get(handles.radiobutton2,'Value')==1)
    Obraz1 = getSCFromHSV(vstupniObraz);
  end
end

vectorComplete = Obraz1;
% prohledavani po castech
if (get(handles.checkbox1,'Value')==1)
    pocetdilku = get(handles.popupmenu4,'Value')+1;
    velikostPresahu = get(handles.popupmenu3,'Value');
    
    posh = round(velikostPresahu*height/(100*pocetdilku));
    posw = round(velikostPresahu*width/(100*pocetdilku));
    posh2 = round(posh*2);
    posw2 = round(posw*2);

    rozsirenyObraz(1:height+round(posh2),1:width+round(posw2),1) = double(0);
    rozsirenyObraz(1:height+round(posh2),1:width+round(posw2),2) = double(0);
    rozsirenyObraz(1:height+round(posh2),1:width+round(posw2),3) = double(0);
    rozsirenyObraz(posh+1:height+posh,posw+1:width+posw,:) = vstupniObraz;
    
    
    lasti = posh+1;
    lastj = posw+1;
    redh = 0;
    redw = 0;
    if (get(handles.checkbox8,'Value')==1)
        figure
    end
    k=1;
    m=1;
    for i = round(height/pocetdilku)+posh:round(height/pocetdilku):height+(posh2)+1
        n=1;
        for j = round(width/pocetdilku)+posw:round(width/pocetdilku):width+(posw2)+1
           
            if (get(handles.checkbox8,'Value')==1)
                subplot(pocetdilku,pocetdilku,k);
            end
            x = [lasti-posh i+posh];
            y = [lastj-posw j+posw];
            if (m==1)
              x(1) = lasti; 
            end
            if (n==1)
              y(1) = lastj; 
            end
            if (m==pocetdilku)
              x(2) = height+posh; 
            end
            if (n==pocetdilku)
              y(2) = width+posw; 
            end
            
            if (get(handles.checkbox8,'Value')==1)
                imshow(uint8(rozsirenyObraz(x(1):x(2),y(1):y(2),:)));
            end
            
            
            if (get(handles.radiobutton1,'Value'))
                Obraz1 = getSCFromRGBe(rozsirenyObraz(x(1):x(2),y(1):y(2),:));
            else
              if(get(handles.radiobutton2,'Value')==1)
                Obraz1 = getSCFromHSVe(rozsirenyObraz(x(1):x(2),y(1):y(2),:));
              end
            end
            
            if (get(handles.checkbox8,'Value')==1)
                HighlightFinded(Obraz1, 0);
            end
            
            Obraz1(6,:) = Obraz1(6,:) + j - round(width/pocetdilku)-posw; 
            Obraz1(7,:) = Obraz1(7,:)+ i - round(height/pocetdilku)-posh;
            if (m>1)
               Obraz1(7,:) = Obraz1(7,:) -posh;
            end
            if (n>1)
               Obraz1(6,:) = Obraz1(6,:) -posw; 
            end

            vectorComplete(:,size(vectorComplete,2)+1:size(vectorComplete,2)+size(Obraz1,2)) = Obraz1;

            redw = redw + posw2;
            lastj = j;
            n=n+1;
            k=k+1;
        end
        redh = redh + posh2;
        redw = 0;
        lastj = posw+1;
        lasti = i;
        m=m+1;
    end
end

% vypsat detekovane barvy
if (get(handles.checkbox6,'Value')==1)
    vectorComplete
end


set(0,'CurrentFigure',mainHandle)
set(mainHandle,'CurrentAxes',handles.axes1);
HighlightFinded(vectorComplete, 0);

%vysledek do noveho okna
if (get(handles.checkbox7,'Value')==1)
    figure
    imshow(uint8(vstupniObraz));
    HighlightFinded(vectorComplete, 0);
end


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

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


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


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


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


% --- Executes on selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu3


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

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


% --- Executes on selection change in popupmenu4.
function popupmenu4_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu4 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu4


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


