function [FP pocetFP ] = ST_detect (image, ST_pocet)
    %% Shi Tomasi detektor - modifikacia Harrisovho detektora s vahovanim 
    %za pomoci vlastnych cisel
    
    %% Premenné
%     ST_pocet =      % nastavenie poctu vyznačných bodov
    
    %%
    img = im2double(image);
    
%     vyhladzovaci filter

    f1 = fspecial ('gauss', [6 6], 1.5);
    fd = imfilter(f1, [-1 1], 'rep');
    fd = fd(1:5,1:5);    %pouzitie HOG gradientu
    
    imgX = imfilter(img,fd, 'rep');
    imgY = imfilter(img,fd', 'rep');
    
%     vyhladzovaci filter s mensim vyhladenim

    ker = fspecial('gauss',[5 5],1.5); 
    imgX2 = imfilter(imgX.^2,ker,'rep');
    imgXY = imfilter(imgX.*imgY,ker,'rep');
    imgY2 = imfilter(imgY.^2,ker,'rep');
    
    a = zeros(2,2,numel(img));
    a(1,1,:) = imgX2(:);
    a(1,2,:) = imgXY(:);
    a(2,1,:) = imgXY(:);
    a(2,2,:) = imgY2(:);
    ev = real(eigenval(a));     % vypocet "vlastnych" hodnot
        
    evTh = .1;
    minEv = min(ev,[],2);
    minEv = reshape(minEv,size(img));
    R = (minEv > evTh*max(max(minEv)));
    
    Rmax = ordfilt2(minEv,9,ones(3));
    
    %potlacenie ne-maximalnych hodnot
    
    Rfeature = (minEv == Rmax) & R; 
    
    I = find(Rfeature);
    
%     Najdenie ST_pocet bodov s najvacsimi vlastnymi hodnotami
    
    if exist('ptNum','var') && nnz(Rfeature) > ST_pocet
        vyberEv = minEv(I);
        [~,I1] = sort(vyberEv,'descend');
        I = I(I1(1:ST_pocet));
    end
    
%     Ulozenie suradnic FP do premennej a vypocet poctu bodov

   [r c] = ind2sub(size(img),I); 
    FP(:,1) = r(:,1);
    FP(:,2) = c(:,1);
    pocetFP = size(FP,1);
end


function ev = eigenval(a)
% Funkcia pre vypocet vlastnych cisel bodov

    b = -(a(1,1,:)+a(2,2,:));
    detA = a(1,1,:).*a(2,2,:)-a(1,2,:).*a(2,2,:);
    delta = b.^2-4*detA;
    ev = [-b+sqrt(delta),-b-sqrt(delta)]/2; 
    ev = reshape(ev,2,[])'; % N-by-2
end