function [sourad_lokal d] = lokalni3(obr2,pocet_x,pocet_y)

pocet_x= 17; %pocet segmentu v rovine x
pocet_y = 13; %pocet segmentu v rovine y

okoli = 10; %velikost okoli kazdeho segmetu pro vypocet
obr3 = obr2(okoli+1:end-okoli,okoli+1:end-okoli);
vel = size(obr3);
a = double(zeros(floor(vel(1)/pocet_x),floor(vel(2)/pocet_y),pocet_x,pocet_y)); %vytvoreni matice pro segmenty
vel2 = size(a);
b = double(zeros(vel2(1)+2*okoli,vel2(2)+2*okoli,pocet_x,pocet_y)); %vytvoreni matice pro segmenty


% vypocet poctu pixelu v kazdem segmentu
rozmer_x = floor(vel(1) / pocet_x); 
rozmer_y = floor(vel(2) / pocet_y);

% --------------------------------------------
% Rozdeleni obrazu na segmenty
for i = 1:pocet_x
    for j = 1:pocet_y       
        b(:,:,i,j) = obr2(rozmer_x*(i-1)+1:i*rozmer_x+2*okoli,rozmer_y*(j-1)+1:j*rozmer_y+2*okoli);
    end
end


% ----------------------------------------------
% figure(4)
% f = 1;
% for i = 1:pocet_x
%     for j = 1:pocet_y
%         subplot(pocet_x, pocet_y, f)
%         imshow(b(:,:,i,j))
%         f = f+1;
%     end
%     
% end


% ----------------------------------------------
% Hledání bifurkací
c= a;
d = a;


sourad = zeros(2,300,pocet_x,pocet_y);
count = zeros (pocet_x,pocet_y);
for i = 1:pocet_x
    for j = 1:pocet_y
        obr_p = b(:,:,i,j);
%         obr_p = obr_p - min(min(obr_p));
%         obr_p=obr_p*1/max(max(obr_p));

%         g = imhist(obr_p);
        c(:,:,i,j) = obr_p(okoli:end-okoli-1,okoli:end-okoli-1);
%         figure(22)
%         subplot(2,2,1)
%         imhist(obr_p)
%         hold on
        
%         [lvl T] = prah_iteracni(obr_p);
%         [lvl T] = prah_kittler2(obr_p);
        [lvl T] = prah_entropy_kapur(obr_p);
        
%         figure(22)
%         plot([lvl lvl],[0 1200],'Color','red')
%         hold off
%         subplot(2,2,2)
%         imshow(obr_p)
           
        obr_p = im2bw(obr_p, lvl); 
%         subplot(2,2,3)
%         imshow(obr_p)
%         subplot(2,2,4)
%         plot (T)
%         pause()
        obr_p = bwmorph(obr_p,'bridge');
        obr_p = bwmorph(obr_p,'clean');
        [L, num] = bwlabel(obr_p, 8); 
%         imwrite(obr_p,'vyrez_d.bmp');
        L = objekty(L);
        L(L>5) = 0; 
        obr_p = L;
        obr_p = bwmorph(obr_p,'thin',30);
        obr_p = bwmorph(obr_p,'dilate',3);
        obr_p = bwmorph(obr_p,'thin',10);

        obr_p = bwmorph(obr_p,'spur',4);
        obr_p = bwmorph(obr_p,'clean');
        
%         figure(23)
%         subplot(2,1,1)
%         imshow(obr_p)
        obr_p = obr_p(okoli:end-okoli-1,okoli:end-okoli-1);
%         subplot(2,1,2)
%         imshow(obr_p)        
        [sourad(:,:,i,j) count(i,j)] = krizeni3(obr_p);     %detekce krizeni
%         pause
        d(:,:,i,j) = obr_p;
    end
    
end
% ----------------------------------------------------------------------
% prepocet souradnic z vyrezu na velky obraz

pocet = 0;
for i = 1:pocet_x
    for j = 1:pocet_y
        for k = 1:count(i,j)
            pocet = pocet + 1;
            sourad_lokal(1,pocet) = sourad(1,k,i,j) + rozmer_x*(i-1)+okoli-1;
            sourad_lokal(2,pocet) = sourad(2,k,i,j) + rozmer_y*(j-1)+okoli-1;
        end
    end
end

obr_vysl = obr2;
for i = 1:length(sourad_lokal)
    obr_vysl(sourad_lokal(1,i)-3:sourad_lokal(1,i)+3,sourad_lokal(2,i)) = 1;
    
    obr_vysl(sourad_lokal(1,i),sourad_lokal(2,i)-3:sourad_lokal(2,i)+3 ) = 1;

end