function f_s = p_klasifikacia(a, b, bf, s_min, pq, r_lbp, K, q_k, metoda_lbp, metoda_kontrast, kvant, R, N, lbp_q)
% r_lbp - veľkosť oblasti pre výpočet LBP/C histogramu
switch(q_k)
    case 2
        C_med = kvant;
    case 3
        C_perc = kvant;
    case 4
        C_k = kvant;
end

[s1, s2] = size(a);

%% výpočet LBP/C histogramov segmentov
se = (unique(bf))';
segm_i = 1;
h_segmentov = cell(length(se), 2);

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

for i = se
    se_s = unique(b);
    se_s = se_s(se_s>= i & se_s<=i+1-0.001);
    se_s = se_s';
    segment_h = zeros(pq,lbp_q);
    segment_s = 0;

    for j = se_s
        % pre vypocet modeloveho histogramu vynechanie okrajovych oblasti
        [fr_n, fc_n] = find(b_n == j);
        fr_n(2:end-1) = [];
        fc_n(2:end-1) = [];
        rc = [-1; 1];
        fr_n = fr_n + rc; % suradnice riadkov okolia
        fc_n = fc_n + rc; % suradnice stlpcov okolia
        okolie = b(fr_n(1):fr_n(2), fc_n(1):fc_n(2));
        
        h = unique(okolie); % zoznam hodnôt vyskytujúcich sa v matici = hodnoty
        % segmentu + hodnoty susedov
        h = round(h);
        h(h == round(j)) = []; % vynechanie hodnoty testovaneho segmentu
        cond = isempty(h);
        if cond == 0;
            continue
        end

        [r_se_s, c_se_s] = find(b == j);
        r_se_s(2:end-1) = [];
        c_se_s(2:end-1) = [];
        n_se_s = a(r_se_s(1):r_se_s(2), c_se_s(1):c_se_s(2));
        
        switch(metoda_lbp)
            case 1
                n_segment_lbp = lbp_8_1(n_se_s);
                lbp_q = 256;
            case 2
                n_segment_lbp = lbp_12_2(n_se_s);
                lbp_q = 4096;
            case 3
                n_segment_lbp = lbp_16_3(n_se_s);
                n_segment_lbp = kvantizacia_lbp(n_segment_lbp, 4096);
                lbp_q = 4096;
            case 4
                n_segment_lbp = lbp(n_se_s,R,N,0,0);
                lbp_q = 2^N;
            case 5
                mapping = getmapping(N,'u2');
                n_segment_lbp = lbp(n_se_s,R,N,mapping,0);
                lbp_q = mapping.num;
            case 6
                mapping = getmapping(N,'ri');
                n_segment_lbp = lbp(n_se_s,R,N,mapping,0);
                lbp_q = mapping.num;
            case 7
                mapping = getmapping(N,'riu2');
                n_segment_lbp = lbp(n_se_s,R,N,mapping,0);
                lbp_q = mapping.num;
        end

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

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

        n_segment_h = (joint_histogram_d(n_segment_c, n_segment_lbp, pq, lbp_q))*K; % histogram suseda
        
        segment_h = segment_h + n_segment_h;
        s = size(n_se_s);
        segment_s = segment_s+(s(1))^2;
    end
    segment_h(segment_h<1) = 1;
    h_segmentov{segm_i,1} = segment_h;
    h_segmentov{segm_i,2} = segment_s;
    h_segmentov{segm_i,3} = segment_s/lbp_q;
    segm_i = segm_i + 1;
end

%% vyhľadanie hraničných pixelov

rr = r_lbp/2-floor(r_lbp/2);
if rr == 0
   lh = r_lbp/2-1; % ľavý & horný
   pd = r_lbp/2; % pravý & dolný
else lh = floor(r_lbp/2);
     pd = floor(r_lbp/2);
end

b_z = zeros(s1, s2); 
for i = lh+1:s1 - pd;
    for j = lh+1:s2 - pd
        C = bf(i-1:i+1,j-1:j+1);
        maska = [0, 1, 0;
                 1, 0, 1;
                 0, 1, 0];
        SS = sum(sum(C.*maska))/4;
        if SS ~= C(2, 2)
           b_z(i, j) = 1;
        end
    end
end
% b_z = bwmorph(b_z,'thin'); % stenšenie
[rb, cb] = find(b_z); % súradnice hraničných pixelov
in(:,1) = rb;
in(:,2) = cb;


%% klasifikácia hraničných pixelov
for ii = 1:2*s_min
% for ii = 1:s_min
    in_s = size(in);
    in_n = zeros(in_s(1), 2);
    poc3 = 0;
    for i = 1: in_s(1)
        obl = a(in(i,1)-lh:in(i,1)+pd, in(i,2)-lh:in(i,2)+pd);
        
        switch(metoda_lbp)
            case 1
                obl_lbp = lbp_8_1(obl);
                lbp_q = 256;
            case 2
                obl_lbp = lbp_12_2(obl);
                lbp_q = 4096;
            case 3
                obl_lbp = lbp_16_3(obl);
                obl_lbp = kvantizacia_lbp(obl_lbp, 4096);
                lbp_q = 4096;
            case 4
                obl_lbp = lbp(obl,R,N,0,0);
                lbp_q = 2^N;
            case 5
                mapping = getmapping(N,'u2');
                obl_lbp = lbp(obl,R,N,mapping,0);
                lbp_q = mapping.num;
            case 6
                mapping = getmapping(N,'ri');
                obl_lbp = lbp(obl,R,N,mapping,0);
                lbp_q = mapping.num;
            case 7
                mapping = getmapping(N,'riu2');
                obl_lbp = lbp(obl,R,N,mapping,0);
                lbp_q = mapping.num;
        end

        switch(metoda_kontrast)
            case 1
                obl_c = kontrast_8_1(obl);
            case 2
                obl_c = kontrast_12_2(obl);
            case 3
                obl_c = kontrast_16_3(obl);
            case 4
                obl_c = cont(obl,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

        obl_h = (joint_histogram_d(obl_c, obl_lbp, pq, lbp_q))*K;  % lbp histogram testovaneho segmentu
        obl_h(obl_h<1) = 1;
        ss_s = zeros(1,4);
        ss_s(1) = bf(in(i,1)-1, in(i,2));
        ss_s(2) = bf(in(i,1), in(i,2)-1);
        ss_s(3) = bf(in(i,1)+1, in(i,2));
        ss_s(4) = bf(in(i,1), in(i,2)+1);
        ss_s = unique(ss_s);
        G_pc = zeros(length(ss_s),1);
        for j = 1:length(ss_s)
            poz = se == ss_s(j);
            G_pc(j) = g_stat(h_segmentov{poz,1}, obl_h);
        end
        [~, index] = min(G_pc(:));
        pp = bf(in(i,1), in(i,2)); % označenie daného pixelu

        % overenie existencie suseda s možnou zmenou označenia
        N = [in(i,1)-1, in(i,2)];
        S = [in(i,1)+1, in(i,2)];
        W = [in(i,1), in(i,2)-1];
        E = [in(i,1), in(i,2)+1];
        
        Ne = sum(in(:, 1) == N(1) & in(:, 2) == N(2));
        Se = sum(in(:, 1) == S(1) & in(:, 2) == S(2));
        We = sum(in(:, 1) == W(1) & in(:, 2) == W(2));
        Ee = sum(in(:, 1) == E(1) & in(:, 2) == E(2));
        
        ex = Ne + Se + We + Ee;
        if ss_s(index) ~= pp && ex > 0
%         if ss_s(index) ~= pp
           poc3 = poc3+1;
           bf(in(i,1), in(i,2)) = ss_s(index);
           in_n(poc3,:) = [in(i,1), in(i,2)];
        end
    end
    in_n(in_n(:, 1)== 0, :)= [];
    in_n_l = size(in_n);
    
    in = zeros(in_n_l(1)*4,2);
    poc4 = 1;
    for k = 1:in_n_l(1)
        in(poc4,:) = [in_n(k,1)-1, in_n(k,2)];
        in(poc4+1,:) = [in_n(k,1)+1, in_n(k,2)];
        in(poc4+2,:) = [in_n(k,1), in_n(k,2)-1];
        in(poc4+3,:) = [in_n(k,1), in_n(k,2)+1];
        poc4 = poc4+4;
    end
%     odstranenie susedov mimo dosah testovania
    in(in(:, 1)< lh+1, :)= [];
    in(in(:, 2)< lh+1, :)= [];
    in(in(:, 1)> s1-pd, :)= [];
    in(in(:, 2)> s2-pd, :)= [];
    if sum(sum(in)) == 0
        break
    end
end

mark = 1;
for i = 1:length(se)
    bf(bf == se(i)) = mark;
    mark = mark+1;
end
f_s = bf;