function binary = region_from_seed(img, y, x, thresh_low, thresh_high)
% funkce provadi segmentaci pomoci rustu oblasti ze seminka s pozici
% urcenou hodnotami x a y. Rust oblasti je zalozen podobnosti hodnot novych
% pixelu s prumernou jasovou hodnotou segmentovane oblasti

img = reshape(normalize(img(:), 'range'), size(img));

nhood = ones(3);

binary = false(size(img)); % Output
binary(y, x) = 1;

mozne_pixely = true(size(img)); % pixely, ktere muzou byt soucasti oblasti


changed_region_size = true;
while changed_region_size
    
    candidates = find((imdilate(binary, nhood) - binary) & mozne_pixely); % get potential neighborhood pixels
    
    if ~any(candidates)
        break
    end
    
    zmena = false;
    for k = 1:numel(candidates)
        region_mean = mean(img(binary)); % get current mean value of region
        
        if img(candidates(k)) > (region_mean - thresh_low) && img(candidates(k)) < (region_mean + thresh_high)
            zmena = true;
            binary(candidates(k)) = true;
        else
            mozne_pixely(candidates(k)) = false;
        end
    end
                  
    if ~zmena
        changed_region_size = false;
    end
    
%     imshow(binary); pause
    
end

end

