function [susedia,h_s,h_s_s] = n_susedia_s(a, b, v, pq, K, q_k, metoda_lbp, metoda_kontrast, kvant, R, N, lbp_q)
% pq - počet kvantizačných úrovní C_m
% obmedzenie indexovania mimo maticu

switch(q_k)
    case 2
        C_med = kvant;
    case 3
        C_perc = kvant;
    case 4
        C_k = kvant;
end

b_n = b;
b_n(1,:) = NaN;
b_n(end,:) = NaN;
b_n(:,1) = NaN;
b_n(:,end) = NaN;

pocitadlo = 1;
segment = ones(200,1)*(-1);
susedia = ones(200,1)*(-1);
poc_s = 1; % pocitadlo susedia

h_s = zeros(pq,lbp_q); % histogram nového segmentu
h_s_s = 0; % počet pixelov nového segmentu
for m = v % prechádzanie všetkých podoblastí nového segmentu
    [r_n, c_n] = find(b_n == m);
    ukoncenie = isempty(r_n);
    if ukoncenie == 1;
       break
    end
    r_n(2:end-1) = [];
    c_n(2:end-1) = [];
%     ro = [r_n(1)-1, r_n(2)+1]; % suradnice riadkov okolia
%     co = [c_n(1)-1, c_n(2)+1]; % suradnice stlpcov okolia
    okolie = b(r_n(1)-1:r_n(2)+1, c_n(1)-1:c_n(2)+1);
    oblast = a(r_n(1):r_n(2), c_n(1):c_n(2));
    s = size(oblast);
    
    switch(metoda_lbp)
        case 1
            obl_lbp = lbp_8_1(oblast);
            lbp_q = 256;
        case 2
            obl_lbp = lbp_12_2(oblast);
            lbp_q = 4096;
        case 3
            obl_lbp = lbp_16_3(oblast);
            obl_lbp = kvantizacia_lbp(obl_lbp, 4096);
            lbp_q = 4096;
        case 4
            obl_lbp = lbp(oblast,R,N,0,0);
            lbp_q = 2^N;
        case 5
            mapping = getmapping(N,'u2');
            obl_lbp = lbp(oblast,R,N,mapping,0);
            lbp_q = mapping.num;
        case 6
            mapping = getmapping(N,'ri');
            obl_lbp = lbp(oblast,R,N,mapping,0);
            lbp_q = mapping.num;
        case 7
            mapping = getmapping(N,'riu2');
            obl_lbp = lbp(oblast,R,N,mapping,0);
            lbp_q = mapping.num;
    end

    switch(metoda_kontrast)
        case 1
            obl_c = kontrast_8_1(oblast);
        case 2
            obl_c = kontrast_12_2(oblast);
        case 3
            obl_c = kontrast_16_3(oblast);
        case 4
            obl_c = cont(oblast,R,N);
    end

    switch(q_k)
        case 1
        obl_c = kvantizacia_C(obl_c, pq);
        case 2
        obl_c = kvantizacia_C_median_iqr(obl_c, C_med);
        case 3
        obl_c = kvantizacia_C_percentily(obl_c, C_perc);
        case 4
        obl_c = kvantizacia_C_kvantily_iqr(obl_c, C_k);
    end

    hist_obl = (joint_histogram_d(obl_c, obl_lbp, pq, lbp_q))*K;  % lbp histogram testovaneho segmentu
    
    h = unique(okolie); % zoznam hodnôt vyskytujúcich sa v matici
    % vynechanie susedov po diagonalach
    hs = histc(okolie(:),h); % počet výskytov daných hodnôt v matici
    ind = hs == 1;
    %     ind = find(hs == 1);
    h(ind) = [];
    h(h == m) = []; % vynechanie hodnoty testovaneho segmentu
    h = h';
    hl = length(h);
    susedia(poc_s:poc_s+hl-1,1) = h;
    poc_s = poc_s+hl; % upravenie pozicie pre dalsi cyklus
    segment(pocitadlo,1) = m;
    h_s = h_s+hist_obl;
    pocitadlo = pocitadlo+1;
    
    h_s_s = h_s_s+(s(1))^2;
end

% histogram_oblasti = histogram_oblasti/(pocitadlo-1);
% histogram_oblasti = round(histogram_oblasti/(pocitadlo-1));

segment(segment<0)=[];
susedia(susedia<0)=[];
susedia = unique(susedia);
susedia(susedia<=max(segment) & susedia>=(min(segment))) = [];
susedia = susedia';

