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

% Last Modified by GUIDE v2.5 05-May-2012 18:45:30

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = livedead_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 I0 FileName
clc
[FileName,PathName] = uigetfile('*.jpg','Vyberte obrázek');
if FileName~=0
    I0=imread(FileName);
    axes(handles.axes1)
    imshow(I0)
end

% --- Executes on button press in zpracovat.
function zpracovat_Callback(hObject, eventdata, handles)
% hObject    handle to zpracovat (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global I0 prah_zive prah_mrtve sl sd FileName

if isempty(FileName)
    run okno
else

% osetreni vstupu
if isempty(sl)
    sl=5;
end
if isempty(sd)
    sd=5;
end
if isempty(prah_zive)
    prah_zive=0;
end
if isempty(prah_mrtve)
    prah_mrtve=0;
end

% %1. detekce zivych bunek
figure;
I=imcrop(I0);
title('Oriznout')
close

%prevest barevny obrazek na sedotonovy
I2 = imadjust (I,[0 0 0; 1 1 1],[0 0 0; 0 1 0]);
I3 = rgb2gray(I2);

%zvysit kontrast
I5=imadjust(I3);

%histogram (zeleny kanal)
axes(handles.histogram_z)
imhist(I5);

%prahovani
BW=im2bw(I5,prah_zive);


%vytvorit kulovity element (se)
se=strel('disk',sl);


%odstrani z obrazu male objekty
BW_opened=imopen(BW,se);


%okraje bunek
B=bwboundaries(BW_opened,'noholes');
axes(handles.axes2)
imshow(I);

hold on

for k = 1:length(B)
    boundary = B{k};
    plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 2)
end
p_zive=length(B);
set(handles.pocetzive,'String',p_zive);

%2. detekce mrtvych bunek

%prevest barevny obrazek na cernobily
I6 = imadjust (I,[0 0 0; 1 1 1],[0 0 0; 1 0 0]);
I7 = rgb2gray(I6);


%zvysit kontrast
I9=imadjust(I7);

%histogram (cerveny kanal)
axes(handles.histogram_m)
imhist(I9);

%prahovani
BW=im2bw(I9,prah_mrtve);


%vytvorit kulovity element (se)
se=strel('disk',sd);


BW_opened=imopen(BW,se);


%okraje bunek
B=bwboundaries(BW_opened,'noholes');
axes(handles.axes3)
imshow(I);

hold on

for j = 1:length(B)
    boundary = B{j};
    plot(boundary(:,2), boundary(:,1), 'r', 'LineWidth', 2)
end

%pocet bunek
p_mrtve=length(B);
set(handles.pocetmrtve,'String',p_mrtve);

p_celkove=p_zive+p_mrtve;
p_zive_procenta=round(p_zive/p_celkove*100);
p_mrtve_procenta=100-p_zive_procenta;
set(handles.procenta_zive,'String',p_zive_procenta);
set(handles.procenta_mrtve,'String',p_mrtve_procenta);

end


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


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


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


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


% --- Executes during object creation, after setting all properties.
function procenta_mrtve_CreateFcn(hObject, eventdata, handles)
% hObject    handle to procenta_mrtve (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 ukoncit.
function ukoncit_Callback(hObject, eventdata, handles)
% hObject    handle to ukoncit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
clear all
clc
close



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


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


% --- Executes during object creation, after setting all properties.
function prah_m_CreateFcn(hObject, eventdata, handles)
% hObject    handle to prah_m (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 slider movement.
function slider_z_Callback(hObject, eventdata, handles)
% hObject    handle to slider_z (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global prah_zive
prah_zive=get(hObject,'Value');
set(handles.prah_z,'String',prah_zive);

% --- Executes during object creation, after setting all properties.
function slider_z_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_z (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider_m_Callback(hObject, eventdata, handles)
% hObject    handle to slider_m (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global prah_mrtve
prah_mrtve=get(hObject,'Value');
set(handles.prah_m,'String',prah_mrtve);

% --- Executes during object creation, after setting all properties.
function slider_m_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_m (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end



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


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


% --- Executes during object creation, after setting all properties.
function sd_m_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sd_m (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 slider movement.
function slider_sl_z_Callback(hObject, eventdata, handles)
% hObject    handle to slider_sl_z (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global sl
sl=round(get(hObject,'Value'));
set(handles.sl_z,'String',sl);

% --- Executes during object creation, after setting all properties.
function slider_sl_z_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_sl_z (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider_sd_m_Callback(hObject, eventdata, handles)
% hObject    handle to slider_sd_m (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global sd
sd=round(get(hObject,'Value'));
set(handles.sd_m,'String',sd);

% --- Executes during object creation, after setting all properties.
function slider_sd_m_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_sd_m (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes during object deletion, before destroying properties.
function prah_z_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to prah_z (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


