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

% Last Modified by GUIDE v2.5 25-May-2015 16:04:24

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = bakalarkagalanova_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 gobraz gcol f1 f2 f3 gT1n gT1s mgT1s mgT1n Z TI

%jednosmerná cesta  spúšťania programu
set(handles.pushbutton7,'enable','on')  
set(handles.pushbutton1,'enable','off')
set(handles.pushbutton2,'enable','off')
set(handles.pushbutton3,'enable','off')


%normalizácia pôvodného obrazu a zobrazenie pôvodného obrazu
f1=0;
f2=0;
f3=0;
obraz=cell2mat(Z(1,1));
axes(handles.axes1)
% obraz=((obraz-min(min(obraz)))/max(max(obraz)))*255; 
obraz=obraz';
gobraz=obraz;
% col=colormap;
% gcol=col;
imagesc(obraz) 


% --- 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 gcol gROI f1 mgT1s mgT1n f2 gT1s gT1n Z TI l gobraz

%aplikovanie ROI na vyznačenú oblasť aj u T1 máp
for u=1:size(Z,1) 
    for v=1:size(Z,2)
        temp=cell2mat(Z(u,v));
        temp=temp.*gROI';
        [t c]=find(temp~=0);
th=max(t)-min(t);
ch=max(c)-min(c);
out=zeros(th,ch);
for i=min(t):max(t)
    for j=min(c):max(c)
    out(i-min(t)+1,j-min(c)+1)=temp(i,j);
    end
end
        Z(u,v)={out};
    end
end


%použitie lsqcurvefit na výpočet M0 a T1 časov
[M0,T1_lsf,aa]=T1mapy(Z,TI);

%spätný výpočet matíc z rovnice a M0 a T1
for ind1=1:size(M0,1) 
for ind2=1:size(M0,2)
    tat1(ind1,ind2,:)=rovnica([M0(ind1,ind2,1) T1_lsf(ind1,ind2,1)],TI(1,7));
    tat2(ind1,ind2,:)=rovnica([M0(ind1,ind2,2) T1_lsf(ind1,ind2,2)],TI(2,7));
    
end
end
%tast=rovnica(aa,TI);

% T1s=T1_lsf(:,:,1)';

%normalizácia 
 T1s=tat1';
% T1s=(((T1s-min(min(T1s)))/max(max(T1s)))*255);
gT1s=T1s;
% T1n=T1_lsf(:,:,2)';
 T1n=tat2';
% T1n=(((T1n-min(min(T1n)))/max(max(T1n)))*255);
gT1n=T1n;

%zobrazenie
axes(handles.axes3)
imagesc(gT1s)
axes(handles.axes4)
imagesc(gT1n)

global T1_lsf

set(handles.pushbutton7,'enable','off')
set(handles.pushbutton1,'enable','off')
set(handles.pushbutton2,'enable','off')
set(handles.pushbutton3,'enable','on')



% --- 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)
set(handles.pushbutton7,'enable','off')
set(handles.pushbutton1,'enable','off')
set(handles.pushbutton2,'enable','off')
set(handles.pushbutton3,'enable','off')


global gcol gROI f1 f2 f3 gperf mgT1s mgT1n gvepr T1_lsf gobraz 
f3=1;

%výpočet perfúznej mapy
aaa2=T1_lsf;

for i=1:size(aaa2,1)
           for j=1:size(aaa2,2)
                perf(i,j)=4980*(aaa2(i,j,2)/2400)*((1/aaa2(i,j,1))-(aaa2(i,j,2)));
           end
end

%zobrazenie perfúznej mapy
perf=abs(perf);
perf=perf';

       gperf = perf;
       axes(handles.axes2)
       
 gvepr=gobraz;
      
       pu=find(gvepr<10);
       gvepr(pu)=0;
       gvepr=gvepr/max(max(gvepr));
       gvepr=gvepr*35;
       perf=perf+gvepr;
        imagesc(perf)
       
       
      

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

%zobrazenie všetkých pôvodných obrazov
load Z.mat
for j=1:15
    p1=cell2mat(Z(1,j));
    f1=cell2mat(Z(2,j));
%     p1=p1/max(max(p1));
%     p1=p1*255;
%     f1=f1/max(max(f1));
%     f1=f1*255;
    figure
    subplot(1,2,1)
    
    imagesc(p1);
    nazev1 = ['Znacene obrazy, TI = ' num2str(TI(1,j))]
    title(nazev1)
    subplot(1,2,2)
    imagesc(f1);
    nazev2 = ['Neznacene obrazy, TI = ' num2str(TI(2,j))]
    title(nazev2)
end


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

%uloženie perfúznej mapy do .png
global gperf gcol
imwrite(gperf,'perfúzna_mapa.png','png')

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

%uloženie perfúznej mapy do .mat
global gperf
save('perfúzna_mapa.mat','gperf')

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

%vytvorenie  ROI 
global gobraz gROI gcol gperf f1 mgT1s mgT1n f2 f3 gT1s gT1n gvepr
mgT1s=[];
mgT1n=[];
axes(handles.axes1)
f2=1;
[x y]=ginput(5)
ROI = roipoly(gobraz,x,y);
gROI=ROI;

%aplikovanie ROI na pôvodný obraz
[t c]=find(ROI~=0);
th=max(t)-min(t);
ch=max(c)-min(c);

obraz2=ROI.*gobraz;
ma=zeros(th,ch);
for i=min(t):max(t)
    for j=min(c):max(c)
    ma(i-min(t)+1,j-min(c)+1)=obraz2(i,j);
    end
end
clear t c
imagesc(ma)
gobraz=ma;
set(handles.pushbutton7,'enable','off')
set(handles.pushbutton1,'enable','off')
set(handles.pushbutton2,'enable','on')
set(handles.pushbutton3,'enable','off')

[m n]=size(gobraz);

%výpočty z ROI
cuba=0;
for i=1:m
        for j=1:n
            if gobraz(i,j)~=0
                cuba=cuba+1;
                vec(cuba)=gobraz(i,j);
            end
        end
end
set(handles.text10,'String',num2str(sum(vec)/cuba))
set(handles.text9,'String',num2str(median(vec)))
set(handles.text11,'String',num2str(std(vec)))


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

%načítanie dát
set(handles.pushbutton7,'enable','off')
set(handles.pushbutton1,'enable','on')
set(handles.pushbutton2,'enable','off')
set(handles.pushbutton3,'enable','off')

run nacitaniedat
load Z.mat
global Z TI l
