function [image_db, mean_dice, mean_jaccard, mean_time] = evaluate_results(image_db, folder_label)
% funkce vyhodnoti vysledky jednotlivych metod, coz zahrnuje vypocet indexu
% Dice a Jaccard. Funkce rovnez ulozi vysledky jako segmentovane masky a
% XLSX soubor obsahujici vysledne vypoctene hodnoty

result_folder = 'results';
result_subfolder = fullfile(result_folder, folder_label);

mkdir(result_folder);
mkdir(result_subfolder);

for k = 1:length(image_db)
    
    result = logical(image_db(k).segmented);
    ground_truth = logical(image_db(k).mask);
    
    image_db(k).dice = dice(result, ground_truth);
    image_db(k).jaccard = jaccard(result, ground_truth);
    
    imwrite(image_db(k).segmented, fullfile(result_subfolder, [image_db(k).name, '.png']));
end

mean_dice = mean([image_db.dice]);
mean_jaccard = mean([image_db.jaccard]);
mean_time = mean([image_db.time]);

field_names_to_remove = fieldnames(image_db);
field_idx = 1;
while field_idx <= length(field_names_to_remove)
    if any(strcmp(field_names_to_remove{field_idx}, {'dice', 'jaccard', 'time', 'pix_size', 'name', 'disc', 'modality', 'series', 'num'}))
        field_names_to_remove(field_idx) = [];
    else
        field_idx = field_idx + 1;
    end
end
output_table = image_db;
output_table = rmfield(output_table, field_names_to_remove);

output_table = struct2table(output_table);
writetable(output_table, fullfile(result_subfolder, 'stats.xlsx'));

end

