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

% Last Modified by GUIDE v2.5 23-May-2015 18:11:24

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = GUICKO_02_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)
global prumjas prumjas01 prumjas02 prumjas03 prumjas04 a b c d e M myFolder 
delete(a)
delete(b)
delete(c)
delete(d)
delete(e)
global imageArray
myFolder = uigetdir;

 if ~isdir(myFolder) %vyber slozky
  errorMessage = sprintf('Pozor: Tato složka neexsistuje:\n%s', myFolder);
  uiwait(warndlg(errorMessage));
  return;
end
filePattern = fullfile(myFolder, '*.tif');
jpegFiles = dir(filePattern);

for k = 1:length(jpegFiles) %Tvorba imageArray
  baseFileName = jpegFiles(k).name;
  fullFileName = fullfile(myFolder, baseFileName);
  fprintf(1, 'Now reading %s\n', fullFileName);
  imageArray(k).image = imread(fullFileName);
  imageArray(k).image =im2double(imageArray(k).image);
  drawnow; 
end


axes(handles.axes1);
imshow(imageArray(M).image) %Vykresleni zvoleneho obrazku



a = imfreehand(gca);      %tvorba ROI + barvene znaceni car
setColor(a,[0 0 255]/255)
b = imfreehand(gca); 
setColor(b,[0 128 0]/255)
c = imfreehand(gca);
setColor(c,[255 0 0]/255)
d = imfreehand(gca);
setColor(d,[0 191 191]/255)
e = imfreehand(gca);
setColor(e,[191 0  191]/255)

oblast = createMask(a); %tvorba masky podle ROI
oblast01 = createMask(b);
oblast02 = createMask(c);
oblast03 = createMask(d);
oblast04 = createMask(e);

H = bwlabel(oblast);
CH = bwlabel(oblast01);
I = bwlabel(oblast02);
J = bwlabel(oblast03);
K = bwlabel(oblast04);

STATS = regionprops(H, 'Extrema'); %hranice ROI
STATS01 = regionprops(CH, 'Extrema');
STATS02 = regionprops(I, 'Extrema');
STATS03 = regionprops(J, 'Extrema');
STATS04 = regionprops(K, 'Extrema');

minimumY=min(STATS(1).Extrema(:,2));
maximumY=max(STATS(1).Extrema(:,2));
minimumX=min(STATS(1).Extrema(:,1));
maximumX=max(STATS(1).Extrema(:,1));

minimumY01=min(STATS01(1).Extrema(:,2));
maximumY01=max(STATS01(1).Extrema(:,2));
minimumX01=min(STATS01(1).Extrema(:,1));
maximumX01=max(STATS01(1).Extrema(:,1));

minimumY02=min(STATS02(1).Extrema(:,2));
maximumY02=max(STATS02(1).Extrema(:,2));
minimumX02=min(STATS02(1).Extrema(:,1));
maximumX02=max(STATS02(1).Extrema(:,1));

minimumY03=min(STATS03(1).Extrema(:,2));
maximumY03=max(STATS03(1).Extrema(:,2));
minimumX03=min(STATS03(1).Extrema(:,1));
maximumX03=max(STATS03(1).Extrema(:,1));

minimumY04=min(STATS04(1).Extrema(:,2));
maximumY04=max(STATS04(1).Extrema(:,2));
minimumX04=min(STATS04(1).Extrema(:,1));
maximumX04=max(STATS04(1).Extrema(:,1));

prumjas=zeros(1,length(imageArray)); %soucet vsech ROI
prumjas01=zeros(1,length(imageArray));
prumjas02=zeros(1,length(imageArray));
prumjas03=zeros(1,length(imageArray));
prumjas04=zeros(1,length(imageArray));
i = [];
for i=1:length(imageArray) 
    X=(imageArray(i).image); 
    
zoom=X(minimumY:maximumY,minimumX:maximumX,:); %ROI urceni
zoom01=X(minimumY01:maximumY01,minimumX01:maximumX01,:);
zoom02=X(minimumY02:maximumY02,minimumX02:maximumX02,:);
zoom03=X(minimumY03:maximumY03,minimumX03:maximumX03,:);
zoom04=X(minimumY04:maximumY04,minimumX04:maximumX04,:);

prumjas(1,i) = mean2(zoom(:,:)); %prumerovani intenzit v ROI
prumjas01(1,i) = mean2(zoom01(:,:));
prumjas02(1,i) = mean2(zoom02(:,:));
prumjas03(1,i) = mean2(zoom03(:,:));
prumjas04(1,i) = mean2(zoom04(:,:));

end


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


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
UpravOsySpektra(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)


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


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
UpravOsyAfterglow(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)



function edit1_Callback(hObject, eventdata, handles)
UpravOsy(hObject, eventdata, handles)
global imageArray

% 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

function UpravOsySpektra(hObject, eventdata, handles)
global prumjas prumjas01 prumjas02 prumjas03 prumjas04 imageArray
VlnovaDelkaPoc = str2double(get(handles.edit1,'string'));
VlnovaDelkaKon = str2double(get(handles.edit2,'string'));

osax=linspace(VlnovaDelkaPoc,VlnovaDelkaKon,length(imageArray));% rozmer osy x
Pom = [prumjas;prumjas01;prumjas02;prumjas03;prumjas04];
set(handles.axes2, 'XTick', osax)
set(handles.axes2, 'XTickLabel', num2str(osax'))
axes(handles.axes2)
plot(osax,Pom)
legend('ROI1','ROI2','ROI3','ROI4','ROI5')
axis([osax(1) osax(end) min(Pom(:)) max(Pom(:))])



function UpravOsyAfterglow(hObject, eventdata, handles)
global prumjas prumjas01 prumjas02 prumjas03 prumjas04 imageArray
Krok = str2double(get(handles.edit6,'string'));

OsaX = 0:length(imageArray)-1;%rozmer osy y
Pop = [ 3.5 4.5];
for i = 2:length(imageArray)
    pom = Pop(end)+Krok;
    Pop = [Pop pom];
end
Pop = Pop';

Pom = [prumjas;prumjas01;prumjas02;prumjas03;prumjas04];
axes(handles.axes3)
plot(OsaX,Pom )
legend('ROI1','ROI2','ROI3','ROI4','ROI5')
vektor=[0 3.5:1:length(imageArray)+2]';
set(handles.axes3, 'XTickLabel', num2str(vektor))








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


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (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 edit6_Callback(hObject, eventdata, handles)
% hObject    handle to edit6 (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 edit6 as text
%        str2double(get(hObject,'String')) returns contents of edit6 as a double


% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit6 (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 Reset.
function Reset_Callback(hObject, eventdata, handles)
global prumjas prumjas01 prumjas02 prumjas03 prumjas04 imageArray a b c d e
prumjas = [];
prumjas01 = [];
prumjas02 = [];
prumjas03 = [];
prumjas04 = [];
imageArray = [];
axes(handles.axes1)
cla
axes(handles.axes2)
cla
axes(handles.axes3)
cla
delete(a)
delete(b)
delete(c)
delete(d)
delete(e)
% hObject    handle to Reset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



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


% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit7 (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 edit8_Callback(hObject, eventdata, handles)
% hObject    handle to edit8 (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 edit8 as text
%        str2double(get(hObject,'String')) returns contents of edit8 as a double


% --- Executes during object creation, after setting all properties.
function edit8_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit8 (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 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)
global myFolder
pseudogui




function edit10_Callback(hObject, eventdata, handles)
% hObject    handle to edit10 (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 edit10 as text
%        str2double(get(hObject,'String')) returns contents of edit10 as a double
global M
M = str2double(get(handles.edit10,'string'));
% --- Executes during object creation, after setting all properties.
function edit10_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit10 (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
