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

% Last Modified by GUIDE v2.5 23-May-2016 15:40:18

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = vyhodnoceni_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 back.
function back_Callback(hObject, eventdata, handles)
global rez
ted=rez;
rez=rez-1;
if rez<1
    wdl=warndlg('Minimal value is 1','Error!')
    set(wdl,'Windowstyle','modal')
    waitfor(wdl)
    rez=ted;
end

vykresli(hObject, eventdata, handles)



% --- Executes on button press in next.
function next_Callback(hObject, eventdata, handles)
global rezmax
global rez

ted=rez;
rez=rez+1;
 if rez>rezmax
    wdl=warndlg(['Maximal value is ', num2str(rezmax)],'Error!')
    set(wdl,'Windowstyle','modal')
    waitfor(wdl)
    rez=ted;
 end

vykresli(hObject, eventdata, handles)

% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function imgload_Callback(hObject, eventdata, handles)
global fixed
global moving
global mozaika
global rez
global rezmax
global b

fixed=[];
moving=[];
mozaika=[];

[refnazev, refcesta]=uigetfile({'*.img'}, 'Choose fixed image');  % okno pro volbu referenčního obrazu
r4D=analyze75read([refcesta, refnazev]);

[nazev, cesta]=uigetfile({'*.img'}, 'Choose transformed image');  % okno pro volbu transformovaného obrazu
z4D=analyze75read([cesta, nazev]);

a=size(r4D);% prevod obrazu do 3D
b=length(a);
if b==4
     a=a(4);
     rezmax=a;
     for i=1:a
          r3D(:,:,i)=r4D(:,:,1,i);
          z3D(:,:,i)=z4D(:,:,1,i);
     end
   fixed=int16(z3D);    %převod obrazu do stejného datového formátu
   moving=int16(r3D);
   
elseif b==3 
    
a=size(r4D);% prevod obrazu do 3D
b=length(a)
a=a(3)
     moving=r4D;
     fixed=z4D;
     rezmax=a;
else 
    klp=warndlg('Dimension image must be 2D or 3D','Error!')
    set(klp,'Windowstyle','modal')
    waitfor(klp)
end


% moving=double(moving); %%uprava pro konkretní obraz z FSL
% 
min1=min(fixed);
min2=min(moving);



 for i=1:rezmax
       mozaika(:,:,i)=cbimage(fixed(:,:,i),moving(:,:,i));
 end

 rez=1;
 
vykresli(hObject, eventdata, handles)

function vykresli(hObject, eventdata, handles)
global fixed
global moving
global mozaika
global rez
global b

  moz=double(mozaika(:,:,rez));
  fix=double(fixed(:,:,rez));
  mov=double(moving(:,:,rez));

axes(handles.axes1)
imagesc(fix)
colormap(gray)
set(gca,'visible','off')
title('Fixed')

axes(handles.axes2)
imagesc(mov)
colormap(gray)
set(gca,'visible','off')
title('Moving')

if(get(handles.moza,'Value')==1)
axes(handles.axes3)
imagesc(moz)
colormap(gray)
set(gca,'visible','off')

axes(handles.axes4)
plot(0,0)
set(handles.axes4,'Visible','off')
set(gca,'visible','off')

elseif (get(handles.mult,'Value')==1)
    axes(handles.axes3)
    histogram(fix,45);
    hold on
    histogram(mov,45)
    hold off
    title('Multiple histogram')
    
else
set(handles.axes4,'Visible','on')

axes(handles.axes4)
histogram(mov,45);
title('moving')

axes(handles.axes3)
histogram(fix,45,'Normalization','pdf');
title('fixed')

end




% --------------------------------------------------------------------
function tiffload_Callback(hObject, eventdata, handles)
% hObject    handle to tiffload (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global fixed
global moving
global mozaika 
global rez 
global rezmax

fixed=[];
moving=[];
mozaika=[];

[refnazev, refcesta]=uigetfile({'*.tif'}, 'Choose fixed image');  % okno pro volbu referenčního obrazu
fixed=imread([refcesta, refnazev]);
fixed=fixed(:,:,1);

[nazev, cesta]=uigetfile({'*.tif'}, 'Choose transformed image');  % okno pro volbu transformovaného obrazu
moving=imread([cesta, nazev]);
moving=moving(:,:,1);

rezmax=1;
mozaika=[];
mozaika=cbimage(fixed,moving);

rez=1;
vykresli(hObject, eventdata, handles)

% --- Executes on button press in moza.
function moza_Callback(hObject, eventdata, handles)
vykresli(hObject, eventdata, handles)


% --- Executes on button press in hist.
function hist_Callback(hObject, eventdata, handles)
vykresli(hObject, eventdata, handles)


% --- Executes on button press in bod.
function bod_Callback(hObject, eventdata, handles)
global rez
global mozaika
global fixed
global moving

% [x,y]=ginputc(1,'Color','b'); % zobrazení měřícího kříže a uložení souřadnic
[x,y] = ginput(1);
moz=double(mozaika(:,:,rez));
fix=double(fixed(:,:,rez));
mov=double(moving(:,:,rez));
x=abs(x);
y=y;

axes(handles.axes1)
imagesc(fix)
colormap(gray)
hold on

plot(x,y,'r+','MarkerSize',20)
set(gca,'visible','off')
title('fixed')
hold off

axes(handles.axes2)
imagesc(mov)
colormap(gray)
hold on
plot(x,y,'r+','MarkerSize',20)
set(gca,'visible','off')
title('moving')
hold off


% --- Executes on button press in mult.
function mult_Callback(hObject, eventdata, handles)
vykresli(hObject, eventdata, handles)


% % --- Executes on button press in rot.
function rot_Callback(hObject, eventdata, handles)
global moving
global mozaika
global fixed

moving=imrotate(moving,90);
fixed=imrotate(fixed,90);
mozaika=imrotate(mozaika,90);
vykresli(hObject, eventdata, handles)
