function [ obr_seg ] = kalibrace( obr_l, koef_prevz, souradnice, prostor, volba, prah, adapt_krit_vaha )
%funkce provadi kalibraci pro segmentaci a uklada prahy do souboru
%Kalibrace_barev.mat a Kalibrace_IR.mat
%vstupy:
%*obr_l - vstupni obrazek pro kalibraci
%*koef_prevz - pro rychlejsi kalibraci - narustani oblasti se provede prevzorkovani
%*souradnice - souradnice seminka pro narustani oblasti - vektor [x,y]
%*prostor - volba barevneho prostoru (RGB,YCbCr, HSV) a IR
%*volba - volba konkretnich barevnych slozek, viz dale:
%(1,0) - RGB (1) dale - 0: slozka R, 1: slozka R a B kombinace
%(2,0) - YCbCr (2) dale - 0: slozka Cr, 1: kombinace Cr a Cb
%(3,0) - kombinace slozek nekolika prostoru (3) dale vzdy 0 - slozka R, clozka Cr a slozka H (HSV)
%(4,0) - vstup z IR kamer (4) dale vzdy 0
%*prah - prah pro narustani oblasti - trislozkovy vektor pro jednotlive barevne slozky [k, l, m]
%*adapt_krit_vaha - kombinuje staticke a adaptivni kriterium, hodnota udava
% vyznam adaptivniho kriteria (0...1), 1 - pouze adaptivni kriterium, 0 - pouze
% staticke kriterium, staticke kriterium je pocitano z okoli zvoleneho
% stredu dlane - trislozkovy vektor pro jednotlive barevne slozky [q, r, s]
% Autor: Jan Netopil

%nutnost medianove filtrace - pripadne odstraneni jpeg artefaktu
maska=[5 5];
obr_l(:,:,1) = medfilt2(obr_l(:,:,1), maska);
obr_l(:,:,2) = medfilt2(obr_l(:,:,2), maska);
obr_l(:,:,3) = medfilt2(obr_l(:,:,3), maska);

cxy=souradnice; 
%% ******************** rozhodovaci strom pro narustani oblasti ********************
switch prostor
    case 1 
        if volba == 0 %RGB R
            prah_v=prah(1);
            adapt_krit_vaha_v=adapt_krit_vaha(1);
            obr_R=obr_l(:,:,1)./(obr_l(:,:,1)+obr_l(:,:,2)+obr_l(:,:,3));%prevod do normalizovaneho RGB - slozka R
            [ obr_seg ] = segincreasearea(obr_R, cxy, prah_v,adapt_krit_vaha_v, koef_prevz);
        else %RGB R a B
            prah_v=prah(1);
            adapt_krit_vaha_v=adapt_krit_vaha(1);
            obr_R=obr_l(:,:,1)./(obr_l(:,:,1)+obr_l(:,:,2)+obr_l(:,:,3));%prevod do normalizovaneho RGB - slozka R
            [ obr_seg1 ] = segincreasearea(obr_R, cxy, prah_v,adapt_krit_vaha_v, koef_prevz);
            
            prah_v=prah(2);
            adapt_krit_vaha_v=adapt_krit_vaha(2);
            obr_B=obr_l(:,:,3)./(obr_l(:,:,1)+obr_l(:,:,2)+obr_l(:,:,3));%prevod do normalizovaneho RGB - slozka B
            [ obr_seg2 ] = segincreasearea(obr_B, cxy, prah_v,adapt_krit_vaha_v, koef_prevz);
            obr_seg=obr_seg1.*obr_seg2;
        end
    case 2 
        if volba == 0 %YCbCr - Cr
            obrycbcr = rgb2ycbcr(obr_l);
            obr_Cr = obrycbcr(:,:,3);
            prah_v=prah(1);
            adapt_krit_vaha_v=adapt_krit_vaha(1);
            [ obr_seg ] = segincreasearea(obr_Cr, cxy, prah_v,adapt_krit_vaha_v, koef_prevz);
        else %YCbCr - Cr a Cb
            
            obrycbcr = rgb2ycbcr(obr_l);
            obr_Cr = obrycbcr(:,:,3);
            prah_v=prah(1);
            adapt_krit_vaha_v=adapt_krit_vaha(1);
            [ obr_seg1 ] = segincreasearea(obr_Cr, cxy, prah_v,adapt_krit_vaha_v, koef_prevz);
            
            
             obrycbcr = rgb2ycbcr(obr_l);
            obr_Cb = obrycbcr(:,:,2);
            prah_v=prah(2);
            adapt_krit_vaha_v=adapt_krit_vaha(2);
            [ obr_seg2 ] = segincreasearea(obr_Cb, cxy, prah_v,adapt_krit_vaha_v, koef_prevz);
            obr_seg=obr_seg1.*obr_seg2;
        end
    case 3
            prah_v=prah(1);
            adapt_krit_vaha_v=adapt_krit_vaha(1);
            obr_R=obr_l(:,:,1)./(obr_l(:,:,1)+obr_l(:,:,2)+obr_l(:,:,3));%prevod do normalizovaneho RGB - slozka R
            [ obr_seg1 ] = segincreasearea(obr_R, cxy, prah_v,adapt_krit_vaha_v, koef_prevz);
            
            obrycbcr = rgb2ycbcr(obr_l);
            obr_Cr = obrycbcr(:,:,3);
            prah_v=prah(2);
            adapt_krit_vaha_v=adapt_krit_vaha(2);
            [ obr_seg2 ] = segincreasearea(obr_Cr, cxy, prah_v,adapt_krit_vaha_v, koef_prevz);
            
            obrHSV=rgb2hsv(obr_l);
            obr_H=obrHSV(:,:,2);
            obr_H = medfilt2(obr_H, maska);
            prah_v=prah(3);
            adapt_krit_vaha_v=adapt_krit_vaha(3);
            [ obr_seg3 ] = segincreasearea(obr_H, cxy, prah_v,adapt_krit_vaha_v, koef_prevz);
            obr_seg=obr_seg1.*obr_seg2.*obr_seg3;
    case 4 %IR prostor
        prah_v=prah(1);
        adapt_krit_vaha_v=adapt_krit_vaha(1);
%         figure;
%         imshow(obr_IR);
        [ obr_seg ] = segincreasearea(rgb2gray(obr_l), cxy, prah_v,adapt_krit_vaha_v, koef_prevz);
%         figure;
%         imshow(obr_seg_IR);
    otherwise
        disp('rozmezi hodnot je 1...4')
end
disp('segmentace hotova, nasleduje vypocet hodnot z histogramu')
% ******************** konec rozhodovaciho stromu narustani oblasti********************
%% ziskani hodnot z histogramu
if prostor~=4 %data pochazi z kamery snimajici ve viditelne oblasti
    obr_rgb=obr_l;
    obr_rgb(:,:,1)=obr_rgb(:,:,1).*obr_seg;
    obr_rgb(:,:,2)=obr_rgb(:,:,2).*obr_seg;
    obr_rgb_b=obr_rgb(:,:,3).*obr_seg;obr_rgb_b(obr_rgb_b==0)=1; %prevedeni cerne barvy na modrou -> modra barva nezasahuje do histogramu barev ruky v nRGB
    obr_rgb(:,:,3)=obr_rgb_b;
%% ******************** ziskani prahovych hodnot pro normovany rgb barevny prostor ********************
    tresh_prob_r=0.001;
    tresh_prob_g=0.002;
    tresh_prob_b=0.003;

%osetreni nul
    null_var=0.001; %hodnota misto nuly pro dalsi vypocty (z duvodu deleni nulou)
    img_null=obr_rgb(:,:,1);img_null(img_null==0)=null_var;obr_rgb(:,:,1)=img_null;
    img_null=obr_rgb(:,:,2);img_null(img_null==0)=null_var;obr_rgb(:,:,2)=img_null;
    img_null=obr_rgb(:,:,3);img_null(img_null==0)=null_var;obr_rgb(:,:,3)=img_null;

%normovany rgb
    rgb_norm_r(:,:,1)=obr_rgb(:,:,1)./(obr_rgb(:,:,1)+obr_rgb(:,:,2)+obr_rgb(:,:,3));
    rgb_norm_g(:,:,2)=obr_rgb(:,:,2)./(obr_rgb(:,:,1)+obr_rgb(:,:,2)+obr_rgb(:,:,3));
    rgb_norm_b(:,:,3)=obr_rgb(:,:,3)./(obr_rgb(:,:,1)+obr_rgb(:,:,2)+obr_rgb(:,:,3));

    xbin=linspace(0,1,256); %pocet hodnot v jednom kanale (pri 32bit rgb -> 256)
    [hist_num_r,hist_x_r]=hist(rgb_norm_r,xbin);[hist_num_g,hist_x_g]=hist(rgb_norm_g,xbin);[hist_num_b,hist_x_b]=hist(rgb_norm_b,xbin); %histogramy ve vsech kanalech

    hist_sum_r=sum(hist_num_r,2);hist_sum_g=sum(hist_num_g,2);hist_sum_b=sum(hist_num_b,2); %ziskani histogramu
    hist_sum_r(1)=0;hist_sum_g(1)=0;hist_sum_b(1)=0;hist_sum_b(255)=0; %vymazani pridane barevne slozky (modra v RGB)
% hist_sum_r(hist_x_r<0.1)=0;hist_sum_g(hist_x_g<0.1)=0;hist_sum_b(hist_x_b>0.9)=0;hist_sum_b(hist_x_b<0.1)=0;
    hist_norm_r=hist_sum_r./sum(hist_sum_r);hist_norm_g=hist_sum_g./sum(hist_sum_g);hist_norm_b=hist_sum_b./sum(hist_sum_b); %normalizace histogramu

%nalezeni prahovyh hodnot pro barvu ruky pro nRGB
    tresh_index_r=find(hist_norm_r>tresh_prob_r);
    tresh_index_g=find(hist_norm_g>tresh_prob_g);
    tresh_index_b=find(hist_norm_b>tresh_prob_b);
    tresh_v_r=[hist_x_r(tresh_index_r(1)),hist_x_r(tresh_index_r(end))];
    tresh_v_g=[hist_x_g(tresh_index_g(1)),hist_x_g(tresh_index_g(end))];
    tresh_v_b=[hist_x_b(tresh_index_b(1)),hist_x_b(tresh_index_b(end))];

% ******************** konec ziskani prahovych hodnot pro normovany rgb ********************
%% ******************** ziskani prahovych hodnot pro YCbCr barevny prostor ********************
    obr_ycbcr = rgb2ycbcr(obr_rgb);
    ycbcr_cb=obr_ycbcr(:,:,2);
    ycbcr_cr=obr_ycbcr(:,:,3);

    tresh_prob_cb=0.005; %pst hodnoty pro nastaveni prahu z histogramu - cervena slozka
    tresh_prob_cr=0.003;

    [hist_num_cb,hist_x_cb]=hist(ycbcr_cb,xbin);[hist_num_cr,hist_x_cr]=hist(ycbcr_cr,xbin);
    hist_sum_cb=sum(hist_num_cb,2);hist_sum_cr=sum(hist_num_cr,2);
    hist_sum_cb(241)=0;hist_sum_cr(111)=0; %vymazani pridane barevne slozky (modra v RGB)
    hist_norm_cb=hist_sum_cb./sum(hist_sum_cb);hist_norm_cr=hist_sum_cr./sum(hist_sum_cr);

%nalezeni prahovych hodnot
    tresh_index_cb=find(hist_norm_cb>tresh_prob_cb);
    tresh_index_cr=find(hist_norm_cr>tresh_prob_cr);

    tresh_v_cb=[hist_x_cb(tresh_index_cb(1)),hist_x_cb(tresh_index_cb(end))];
    tresh_v_cr=[hist_x_cr(tresh_index_cr(1)),hist_x_cr(tresh_index_cr(end))];

%%
    tresh_colors=[tresh_v_r;tresh_v_g;tresh_v_b;tresh_v_cb;tresh_v_cr;];

%     figure;
% 	subplot(231);bar(hist_x_r,hist_norm_r);subplot(232);bar(hist_x_g,hist_norm_g);subplot(233);bar(hist_x_b,hist_norm_b);
%     subplot(234);bar(hist_x_cb,hist_norm_cb);subplot(235);bar(hist_x_cr,hist_norm_cr);subplot(236);
%     figure;
%     imshow(obr_rgb);
%     figure;
%     imshow(obr_seg);
    
    save('Kalibrace_barev','tresh_colors')
else %data pochazi z IR kamer
    obr_IR_h=rgb2gray(obr_l).*obr_seg;
    tresh_prob_IR=0.01;
    
    xbin=linspace(0,1,256);
    [hist_num_IR,hist_x_IR]=hist(obr_IR_h,xbin);
    hist_sum_IR=sum(hist_num_IR,2);hist_sum_IR(1)=0;
    hist_norm_IR=hist_sum_IR./sum(hist_sum_IR);
    
    tresh_index_IR=find(hist_norm_IR>tresh_prob_IR);
    tresh_v_IR=[hist_x_IR(tresh_index_IR(1)),hist_x_IR(tresh_index_IR(end))];
    
    save('Kalibrace_IR','tresh_v_IR')
%     figure;
% 	bar(hist_x_IR,hist_norm_IR);
end
end

