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

% Last Modified by GUIDE v2.5 19-May-2009 18:05:44

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = findTranslation_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)
global pos;
pos = 0;

global pathname;
[filename,pathname,FilterIndex] = uigetfile({'*.jpg'}, 'Otevrit jpg soubor','MultiSelect','on');
set(handles.listbox1,'String',filename);

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

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


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

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


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

global pathname;
filename = get(handles.listbox1,'String');

global pos;
if (exist('pos','var') && pos~=size(filename,1)-1) 
    pos = pos + 1;
else
    pos =  1;
end

if (size(filename,1)>=2 )
    
    jmeno1 = char(strcat(pathname, filename(1,:)));
    jmeno2 = char(strcat(pathname, filename(2,:)));
    
    filename = get(handles.listbox1,'String');
    set(handles.listbox1,'String',filename(2:size(filename)));
    
    set(handles.text4,'String',jmeno1);
    set(handles.text5,'String',jmeno2);

    vstupniObraz  = double(imread(jmeno1)); % nacteni vstupniho obrazu 1
    vstupniObraz2 = double(imread(jmeno2)); % nacteni vstupniho obrazu 2

    [height1 width1 z1] = size(vstupniObraz);
    [height2 width2 z2] = size(vstupniObraz2);
    [height width z] = size(vstupniObraz2);

    vystupniObraz = vstupniObraz;
    vystupniObraz(height1+1:height1+height2,1:width2,1:3) = vstupniObraz2;

    set(gcf,'CurrentAxes',handles.axes1);
    axes1=imshow(uint8(vystupniObraz));
    
    vectorComplete1 = getSCFromHSV(vstupniObraz);
    vectorComplete2 = getSCFromHSV(vstupniObraz2); 
    KHprah = 1000;

    HighlightFinded(vectorComplete1, 0);
    HighlightFinded(vectorComplete2, height1);
    
    % prohledavani po castech    
    if (get(handles.checkbox1,'Value')==1)
        pocetdilku = get(handles.popupmenu2,'Value')+1;
        velikostPresahu = get(handles.popupmenu1,'Value');
        posh = round(velikostPresahu*height/(100*pocetdilku));
        posw = round(velikostPresahu*width/(100*pocetdilku));
        posh2 = round(posh*2);
        posw2 = round(posw*2);
        rozsirenyObraz1(1:height+round(posh2),1:width+round(posw2),1) = double(0);
        rozsirenyObraz1(1:height+round(posh2),1:width+round(posw2),2) = double(0);
        rozsirenyObraz1(1:height+round(posh2),1:width+round(posw2),3) = double(0);
        rozsirenyObraz1(posh+1:height+posh,posw+1:width+posw,:) = vstupniObraz;
        
        rozsirenyObraz2(1:height+round(posh2),1:width+round(posw2),1) = double(0);
        rozsirenyObraz2(1:height+round(posh2),1:width+round(posw2),2) = double(0);
        rozsirenyObraz2(1:height+round(posh2),1:width+round(posw2),3) = double(0);
        rozsirenyObraz2(posh+1:height+posh,posw+1:width+posw,:) = vstupniObraz2;

        lasti = posh+1;
        lastj = posw+1;
        redh = 0;
        redw = 0;
        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
                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

                Obraz1 = getSCFromHSVe(rozsirenyObraz1(x(1):x(2),y(1):y(2),:));
                Obraz2 = getSCFromHSVe(rozsirenyObraz2(x(1):x(2),y(1):y(2),:));

                Obraz1(6,:) = Obraz1(6,:) + j - round(width/pocetdilku)-posw; 
                Obraz1(7,:) = Obraz1(7,:)+ i - round(height/pocetdilku)-posh;
                Obraz2(6,:) = Obraz2(6,:) + j - round(width/pocetdilku)-posw; 
                Obraz2(7,:) = Obraz2(7,:)+ i - round(height/pocetdilku)-posh;
                if (m>1)
                   Obraz1(7,:) = Obraz1(7,:) -posh;
                end
                if (n>1)
                   Obraz1(6,:) = Obraz1(6,:) -posw; 
                   Obraz2(6,:) = Obraz2(6,:) -posw; 
                end
                vectorComplete1(:,size(vectorComplete1,2)+1:size(vectorComplete1,2)+size(Obraz1,2)) = Obraz1;
                vectorComplete2(:,size(vectorComplete2,2)+1:size(vectorComplete2,2)+size(Obraz2,2)) = Obraz2;
                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
    HighlightFinded(vectorComplete1, 0);
    HighlightFinded(vectorComplete2, height1);
    
    %hledani korespondenci
    outputVector = CorespondDel(vectorComplete1(:,:), vectorComplete2(:,:)); 
    %odstraneni prazdnych korespondenci
    v=1;
    for u=1:1:size(outputVector,2)   
        if (outputVector(17,u)==1)
        else
          outputVectorRed(:,v) = outputVector(:,u);
          v = v+1; 
        end
    end
  
    %filtr pomoci RANSAC
    [H, inliers] = ransacfithomography(outputVectorRed(6:7,:),outputVectorRed(19:20,:), 0.1);
    [height width z] = size(vystupniObraz);
   
    for u=1:1:size(inliers,2) 
        h = line([outputVectorRed(19,inliers(u)) outputVectorRed(6,inliers(u))],[outputVectorRed(20,inliers(u)) height1+outputVectorRed(7,inliers(u))]);
        h = line([outputVectorRed(19,inliers(u)) outputVectorRed(6,inliers(u))],[height1+outputVectorRed(20,inliers(u)) height1+outputVectorRed(7,inliers(u))]);
        set(h,'Color',[1,1,1])
        set(h,'LineWidth',2)
    end
else
    set(handles.text6,'String','Neni nacten dostatecny pocet obrazu');
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 selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (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 popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (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 popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

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


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

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


