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

% Last Modified by GUIDE v2.5 15-May-2009 06:08:40

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = findCorespondColors_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 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)
vstupniObraz  = double(imread(get(handles.edit1,'String'), 'jpg')+1); % nacteni vstupniho obrazu 1
vstupniObraz2 = double(imread(get(handles.edit2,'String'), 'jpg')+1); % nacteni vstupniho obrazu 2

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

if (get(handles.radiobutton1,'Value'))
    Obraz1 = getSCFromRGB(vstupniObraz);
    Obraz2 = getSCFromRGB(vstupniObraz2);
    KHprah = 150;
else
  if(get(handles.radiobutton2,'Value')==1)
    Obraz1 = getSCFromHSV(vstupniObraz);
    Obraz2 = getSCFromHSV(vstupniObraz2); 
    KHprah = 1000;
  end
end
 
outputVector = Kohonen(Obraz1(:,:), Obraz2(:,:),8,KHprah);

[height1 width1 z1] = size(vstupniObraz);
[height2 width2 z2] = size(vstupniObraz2);
 
vystupniObraz = vstupniObraz;
vystupniObraz(height1+1:height1+height2,1:width2,1:3) = vstupniObraz2;

set(gcf,'CurrentAxes',handles.axes3);
axes3=imshow(uint8(vystupniObraz));

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

HighlightFinded(Obraz1, 0);
HighlightFinded(Obraz2, height1);


outputVector = Kohonen(Obraz1(:,:), Obraz2(:,:),8,KHprah); 
v = 1;
for u=1:1:size(outputVector,2)   
    if (outputVector(17,u)==1)
    else
      % vyznacit korespondence
      if (get(handles.checkbox4,'Value')==1)
        h = line([outputVector(19,u) outputVector(6,u)],[outputVector(20,u) height1+outputVector(7,u)]);
      end
      outputVectorRed(:,v) = outputVector(:,u);
      v = v+1; 
    end
end



% vypsat korespondence
if (get(handles.checkbox6,'Value')==1)
    outputVector
end

%vysledek do noveho okna
if (get(handles.checkbox7,'Value')==1)
    figure
    imshow(uint8(vystupniObraz));
    HighlightFinded(Obraz1, 0);
    HighlightFinded(Obraz2, height1);
    % vyznacit korespondence
    if (get(handles.checkbox4,'Value')==1)
        for u=1:1:size(outputVector,2)   
            if (outputVector(17,u)==1)
            else
              h = line([outputVector(19,u) outputVector(6,u)],[outputVector(20,u) height1+outputVector(7,u)]);              
            end
        end
    end
end

%RANSAC, MHT
[H, inliers] = ransacfithomography(outputVectorRed(6:7,:),outputVectorRed(19:20,:), 0.1);


%RANSAC do noveho okna
if (get(handles.checkbox8,'Value')==1)
    if (get(handles.checkbox10,'Value')==1)
        inliers
    end
    [height width z] = size(vystupniObraz);
    figure
    imshow(uint8(vystupniObraz));
    HighlightFinded(Obraz1, 0);
    HighlightFinded(Obraz2, height1);
    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))]);
    end
end
    
%homografie
if (get(handles.checkbox5,'Value')==1)
    if (get(handles.checkbox9,'Value')==1)
        H
    end

    img1  = double(imread(get(handles.edit1,'String'), 'jpg')+1);
    img2  = double(imread(get(handles.edit2,'String'), 'jpg')+1); 

    Hi = inv(H);
    % init imgdest
    V = [ 1 1 1; size(img1,2) 1 1; size(img1,2) size(img1,1) 1; 1 size(img1,1) 1 ;
          1 1 1; size(img2,2) 1 1; size(img2,2) size(img2,1) 1; 1 size(img2,1) 1 ]';
    V(:,1:4) = Hi * V(:,1:4);
    V(:,1:4) = V(:,1:4) ./ [ ones(3,1) * V(3,1:4) ];
    xmin = floor(min(V(1,:)))-1;
    ymin = floor(min(V(2,:)))-1;
    width = ceil(max(V(1,:))) - xmin;
    heigth = ceil(max(V(2,:))) - ymin;
    imgdest = uint8(zeros(heigth,width,3));
    ixmin = floor(min(V(1,1:4)));
    iymin = floor(min(V(2,1:4)));
    ixmax = ceil(max(V(1,1:4)));
    iymax = ceil(max(V(2,1:4)));
    % init imgdest
    [ X Y ] = meshgrid(ixmin:ixmax,iymin:iymax);
    gridsize = size(X);
    TEMP = prod(gridsize);
    X = reshape(X,1,TEMP);
    Y = reshape(Y,1,TEMP);
    TEMP = H * [ X; Y; ones(1,TEMP) ];
    TEMP = TEMP ./ [ ones(3,1) * TEMP(3,:) ];
    X = reshape(TEMP(1,:),gridsize);
    Y = reshape(TEMP(2,:),gridsize);
    for I = 1:3
       imgdest(iymin-ymin:iymax-ymin,ixmin-xmin:ixmax-xmin,I) = interp2(double(img1(:,:,I)),X,Y);
    end

    figure;
    imshow(imgdest);

    ixmin = floor(min(V(1,5:8)))-xmin;
    ixmax = ixmin+size(img2,2)-1;
    iymin = floor(min(V(2,5:8)))-ymin;
    iymax = iymin+size(img2,1)-1;
    imgdest(iymin:iymax,ixmin:ixmax,:) = img2(:,:,:);

    figure;
    imshow(imgdest);

end













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


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


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

% --- 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)
[filename, pathname] = uigetfile({'*.jpg'}, 'Otevrit jpg soubor');
set(handles.edit2,'String',[pathname,filename]);
set(gcf,'CurrentAxes',handles.axes2);
axes2=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



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


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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 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 button press in checkbox9.
function checkbox9_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox9 (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 checkbox9


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


