clc
clear all
close all

%nacist obrazek a oriznout obrazek
I0=imread('DSC_2231.jpg');

figure(1)
I=imcrop(I0);


%1. detekce zivych bunek

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

figure(2);
imshow(I3);


%automaticka uprava kontrastu
I5=imadjust(I3);

figure(3)
imshow(I5);

%histogram (zeleny kanal)
figure(4)
imhist(I5);


%prahovani
prah = 0.7;
BW=im2bw(I5,prah);

figure(5)
imshow(BW);


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


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

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

hold on

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

%pocet bunek
l=length(B);
disp('Počet živých buněk:  '  )  ,disp(num2str(l))





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

figure(8);
imshow(I3);


%automaticka uprava kontrastu
I9=imadjust(I7);

figure(9)
imshow(I9)

%histogram (cerveny kanal)
figure(10)
imhist(I9);

%prahovani
prah = 0.5;
BW=im2bw(I9,prah);

figure(11)
imshow(BW);


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


%
BW_opened=imopen(BW,se);
figure(12)
imshow(BW_opened,[]);

%okraje bunek
B=bwboundaries(BW_opened,'noholes');
figure(13)
imshow(I9);

hold on

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

%pocet bunek
d=length(B);
disp('pocet mrtvych bunek:')  ,disp(num2str(d))
