clc
clear all
close all

%nacist obrazek
I=imread('DSC_7719.jpg');
figure(1)
imshow(I)


%1. detekce vsech bunek

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

figure(2);
imshow(I3);

%nastavit oriznuti obrazu
I4=imcrop(I3,[100 100 3600 2200]);

%automaticka uprava kontrastu
I5=imadjust(I4);

figure(3)
imshow(I5);

%detekce hran
I6 = edge(I5,'sobel');
figure (4)
imshow(I6);

%dilatace obrazu
s = strel('disk',6);
I7 = imdilate(I6, s);
figure (5)
imshow(I7);

%vyplneni bunek
I8 = imfill(I7, 'holes');
figure (6)
imshow(I8);

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

%odstrani z obrazu male objekty
BW_opened=imopen(I8,se);
figure(6)
imshow(BW_opened,[]);

%okraje bunek
C=bwboundaries(BW_opened,'noholes');
figure(7)
imshow(I5);

hold on

for k = 1:length(C)
    boundary = C{k};
    plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 2)
end



%2. detekce mrtvych bunek

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

%nastavit oriznuti obrazu
I11=imcrop(I10,[100 100 3600 2200]);

%automaticka uprava kontrastu
I12=imadjust(I11);

figure(8)
imshow(I12)

%prahovani
prah = 0.6;
BW=im2bw(I12,prah);

figure(9)
imshow(BW);

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

%odstraneni malych objektu
BW_opened=imopen(BW,se);
figure(10)
imshow(BW_opened,[]);

%okraje bunek
B=bwboundaries(BW_opened,'noholes');
figure(11)
imshow(I12);

hold on

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

%pocet bunek
c=length(C);
d=length(B);
l=c-d;
disp('Celkový počet buněk:  '  )  ,disp(num2str(c))
disp('Počet živých buněk:  ')  ,disp(num2str(l))
disp('pocet mrtvych bunek:')  ,disp(num2str(d))

