clc;
clear all;
close all;

ii = imread('foto.jpg');
%ii = imread('foto.png');

% extrakce R G B slozek
rr = ii(:,:,1);
g = ii(:,:,2);
b = ii(:,:,3);
 
% multiregionalni segmentace
% definice prahu - metoda Otsu
% 6 prahu, 7 oblasti
thresh = multithresh(b,6);
seg = imquantize(b,thresh);
imagesc(seg)
 
[r, s] = size(seg);
for i1 = 1:r
    for j1 = 1:s
        if seg(i1,j1) == 7 % posledni oblast
            seg2(i1,j1) = 1; % prirazeni absolutni bile
        else
            seg2(i1,j1) = 0; % absolutni cerna
        end
    end
end
 
figure()

vyska = size(ii,1);
sirka = size(ii,2);
 
stred_y = floor(vyska/2);
stred_x = floor(sirka/2);

% konverze na HSV
hsv = rgb2hsv(ii);
% jasova slozka
v = hsv(:,:,3);
% nejvyssi jas
max_v = max(max(v));
% souradnice pixelu s nejvyssim jasem
[r, s] = find(v == max_v);

% vypocet teziste
tez_y = mean(r);
tez_x = mean(s);

seg2 = insertMarker(seg2,[stred_x stred_y]);
seg2 = insertMarker(seg2,[tez_x tez_y],'x');
imshow(seg2)

figure()

ii = insertMarker(ii,[stred_x stred_y]);
ii = insertMarker(ii,[tez_x tez_y],'x');
imshow(ii)

rozdil_x = stred_x - tez_x
rozdil_y = stred_y - tez_y