%% Skript pro extrahování příznaků z retinálních cév za účelem jejich klasifikace - S MASKOU KOLEM OD
% Načtené snímky a k nim příslušná data jsou zpracovávany do takové podoby,
% aby bylo možno extrahovat příznaky cév. Stěžejním výstupem skriptu jsou
% proměnné: priznak_mat_skeleton, label_mat_skeleton, priznak_mat_vess a
% label_mat_skeleton. Tyto proměnné jsou vstupy do SVM klasifikátoru.

% Tento skript slouží pro klasifikaci cév pouze okolo optického disku, je
% definována oblast zájmu klasifikace!

clc; clear;

%% Načtení cest k datům
% snímky sítnice
img_location = 'C:\Users\jana-\Documents\FEKT\DP\data\RITE\train\images';    % cesta ke snímkům
addpath(img_location);
img_data = imageDatastore(img_location);
pocet = length(img_data.Files);                                              % počet dat

% segmentace cév
seg_location = 'C:\Users\jana-\Documents\FEKT\DP\data\RITE\train\vessels';   % cesta k maskám segmentovaných cév
addpath(seg_location);
seg_data = imageDatastore(seg_location);

% labely cév
class_location = 'C:\Users\jana-\Documents\FEKT\DP\data\RITE\train\classes'; % cesta k labelům cév označneých experty
addpath(class_location);
class_data = imageDatastore(class_location);

% maska okolo OD
mask_location = 'C:\Users\jana-\Documents\FEKT\DP\data\RITE\train\masks';    % cesta k ROI maskám
addpath(mask_location);
mask_data = fileDatastore(mask_location, 'ReadFcn', @load, 'FileExtensions', '.mat');

%% For cycklus pro procházení jednotlivých snímků a jim příslušných dat (segmentace, labely, masky)
priznak_mat_skeleton = double([]);
priznak_mat_vess = double([]);
label_mat_skeleton = double([]);
label_mat_vess = double([]);

for i = 1:pocet
    % načtení snímků a masek 
    img_RGB = im2double(imread(strcat('rite', num2str(i), '_train_img.tif')));
    segmentace = im2double(imread(strcat('rite', num2str(i), '_train_seg.png')));
    klas = imread(strcat('rite', num2str(i), '_train_class.png'));
    load(strcat('rite', num2str(i), '_train_mask.mat'));

    %% RGB, HSV prostor
    [redChannel_nativ, greenChannel_nativ, blueChannel_nativ] = imsplit(img_RGB);
    img_HSV = rgb2hsv(img_RGB);
    [hueChannel_nativ, saturationChannel_nativ, valueChannel_nativ] = imsplit(img_HSV);
 
    % figure
    % subplot 232; imshow(img_RGB, []); title('RGB prostor');
    % subplot 234; imshow(redChannel_nativ, []); title('Červený kanál');
    % subplot 235; imshow(greenChannel_nativ, []); title('Zelený kanál');
    % subplot 236; imshow(blueChannel_nativ, []); title('Modrý kanál');

    % figure
    % subplot 232; imshow(img_HSV, []); title('HSV prostor');
    % subplot 234; imshow(hueChannel_nativ, []); title('Odstín');
    % subplot 235; imshow(saturationChannel_nativ, []); title('Saturace - sytost');
    % subplot 236; imshow(valueChannel_nativ, []); title('Hodnota jasu');

    % adaptivní ekvalizace histogramu
    redChannel = adapthisteq(redChannel_nativ);
    greenChannel = adapthisteq(greenChannel_nativ);
    blueChannel = adapthisteq(blueChannel_nativ);
    hueChannel = adapthisteq(hueChannel_nativ);
    saturationChannel = adapthisteq(saturationChannel_nativ);
    valueChannel = adapthisteq(valueChannel_nativ);

    % imshowpair(greenChannel_nativ, greenChannel, 'montage');

    %% Vytvoření skeletonu
    BW = imbinarize(segmentace); % binarizace segmentovaného obrazu
    skeleton = bwskel(BW);       % vytvoření skeletonu cévního stromu
    branch = bwmorph(skeleton, 'branchpoints');
    skeleton(branch) = 0;        % odstranění bifurkací a bodů křížení

    %% Násobení obrazu a masek
    ROI_cevy = immultiply(segmentace, maska_rozdil);  % násobení segmentační masky a ROI okolo OD
    ROI_skeleton = immultiply(ROI_cevy, skeleton);    % násobení ROI a skeletonu

    % vynásobení RGB obrazu a segmentační masky
    img_nasobeny_R_vess = immultiply(redChannel, ROI_cevy);
    img_nasobeny_G_vess = immultiply(greenChannel, ROI_cevy);
    img_nasobeny_B_vess = immultiply(blueChannel, ROI_cevy);

    % vynásobení RGB obrazu a skeletonu
    img_nasobeny_R_skeleton = immultiply(redChannel, ROI_skeleton);
    img_nasobeny_G_skeleton = immultiply(greenChannel, ROI_skeleton);
    img_nasobeny_B_skeleton = immultiply(blueChannel, ROI_skeleton);

    % vynásobení HSV obrazu a segmentační masky
    img_nasobeny_H_vess = immultiply(hueChannel, ROI_cevy);
    img_nasobeny_S_vess = immultiply(saturationChannel, ROI_cevy);
    img_nasobeny_V_vess = immultiply(valueChannel, ROI_cevy);

    % vynásobení HSV obrazu a skeletonu
    img_nasobeny_H_skeleton = immultiply(hueChannel, ROI_skeleton);
    img_nasobeny_S_skeleton = immultiply(saturationChannel, ROI_skeleton);
    img_nasobeny_V_skeleton = immultiply(valueChannel, ROI_skeleton);

    %% Vytvoření distanční matice
    dist_matrix = bwdist(~BW);                      % zisk distanční matice z binárního obrazu
    window = ones(5, 5) / 25;
    dist_matrix_av = imfilter(dist_matrix, window); % průměrování distanční matice

    % anebo, výsledek je stejný : 
    % window_x = 5;
    % kernel = ones(window_x) / window_x ^ 2;
    % dist_matrix_x = conv2(single(dist_matrix), kernel, 'same');

    % https://www.sciencedirect.com/science/article/pii/S0169260717312415 popis distanční matice

    matrix = zeros(size(img_RGB, 1), size(img_RGB, 2)); % vytvoření matice nul o velikosti stejné, jako je vstupní obraz

    for m = 1:size(skeleton, 1)
        for n = 1:size(skeleton, 2)
            if skeleton(m, n) == 1
                matrix(m, n) = dist_matrix_av(m, n);
            end
        end
    end

    dist_matrix_skeleton = immultiply(double(dist_matrix_av), ROI_skeleton);
    norm_dist_matrix_skeleton = normalizace_priznaku(dist_matrix_skeleton, ROI_skeleton);

    dist_matrix_vess = immultiply(double(dist_matrix_av), ROI_cevy);
    norm_dist_matrix_vess = normalizace_priznaku(dist_matrix_vess, ROI_cevy);

    % figure
    % subplot 141; imshow(segmentace, []); title('Segmentovaný cévní strom');
    % subplot 142; imshow(skeleton, []); title('Skeleton cévního stromu');
    % subplot 143; imshow(dist_matrix, []); title('Distanční matice');
    % subplot 144; imshow(matrix, []); title('Skeleton + distance');

    %% Normalizace intenzity centrálních pixelů
    norm_priznak_R_skeleton = normalizace_priznaku(img_nasobeny_R_skeleton, ROI_skeleton);
    norm_priznak_G_skeleton = normalizace_priznaku(img_nasobeny_G_skeleton, ROI_skeleton);
    norm_priznak_B_skeleton = normalizace_priznaku(img_nasobeny_B_skeleton, ROI_skeleton);

    norm_priznak_H_skeleton = normalizace_priznaku(img_nasobeny_H_skeleton, ROI_skeleton);
    norm_priznak_S_skeleton = normalizace_priznaku(img_nasobeny_S_skeleton, ROI_skeleton);
    norm_priznak_V_skeleton = normalizace_priznaku(img_nasobeny_V_skeleton, ROI_skeleton);

    %% Procházení okolí centrálního pixelu a výpočet příznaků z definovaného okna, příznaky následně normalizovány
    % R kanál z RGB
    pom_matrix_R_mean = zeros(size(img_RGB, 1), size(img_RGB, 2));
    pom_matrix_R_std = zeros(size(img_RGB, 1), size(img_RGB, 2));
    pom_matrix_R_ent = zeros(size(img_RGB, 1), size(img_RGB, 2));

    for m = 1:size(ROI_skeleton, 1)
        for n = 1:size(ROI_skeleton, 2)
            if ROI_skeleton(m, n) == 1
                center = img_nasobeny_R_vess(m, n);
                nahoru = img_nasobeny_R_vess(m - 1, n); 
                dolu = img_nasobeny_R_vess(m + 1, n); 
                vlevo = img_nasobeny_R_vess(m, n - 1);
                vpravo = img_nasobeny_R_vess(m, n + 1);
                s1 = img_nasobeny_R_vess(m - 1, n - 1);
                s2 = img_nasobeny_R_vess(m + 1, n - 1);
                s3 = img_nasobeny_R_vess(m - 1, n + 1);
                s4 = img_nasobeny_R_vess(m + 1, n + 1);
                pom = [s1, nahoru, s3; vlevo, center, vpravo; s2, dolu, s4];
                pom_matrix_R_mean(m, n) = mean2(pom);
                pom_matrix_R_std(m, n) = std2(pom);
                pom_matrix_R_ent(m, n) = entropy(pom);
            end
        end
    end

    norm_priznak_R_mean_skeleton = normalizace_priznaku(pom_matrix_R_mean, ROI_skeleton);
    norm_priznak_R_std_skeleton = normalizace_priznaku(pom_matrix_R_std, ROI_skeleton);
    norm_priznak_R_ent_skeleton = normalizace_priznaku(pom_matrix_R_ent, ROI_skeleton);

    % G kanál z RGB
    pom_matrix_G_mean = zeros(size(img_RGB, 1), size(img_RGB, 2));
    pom_matrix_G_std = zeros(size(img_RGB, 1), size(img_RGB, 2));
    pom_matrix_G_ent = zeros(size(img_RGB, 1), size(img_RGB, 2));

    for m = 1:size(ROI_skeleton, 1)
        for n = 1:size(ROI_skeleton, 2)
            if ROI_skeleton(m, n) == 1
                center = img_nasobeny_G_vess(m, n);
                nahoru = img_nasobeny_G_vess(m - 1, n); 
                dolu = img_nasobeny_G_vess(m + 1, n); 
                vlevo = img_nasobeny_G_vess(m, n - 1);
                vpravo = img_nasobeny_G_vess(m, n + 1);
                s1 = img_nasobeny_G_vess(m - 1, n - 1);
                s2 = img_nasobeny_G_vess(m + 1, n - 1);
                s3 = img_nasobeny_G_vess(m - 1, n + 1);
                s4 = img_nasobeny_G_vess(m + 1, n + 1);
                pom = [s1, nahoru, s3; vlevo, center, vpravo; s2, dolu, s4];
                pom_matrix_G_mean(m, n) = mean2(pom);
                pom_matrix_G_std(m, n) = std2(pom);
                pom_matrix_G_ent(m, n) = entropy(pom);
            end
        end
    end

    norm_priznak_G_mean_skeleton = normalizace_priznaku(pom_matrix_G_mean, ROI_skeleton);
    norm_priznak_G_std_skeleton = normalizace_priznaku(pom_matrix_G_std, ROI_skeleton);
    norm_priznak_G_ent_skeleton = normalizace_priznaku(pom_matrix_G_ent, ROI_skeleton);

    % B kanál z RGB
    pom_matrix_B_mean = zeros(size(img_RGB, 1), size(img_RGB, 2));
    pom_matrix_B_std = zeros(size(img_RGB, 1), size(img_RGB, 2));
    pom_matrix_B_ent = zeros(size(img_RGB, 1), size(img_RGB, 2));

    for m = 1:size(ROI_skeleton, 1)
        for n = 1:size(ROI_skeleton, 2)
            if ROI_skeleton(m, n) == 1
                center = img_nasobeny_B_vess(m, n);
                nahoru = img_nasobeny_B_vess(m - 1, n); 
                dolu = img_nasobeny_B_vess(m + 1, n); 
                vlevo = img_nasobeny_B_vess(m, n - 1);
                vpravo = img_nasobeny_B_vess(m, n + 1);
                s1 = img_nasobeny_B_vess(m - 1, n - 1);
                s2 = img_nasobeny_B_vess(m + 1, n - 1);
                s3 = img_nasobeny_B_vess(m - 1, n + 1);
                s4 = img_nasobeny_B_vess(m + 1, n + 1);
                pom = [s1, nahoru, s3; vlevo, center, vpravo; s2, dolu, s4];
                pom_matrix_B_mean(m, n) = mean2(pom);
                pom_matrix_B_std(m, n) = std2(pom);
                pom_matrix_B_ent(m, n) = entropy(pom);
            end
        end
    end

    norm_priznak_B_mean_skeleton = normalizace_priznaku(pom_matrix_B_mean, ROI_skeleton);
    norm_priznak_B_std_skeleton = normalizace_priznaku(pom_matrix_B_std, ROI_skeleton);
    norm_priznak_B_ent_skeleton = normalizace_priznaku(pom_matrix_B_ent, ROI_skeleton);

    % H kanál z HSV
    pom_matrix_H_mean = zeros(size(img_RGB, 1), size(img_RGB, 2));
    pom_matrix_H_std = zeros(size(img_RGB, 1), size(img_RGB, 2));
    pom_matrix_H_ent = zeros(size(img_RGB, 1), size(img_RGB, 2));

    for m = 1:size(ROI_skeleton, 1)
        for n = 1:size(ROI_skeleton, 2)
            if ROI_skeleton(m, n) == 1
                center = img_nasobeny_H_vess(m, n);
                nahoru = img_nasobeny_H_vess(m - 1, n); 
                dolu = img_nasobeny_H_vess(m + 1, n); 
                vlevo = img_nasobeny_H_vess(m, n - 1);
                vpravo = img_nasobeny_H_vess(m, n + 1);
                s1 = img_nasobeny_H_vess(m - 1, n - 1);
                s2 = img_nasobeny_H_vess(m + 1, n - 1);
                s3 = img_nasobeny_H_vess(m - 1, n + 1);
                s4 = img_nasobeny_H_vess(m + 1, n + 1);
                pom = [s1, nahoru, s3; vlevo, center, vpravo; s2, dolu, s4];
                pom_matrix_H_mean(m, n) = mean2(pom);
                pom_matrix_H_std(m, n) = std2(pom);
                pom_matrix_H_ent(m, n) = entropy(pom);
            end
        end
    end

    norm_priznak_H_mean_skeleton = normalizace_priznaku(pom_matrix_H_mean, ROI_skeleton);
    norm_priznak_H_std_skeleton = normalizace_priznaku(pom_matrix_H_std, ROI_skeleton);
    norm_priznak_H_ent_skeleton = normalizace_priznaku(pom_matrix_H_ent, ROI_skeleton);

    % S kanál z HSV
    pom_matrix_S_mean = zeros(size(img_RGB, 1), size(img_RGB, 2));
    pom_matrix_S_std = zeros(size(img_RGB, 1), size(img_RGB, 2));
    pom_matrix_S_ent = zeros(size(img_RGB, 1), size(img_RGB, 2));

    for m = 1:size(ROI_skeleton, 1)
        for n = 1:size(ROI_skeleton, 2)
            if ROI_skeleton(m, n) == 1
                center = img_nasobeny_S_vess(m, n);
                nahoru = img_nasobeny_S_vess(m - 1, n); 
                dolu = img_nasobeny_S_vess(m + 1, n); 
                vlevo = img_nasobeny_S_vess(m, n - 1);
                vpravo = img_nasobeny_S_vess(m, n + 1);
                s1 = img_nasobeny_S_vess(m - 1, n - 1);
                s2 = img_nasobeny_S_vess(m + 1, n - 1);
                s3 = img_nasobeny_S_vess(m - 1, n + 1);
                s4 = img_nasobeny_S_vess(m + 1, n + 1);
                pom = [s1, nahoru, s3; vlevo, center, vpravo; s2, dolu, s4];
                pom_matrix_S_mean(m, n) = mean2(pom);
                pom_matrix_S_std(m, n) = std2(pom);
                pom_matrix_S_ent(m, n) = entropy(pom);
            end
        end
    end

    norm_priznak_S_mean_skeleton = normalizace_priznaku(pom_matrix_S_mean, ROI_skeleton);
    norm_priznak_S_std_skeleton = normalizace_priznaku(pom_matrix_S_std, ROI_skeleton);
    norm_priznak_S_ent_skeleton = normalizace_priznaku(pom_matrix_S_ent, ROI_skeleton);

    % V kanál z HSV
    pom_matrix_V_mean = zeros(size(img_RGB, 1), size(img_RGB, 2));
    pom_matrix_V_std = zeros(size(img_RGB, 1), size(img_RGB, 2));
    pom_matrix_V_ent = zeros(size(img_RGB, 1), size(img_RGB, 2));

    for m = 1:size(ROI_skeleton, 1)
        for n = 1:size(ROI_skeleton, 2)
            if ROI_skeleton(m, n) == 1
                center = img_nasobeny_V_vess(m, n);
                nahoru = img_nasobeny_V_vess(m - 1, n); 
                dolu = img_nasobeny_V_vess(m + 1, n); 
                vlevo = img_nasobeny_V_vess(m, n - 1);
                vpravo = img_nasobeny_V_vess(m, n + 1);
                s1 = img_nasobeny_V_vess(m - 1, n - 1);
                s2 = img_nasobeny_V_vess(m + 1, n - 1);
                s3 = img_nasobeny_V_vess(m - 1, n + 1);
                s4 = img_nasobeny_V_vess(m + 1, n + 1);
                pom = [s1, nahoru, s3; vlevo, center, vpravo; s2, dolu, s4];
                pom_matrix_V_mean(m, n) = mean2(pom);
                pom_matrix_V_std(m, n) = std2(pom);
                pom_matrix_V_ent(m, n) = entropy(pom);
            end
        end
    end

    norm_priznak_V_mean_skeleton = normalizace_priznaku(pom_matrix_V_mean, ROI_skeleton);
    norm_priznak_V_std_skeleton = normalizace_priznaku(pom_matrix_V_std, ROI_skeleton);
    norm_priznak_V_ent_skeleton = normalizace_priznaku(pom_matrix_V_ent, ROI_skeleton);

    %% Intenzita centrálních pixelů v gaussian blurred RGB
    % sigma = 1
    gauss1_R_skeleton = imgaussfilt(img_nasobeny_R_skeleton, 1);
    gauss1_G_skeleton = imgaussfilt(img_nasobeny_G_skeleton, 1);
    gauss1_B_skeleton = imgaussfilt(img_nasobeny_B_skeleton, 1);
    gauss1_R_skeleton = immultiply(gauss1_R_skeleton, segmentace);
    gauss1_G_skeleton = immultiply(gauss1_G_skeleton, segmentace);
    gauss1_B_skeleton = immultiply(gauss1_B_skeleton, segmentace);
    norm_priznak_gauss1_R_skeleton = normalizace_priznaku(gauss1_R_skeleton, ROI_cevy);
    norm_priznak_gauss1_G_skeleton = normalizace_priznaku(gauss1_G_skeleton, ROI_cevy);
    norm_priznak_gauss1_B_skeleton = normalizace_priznaku(gauss1_B_skeleton, ROI_cevy);

    % sigma = 2
    gauss2_R_skeleton = imgaussfilt(img_nasobeny_R_skeleton, 2);
    gauss2_G_skeleton = imgaussfilt(img_nasobeny_G_skeleton, 2);
    gauss2_B_skeleton = imgaussfilt(img_nasobeny_B_skeleton, 2);
    gauss2_R_skeleton = immultiply(gauss2_R_skeleton, segmentace);
    gauss2_G_skeleton = immultiply(gauss2_G_skeleton, segmentace);
    gauss2_B_skeleton = immultiply(gauss2_B_skeleton, segmentace);
    norm_priznak_gauss2_R_skeleton = normalizace_priznaku(gauss2_R_skeleton, ROI_cevy);
    norm_priznak_gauss2_G_skeleton = normalizace_priznaku(gauss2_G_skeleton, ROI_cevy);
    norm_priznak_gauss2_B_skeleton = normalizace_priznaku(gauss2_B_skeleton, ROI_cevy);

    % sigma = 4
    gauss4_R_skeleton = imgaussfilt(img_nasobeny_R_skeleton, 4);
    gauss4_G_skeleton = imgaussfilt(img_nasobeny_G_skeleton, 4);
    gauss4_B_skeleton = imgaussfilt(img_nasobeny_B_skeleton, 4);
    gauss4_R_skeleton = immultiply(gauss4_R_skeleton, segmentace);
    gauss4_G_skeleton = immultiply(gauss4_G_skeleton, segmentace);
    gauss4_B_skeleton = immultiply(gauss4_B_skeleton, segmentace);
    norm_priznak_gauss4_R_skeleton = normalizace_priznaku(gauss4_R_skeleton, ROI_cevy);
    norm_priznak_gauss4_G_skeleton = normalizace_priznaku(gauss4_G_skeleton, ROI_cevy);
    norm_priznak_gauss4_B_skeleton = normalizace_priznaku(gauss4_B_skeleton, ROI_cevy);

    % sigma = 8
    gauss8_R_skeleton = imgaussfilt(img_nasobeny_R_skeleton, 8);
    gauss8_G_skeleton = imgaussfilt(img_nasobeny_G_skeleton, 8);
    gauss8_B_skeleton = imgaussfilt(img_nasobeny_B_skeleton, 8);
    gauss8_R_skeleton = immultiply(gauss8_R_skeleton, segmentace);
    gauss8_G_skeleton = immultiply(gauss8_G_skeleton, segmentace);
    gauss8_B_skeleton = immultiply(gauss8_B_skeleton, segmentace);
    norm_priznak_gauss8_R_skeleton = normalizace_priznaku(gauss8_R_skeleton, ROI_cevy);
    norm_priznak_gauss8_G_skeleton = normalizace_priznaku(gauss8_G_skeleton, ROI_cevy);
    norm_priznak_gauss8_B_skeleton = normalizace_priznaku(gauss8_B_skeleton, ROI_cevy);

    %% Průměr + směrodatná odchylka + rozptyl intenzity v červeném, zeleném a modrém kanálu cév
    okno = 3;

    mean_R_vess = conv2(img_nasobeny_R_vess, ones(okno)/okno^2, 'same');
    mean_G_vess = conv2(img_nasobeny_G_vess, ones(okno)/okno^2, 'same');
    mean_B_vess = conv2(img_nasobeny_B_vess, ones(okno)/okno^2, 'same');
    mean_R_vess = immultiply(mean_R_vess, segmentace);
    mean_G_vess = immultiply(mean_G_vess, segmentace);
    mean_B_vess = immultiply(mean_B_vess, segmentace);
    norm_priznak_mean_R_vess = normalizace_priznaku(mean_R_vess, ROI_cevy);
    norm_priznak_mean_G_vess = normalizace_priznaku(mean_G_vess, ROI_cevy);
    norm_priznak_mean_B_vess = normalizace_priznaku(mean_B_vess, ROI_cevy);

    std_R_vess = stdfilt(img_nasobeny_R_vess, ones(okno));
    std_G_vess = stdfilt(img_nasobeny_G_vess, ones(okno));
    std_B_vess = stdfilt(img_nasobeny_B_vess, ones(okno));
    std_R_vess = immultiply(std_R_vess, segmentace);
    std_G_vess = immultiply(std_G_vess, segmentace);
    std_B_vess = immultiply(std_B_vess, segmentace);
    norm_priznak_std_R_vess = normalizace_priznaku(std_R_vess, ROI_cevy);
    norm_priznak_std_G_vess = normalizace_priznaku(std_G_vess, ROI_cevy);
    norm_priznak_std_B_vess = normalizace_priznaku(std_B_vess, ROI_cevy);

    var_R_vess = std_R_vess.^2;
    var_G_vess = std_G_vess.^2;
    var_B_vess = std_B_vess.^2;
    var_R_vess = immultiply(var_R_vess, segmentace);
    var_G_vess = immultiply(var_G_vess, segmentace);
    var_B_vess = immultiply(var_B_vess, segmentace);
    norm_priznak_var_R_vess = normalizace_priznaku(var_R_vess, ROI_cevy);
    norm_priznak_var_G_vess = normalizace_priznaku(var_G_vess, ROI_cevy);
    norm_priznak_var_B_vess = normalizace_priznaku(var_B_vess, ROI_cevy);

    %% Průměr + směrodatná odchylka + rozptyl odstínu, saturace a intenzity cév
    mean_H_vess = conv2(img_nasobeny_H_vess, ones(okno)/okno^2, 'same');
    mean_S_vess = conv2(img_nasobeny_S_vess, ones(okno)/okno^2, 'same');
    mean_V_vess = conv2(img_nasobeny_V_vess, ones(okno)/okno^2, 'same');
    mean_H_vess = immultiply(mean_H_vess, segmentace);
    mean_S_vess = immultiply(mean_S_vess, segmentace);
    mean_V_vess = immultiply(mean_V_vess, segmentace);
    norm_priznak_mean_H_vess = normalizace_priznaku(mean_H_vess, ROI_cevy);
    norm_priznak_mean_S_vess = normalizace_priznaku(mean_S_vess, ROI_cevy);
    norm_priznak_mean_V_vess = normalizace_priznaku(mean_V_vess, ROI_cevy);

    std_H_vess = stdfilt(img_nasobeny_H_vess, ones(okno));
    std_S_vess = stdfilt(img_nasobeny_S_vess, ones(okno));
    std_V_vess = stdfilt(img_nasobeny_V_vess, ones(okno));
    std_H_vess = immultiply(std_H_vess, segmentace);
    std_S_vess = immultiply(std_S_vess, segmentace);
    std_V_vess = immultiply(std_V_vess, segmentace);
    norm_priznak_std_H_vess = normalizace_priznaku(std_H_vess, ROI_cevy);
    norm_priznak_std_S_vess = normalizace_priznaku(std_S_vess, ROI_cevy);
    norm_priznak_std_V_vess = normalizace_priznaku(std_V_vess, ROI_cevy);

    var_H_vess = std_H_vess.^2;
    var_S_vess = std_S_vess.^2;
    var_V_vess = std_V_vess.^2;
    var_H_vess = immultiply(var_H_vess, segmentace);
    var_S_vess = immultiply(var_S_vess, segmentace);
    var_V_vess = immultiply(var_V_vess, segmentace);
    norm_priznak_var_H_vess = normalizace_priznaku(var_H_vess, ROI_cevy);
    norm_priznak_var_S_vess = normalizace_priznaku(var_S_vess, ROI_cevy);
    norm_priznak_var_V_vess = normalizace_priznaku(var_V_vess, ROI_cevy);

    %% Vytvoření boxplotu pro posouzení jednotlivého příznaku
    lab_mat = labels_rite(klas);
    
    % násobení segmentační masky a ROI okolo OD + ROI a skeletonu
    ROI_klas = immultiply(lab_mat, maska_rozdil);
    ROI_klas = immultiply(ROI_klas, skeleton);
    
    klas_pom = reshape(ROI_klas, [], 1);
    klas_pom(klas_pom == 0) = [];            % odstranění pozadí
    
    vektor_zkusebni_tepny = zeros(length(klas_pom), 1);
    vektor_zkusebni_zily = zeros(length(klas_pom), 1);
          
    for i = 1:length(klas_pom)
        if klas_pom(i) == 2
            vektor_zkusebni_tepny(i) = norm_dist_matrix_skeleton(i);
        end
    end
    
    for i = 1:length(klas_pom)
        if klas_pom(i) == 3
            vektor_zkusebni_zily(i) = norm_dist_matrix_skeleton(i);
        end
    end
    
    vektor_zkusebni_tepny = nonzeros(vektor_zkusebni_tepny');
    vektor_zkusebni_zily = nonzeros(vektor_zkusebni_zily');
    
    group = [2 * ones(size(vektor_zkusebni_tepny)); 3 * ones(size(vektor_zkusebni_zily))];
    boxplot([vektor_zkusebni_tepny; vektor_zkusebni_zily], group);

    %% Vytvoření matice extrahovaných příznaků pro vstup do SVM
    priznak_pom_skeleton = double([norm_dist_matrix_skeleton, norm_priznak_R_skeleton, norm_priznak_G_skeleton, norm_priznak_B_skeleton, ...
        norm_priznak_H_skeleton, norm_priznak_V_skeleton, ...
        norm_priznak_R_mean_skeleton, norm_priznak_G_mean_skeleton, norm_priznak_B_mean_skeleton, ...
        norm_priznak_H_mean_skeleton, norm_priznak_S_mean_skeleton, norm_priznak_V_mean_skeleton, ...
        norm_priznak_H_std_skeleton, norm_priznak_G_ent_skeleton, norm_priznak_H_ent_skeleton, norm_priznak_S_ent_skeleton]);
    priznak_mat_skeleton = [priznak_mat_skeleton; priznak_pom_skeleton];
    
    priznak_pom_vess = double([norm_priznak_gauss1_R_skeleton, norm_priznak_gauss1_G_skeleton, norm_priznak_gauss1_B_skeleton, ...
        norm_priznak_gauss2_R_skeleton, norm_priznak_gauss2_G_skeleton, norm_priznak_gauss2_B_skeleton, ...
        norm_priznak_gauss4_R_skeleton, norm_priznak_gauss4_G_skeleton, norm_priznak_gauss4_B_skeleton, ...
        norm_priznak_mean_R_vess, norm_priznak_mean_G_vess, norm_priznak_mean_B_vess, ...
        norm_priznak_std_R_vess, norm_priznak_std_G_vess, norm_priznak_std_B_vess, ...
        norm_priznak_mean_H_vess, norm_priznak_mean_S_vess, norm_priznak_mean_V_vess, ...
        norm_priznak_std_H_vess, norm_priznak_std_V_vess]);
    priznak_mat_vess = [priznak_mat_vess; priznak_pom_vess];
    
    % pro testovací snímky
    % priznak_mat{i} = priznak_pom;
    
    %% Vytvoření matice labelů pro vstup do SVM
    pom = labels_rite(klas);
    label_pom = immultiply(pom, maska_rozdil);
    
    label_pom_skeleton = immultiply(label_pom, skeleton);
    label_pom_skeleton = reshape(label_pom_skeleton, [], 1);
    label_pom_skeleton(label_pom_skeleton == 0) = [];
    label_mat_skeleton = [label_mat_skeleton; label_pom_skeleton];
    
    label_pom_vess = immultiply(label_pom, segmentace);
    label_pom_vess = reshape(label_pom_vess, [], 1);
    label_pom_vess(label_pom_vess == 0) = [];
    label_mat_vess = [label_mat_vess; label_pom_vess];
    
    % pro testovací snímky
    % label_mat{i} = label_pom;
    
end

