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

% Last Modified by GUIDE v2.5 03-May-2011 18:51:36

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

% Choose default command line output for Subgui_jednotlive
set(hObject,'toolbar','figure');
handles.output = hObject;

axes(handles.axes1)
handles.obr1 = ones(1000,1500);
imshow(handles.obr1);
axes(handles.axes2)
handles.obr2 = ones(1000,1500);
imshow(handles.obr2);
% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Subgui_jednotlive_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;


function Nacteni1_pushbutton_Callback(hObject, eventdata, handles)
clear handles.obr1_bif handles.obr1_faz handles.obr2_faz
handles.bifurkace_souradnice1 = 0;

[soubor1,cesta1,FilterIndex] = uigetfile( ...
{'*.jpg;*.jpeg;*.bmp;*.png'}, ...
   'Pick a file');
if FilterIndex ~= 0
    axes(handles.axes1)
    handles.obr1_orig = imread([cesta1 '' soubor1]);  
    vel=size(handles.obr1_orig);
    if vel(1) > 1000 && vel(2) > 1500
        handles.obr1_orig = imresize(handles.obr1_orig,[1000 1500]);
        clear vel
    end   
    handles.obr1 = handles.obr1_orig;
    imshow(handles.obr1);
end


guidata(hObject, handles);


function Nacteni2_pushbutton_Callback(hObject, eventdata, handles)
clear handles.obr2_bif handles.obr1_faz handles.obr2_faz
handles.bifurkace_souradnice2 = 0;
[soubor2,cesta2,FilterIndex] = uigetfile( ...
{'*.jpg;*.jpeg;*.bmp;*.png'}, ...
   'Pick a file');
if FilterIndex ~= 0
    axes(handles.axes2)
    handles.obr2_orig = imread([cesta2 '' soubor2]);
    vel=size(handles.obr2_orig);
    if vel(1) > 1000 && vel(2) > 1500
        handles.obr2_orig = imresize(handles.obr2_orig,[1000 1500]);
        clear vel
    end 
    handles.obr2 = handles.obr2_orig;
    imshow(handles.obr2);
end


guidata(hObject, handles);


function Bifurkace1_pushbutton_Callback(hObject, eventdata, handles)
set(handles.status_text,'String','Pracuji');
pause(0.1)
[obr_bifurkace1,handles.obr1_bif,handles.bifurkace_souradnice1] = komplexni(handles.obr1_orig);
bifurkace_souradnice1 = handles.bifurkace_souradnice1;
handles.obr1 = handles.obr1_bif;
axes(handles.axes1)
imshow(handles.obr1);

[soubor,cesta,FilterIndex] = uiputfile( ...
{'*.mat'}, ...
   'Pick a file');
if FilterIndex ~= 0
    save([cesta '' soubor], 'bifurkace_souradnice1')
end

set(handles.status_text,'String','Hotovo');
guidata(hObject, handles);

function Bifurkace2_pushbutton_Callback(hObject, eventdata, handles)
set(handles.status_text,'String','Pracuji');
pause(0.1)
[obr_bifurkace2,handles.obr2_bif,handles.bifurkace_souradnice2] = komplexni(handles.obr2_orig);
handles.obr2 = handles.obr2_bif;
axes(handles.axes2)
imshow(handles.obr2);

[soubor,cesta,FilterIndex] = uiputfile( ...
{'*.mat'}, ...
   'Pick a file');
if FilterIndex ~= 0
    save([cesta '' soubor], 'handles.bifurkace_souradnice2')
end
set(handles.status_text,'String','Hotovo');
guidata(hObject, handles);


function ulozeni1_pushbutton_Callback(hObject, eventdata, handles)

[soubor1,cesta1,FilterIndex] = uiputfile( ...
{'*.jpg;*.jpeg;*.bmp;*.png'}, ...
   'Pick a file');

imwrite(handles.obr1, [cesta1 '' soubor1])
guidata(hObject, handles);

function ulozeni2_pushbutton_Callback(hObject, eventdata, handles)

[soubor2,cesta2,FilterIndex] = uiputfile( ...
{'*.jpg;*.jpeg;*.bmp;*.png'}, ...
   'Pick a file');

imwrite(handles.obr2, [cesta2 '' soubor2])

guidata(hObject, handles);


function fazova_pushbutton_Callback(hObject, eventdata, handles)
set(handles.status_text,'String','Pracuji');
pause(0.1)
[handles.obr1_faz handles.obr2_faz] = fazova_korelace(handles.obr1_orig, handles.obr2_orig, 0, 3);

handles.obr1 = handles.obr1_faz;
axes(handles.axes1)
imshow(handles.obr1);

handles.obr2 = handles.obr2_faz;
axes(handles.axes2)
imshow(handles.obr2);

figure
imshow(sachovnice(handles.obr1_faz, handles.obr2_faz),[])

set(handles.status_text,'String','Hotovo');
guidata(hObject, handles);

function kvadraticka_pushbutton_Callback(hObject, eventdata, handles)
set(handles.status_text,'String','Pracuji');
pause(0.1)
if handles.bifurkace_souradnice1 == 0
    disp('bifurkace')
    [obr_bifurkace1,handles.obr1_bif,handles.bifurkace_souradnice1] = komplexni(handles.obr1_orig);
end
% wait_interruptible()

[handles.obr2_kvadrat]=slicovani(handles.obr1_orig, handles.obr2_orig,handles.bifurkace_souradnice1);

handles.obr1 = handles.obr1_orig;
axes(handles.axes1)
imshow(handles.obr1);

handles.obr2 = handles.obr2_kvadrat;
axes(handles.axes2)
imshow(handles.obr2);

figure
imshow(sachovnice(handles.obr1_orig, handles.obr2_kvadrat),[])
set(handles.status_text,'String','Hotovo');
guidata(hObject, handles);

function korespondence_pushbutton_Callback(hObject, eventdata, handles)
set(handles.status_text,'String','Pracuji');
pause(0.1)
if handles.bifurkace_souradnice1 == 0
    disp('bifurkace')
    [obr_bifurkace1,handles.obr1_bif,handles.bifurkace_souradnice1] = komplexni(handles.obr1_orig);
end
% wait_interruptible()
%% nacteni snimku a souradnic bifurkaci
souradnice = handles.bifurkace_souradnice1;
snimek1 = handles.obr1_orig;
snimek2 = handles.obr2_orig;

y1=souradnice(1,:);
x1=souradnice(2,:);

x1(y1==0)=[];
y1(y1==0)=[];
%% nalezeni korespondujicich bifurkaci v druhem snimku

[snimek1,snimek2_slicovany]=fazova_korelace(snimek1,snimek2,0,1);



if size(snimek1,3)==3
    f1=(snimek1(:,:,2));
else
    f1=(snimek1(:,:,1));
end

if size(snimek2,3)==3
    f2=(snimek2_slicovany(:,:,2));
else
    f2=(snimek2_slicovany(:,:,1));
end

maska1=zeros(size(f1));
maska1(f1>=5)=1;
maska1=bwmorph(maska1,'dilate',10);
maska1=bwmorph(maska1,'erode',70);
maska2=zeros(size(f2));
maska2(f2>=5)=1;
maska2=bwmorph(maska2,'dilate',10);
maska2=bwmorph(maska2,'erode',70);
maska=maska1.*maska2;

x1_oblast=[];
y1_oblast=[];

j=1;
for i=1:length(x1)
    if maska(y1(i),x1(i))==1
        x1_oblast(j)=x1(i);
        y1_oblast(j)=y1(i);
        j=j+1;
    end
end

velikost=90;
x2_koresp=[];
y2_koresp=[];

for i=1:length(x1_oblast)

    vyrez1=f1(y1_oblast(i)-velikost:y1_oblast(i)+velikost,x1_oblast(i)-velikost:x1_oblast(i)+velikost,:);
    vyrez2=f2(y1_oblast(i)-velikost:y1_oblast(i)+velikost,x1_oblast(i)-velikost:x1_oblast(i)+velikost);

    [posun_x,posun_y]=lokalni_fazova_korelace(vyrez1,vyrez2);

    x2_koresp(i)=x1_oblast(i)-posun_x;
    y2_koresp(i)=y1_oblast(i)-posun_y;
end
obr_vysl = snimek2_slicovany(:,:,2);
for i = 1:length(y2_koresp)
    obr_vysl(y2_koresp(i)-3:y2_koresp(i)+3,x2_koresp(i)) = 255;
    obr_vysl(y2_koresp(i),x2_koresp(i)-3:x2_koresp(i)+3 ) = 255;
end

obr_vysl1 = snimek1(:,:,2);
for i = 1:length(y1_oblast)
    obr_vysl1(y1_oblast(i)-3:y1_oblast(i)+3,x1_oblast(i)) = 255;
    obr_vysl1(y1_oblast(i),x1_oblast(i)-3:x1_oblast(i)+3 ) = 255;
end

handles.obr1 = obr_vysl1;
axes(handles.axes1)
imshow(handles.obr1);

handles.obr2 = obr_vysl;
axes(handles.axes2)
imshow(handles.obr2);

set(handles.status_text,'String','Hotovo');
guidata(hObject, handles);
