clear all; close all; %clc

% načítanie databáz - triedy, testované obr.

load('databaza_tried_co_oc_br.mat')
load('databaza_testov_co_oc_br.mat')

cn = size(datab_triedy); % počet tried
[s1,s2] = size(datab_triedy{1,1});

rot_i = 0;% priemerovanie parametrov 1-20 v rôznych smeroch - rotačná invariantnosť
% rot_i = 1; % priemerovanie parametrov 1-20 v rôznych smeroch - rotačná invariantnosť

% pozicie = 1:9;
% výber parametrov pre klasifikáciu
% pozicie = [3, 4, 5, 6, 7, 8, 9]; 

pozicie = 1:24;

% pozicie = [3 4 5 8 9 10 13 14 15 18 19 20 21 22 23 24]; % vyber konkretnych parametrov
% rôzne smery
% pozicie = [3 4 5 20 21 22 23 24]; %
% pozicie = [8 9 10 20 21 22 23 24]; % 
% pozicie = [13 14 15 20 21 22 23 24]; % 
% pozicie = [18 19 20 20 21 22 23 24]; % 
% pozicie = [3, 9, 15, 21, 22, 23, 24];

p = 0; % počítadlo - posun o 20 stĺpcov v matici výsledkov
m_dist = zeros(cn(1)*s1, cn(1));
vysledky = zeros(cn(1)*s1, cn(1));
for j = 1:cn(1) % výber triedy
    for m = 1:s1 % výber testovaných obrazov v triede
        vzorka = datab_testy{j,1};
        v = vzorka(m,:); % v - príslušný vektor parametrov
        if rot_i == 1
            vz_1 = v(1, [1,5,9,13,17]);
            vz_2 = v(1, [2,6,10,14,18]);
            vz_3 = v(1, [3,7,11,15,19]);
            vz_4 = v(1, [4,8,12,16,20]);

            v_p = zeros(1, 9); 
            v_p(1,1:5) = (vz_1+vz_2+vz_3+vz_4)/4;
            v_p(:,6:9) = v(:,21:24);
            v = v_p;
        end
        vs = (v(pozicie));% vs - príslušný skrátený vektor parametrov

        % výpočet postupne vzdialenosti test. vzorky od tried
        for n = 1:cn(1)
            trieda = datab_triedy{n,1};
            if rot_i == 1
                tr_s1 = trieda(:, [1,5,9,13,17]);
                tr_s2 = trieda(:, [2,6,10,14,18]);
                tr_s3 = trieda(:, [3,7,11,15,19]);
                tr_s4 = trieda(:, [4,8,12,16,20]);

                trieda_p = zeros(s1, 9); 
                trieda_p(:,1:5) = (tr_s1+tr_s2+tr_s3+tr_s4)/4;
                trieda_p(:,6:9) = trieda(:,21:24);
                trieda = trieda_p;
            end
            trieda_s = trieda(:,pozicie);
            m_dist(m+p*s1, n) = mahal(vs,trieda_s);
        end
    end
    p = p + 1;
end

% zápis 1 na pozícii kde je najmenšia mahal. vzdialenosť
for o = 1:cn(1)*s1
%         dist = m_dist(o,:);
%         [M,I] = min(dist(:));

%         dist = m_dist(o,:);
    [M,I] = min(m_dist(o,:));
    vysledky(o,I) = 1;
end
    
% časti matice výsledkov kde má byť podľa správnosti súčet rovný
% testovanému počtu obrázkov danej triedy
v1 = vysledky(1:s1,1);
v2 = vysledky(s1+1:2*s1,2);
v3 = vysledky(2*s1+1:3*s1,3);
v4 = vysledky(3*s1+1:4*s1,4);
v5 = vysledky(4*s1+1:5*s1,5);
v6 = vysledky(5*s1+1:6*s1,6);
v7 = vysledky(6*s1+1:7*s1,7);
v8 = vysledky(7*s1+1:8*s1,8);
v9 = vysledky(8*s1+1:9*s1,9);

% a: 001:220      c: 441:660
% b: 221:440      d: 661:880

ch_a2 = vysledky(1:s1,2);            pch_a2 = sum(ch_a2); % súčet chybných klasifikácii
ch_a3 = vysledky(1:s1,3);            pch_a3 = sum(ch_a3);
ch_a4 = vysledky(1:s1,4);            pch_a4 = sum(ch_a4);
ch_a5 = vysledky(1:s1,5);            pch_a5 = sum(ch_a5);
ch_a6 = vysledky(1:s1,6);            pch_a6 = sum(ch_a6);
ch_a7 = vysledky(1:s1,7);            pch_a7 = sum(ch_a7);
ch_a8 = vysledky(1:s1,8);            pch_a8 = sum(ch_a8);
ch_a9 = vysledky(1:s1,9);            pch_a9 = sum(ch_a9);

ch_b1 = vysledky(s1+1:2*s1,1);        pch_b1 = sum(ch_b1);
ch_b3 = vysledky(s1+1:2*s1,3);        pch_b3 = sum(ch_b3);
ch_b4 = vysledky(s1+1:2*s1,4);        pch_b4 = sum(ch_b4);
ch_b5 = vysledky(s1+1:2*s1,5);        pch_b5 = sum(ch_b5);
ch_b6 = vysledky(s1+1:2*s1,6);        pch_b6 = sum(ch_b6);
ch_b7 = vysledky(s1+1:2*s1,7);        pch_b7 = sum(ch_b7);
ch_b8 = vysledky(s1+1:2*s1,8);        pch_b8 = sum(ch_b8);
ch_b9 = vysledky(s1+1:2*s1,9);        pch_b9 = sum(ch_b9);

ch_c1 = vysledky(2*s1+1:3*s1,1);      pch_c1 = sum(ch_c1);
ch_c2 = vysledky(2*s1+1:3*s1,2);      pch_c2 = sum(ch_c2);
ch_c4 = vysledky(2*s1+1:3*s1,4);      pch_c4 = sum(ch_c4);
ch_c5 = vysledky(2*s1+1:3*s1,5);      pch_c5 = sum(ch_c5);
ch_c6 = vysledky(2*s1+1:3*s1,6);      pch_c6 = sum(ch_c6);
ch_c7 = vysledky(2*s1+1:3*s1,7);      pch_c7 = sum(ch_c7);
ch_c8 = vysledky(2*s1+1:3*s1,8);      pch_c8 = sum(ch_c8);
ch_c9 = vysledky(2*s1+1:3*s1,9);      pch_c9 = sum(ch_c9);

ch_d1 = vysledky(3*s1+1:4*s1,1);      pch_d1 = sum(ch_d1);
ch_d2 = vysledky(3*s1+1:4*s1,2);      pch_d2 = sum(ch_d2);
ch_d3 = vysledky(3*s1+1:4*s1,3);      pch_d3 = sum(ch_d3);
ch_d5 = vysledky(3*s1+1:4*s1,5);      pch_d5 = sum(ch_d5);
ch_d6 = vysledky(3*s1+1:4*s1,6);      pch_d6 = sum(ch_d6);
ch_d7 = vysledky(3*s1+1:4*s1,7);      pch_d7 = sum(ch_d7);
ch_d8 = vysledky(3*s1+1:4*s1,8);      pch_d8 = sum(ch_d8);
ch_d9 = vysledky(3*s1+1:4*s1,9);      pch_d9 = sum(ch_d9);

ch_e1 = vysledky(4*s1+1:5*s1,1);      pch_e1 = sum(ch_e1);
ch_e2 = vysledky(4*s1+1:5*s1,2);      pch_e2 = sum(ch_e2);
ch_e3 = vysledky(4*s1+1:5*s1,3);      pch_e3 = sum(ch_e3);
ch_e4 = vysledky(4*s1+1:5*s1,4);      pch_e4 = sum(ch_e4);
ch_e6 = vysledky(4*s1+1:5*s1,6);      pch_e6 = sum(ch_e6);
ch_e7 = vysledky(4*s1+1:5*s1,7);      pch_e7 = sum(ch_e7);
ch_e8 = vysledky(4*s1+1:5*s1,8);      pch_e8 = sum(ch_e8);
ch_e9 = vysledky(4*s1+1:5*s1,9);      pch_e9 = sum(ch_e9);

ch_f1 = vysledky(5*s1+1:6*s1,1);      pch_f1 = sum(ch_f1);
ch_f2 = vysledky(5*s1+1:6*s1,2);      pch_f2 = sum(ch_f2);
ch_f3 = vysledky(5*s1+1:6*s1,3);      pch_f3 = sum(ch_f3);
ch_f4 = vysledky(5*s1+1:6*s1,4);      pch_f4 = sum(ch_f4);
ch_f5 = vysledky(5*s1+1:6*s1,5);      pch_f5 = sum(ch_f5);
ch_f7 = vysledky(5*s1+1:6*s1,7);      pch_f7 = sum(ch_f7);
ch_f8 = vysledky(5*s1+1:6*s1,8);      pch_f8 = sum(ch_f8);
ch_f9 = vysledky(5*s1+1:6*s1,9);      pch_f9 = sum(ch_f9);

ch_g1 = vysledky(6*s1+1:7*s1,1);      pch_g1 = sum(ch_g1);
ch_g2 = vysledky(6*s1+1:7*s1,2);      pch_g2 = sum(ch_g2);
ch_g3 = vysledky(6*s1+1:7*s1,3);      pch_g3 = sum(ch_g3);
ch_g4 = vysledky(6*s1+1:7*s1,4);      pch_g4 = sum(ch_g4);
ch_g5 = vysledky(6*s1+1:7*s1,5);      pch_g5 = sum(ch_g5);
ch_g6 = vysledky(6*s1+1:7*s1,6);      pch_g6 = sum(ch_g6);
ch_g8 = vysledky(6*s1+1:7*s1,8);      pch_g8 = sum(ch_g8);
ch_g9 = vysledky(6*s1+1:7*s1,9);      pch_g9 = sum(ch_g9);

ch_h1 = vysledky(7*s1+1:8*s1,1);      pch_h1 = sum(ch_h1);
ch_h2 = vysledky(7*s1+1:8*s1,2);      pch_h2 = sum(ch_h2);
ch_h3 = vysledky(7*s1+1:8*s1,3);      pch_h3 = sum(ch_h3);
ch_h4 = vysledky(7*s1+1:8*s1,4);      pch_h4 = sum(ch_h4);
ch_h5 = vysledky(7*s1+1:8*s1,5);      pch_h5 = sum(ch_h5);
ch_h6 = vysledky(7*s1+1:8*s1,6);      pch_h6 = sum(ch_h6);
ch_h7 = vysledky(7*s1+1:8*s1,7);      pch_h7 = sum(ch_h7);
ch_h9 = vysledky(7*s1+1:8*s1,9);      pch_h9 = sum(ch_h9);

ch_i1 = vysledky(8*s1+1:end,1);      pch_i1 = sum(ch_i1);
ch_i2 = vysledky(8*s1+1:end,2);      pch_i2 = sum(ch_i2);
ch_i3 = vysledky(8*s1+1:end,3);      pch_i3 = sum(ch_i3);
ch_i4 = vysledky(8*s1+1:end,4);      pch_i4 = sum(ch_i4);
ch_i5 = vysledky(8*s1+1:end,5);      pch_i5 = sum(ch_i5);
ch_i6 = vysledky(8*s1+1:end,6);      pch_i6 = sum(ch_i6);
ch_i7 = vysledky(8*s1+1:end,7);      pch_i7 = sum(ch_i7);
ch_i8 = vysledky(8*s1+1:end,8);      pch_i8 = sum(ch_i8);

ks1 = sum(v1);   % kontrolná suma - súčet príslušnej časti každého zo stĺpcov musí  
ks2 = sum(v2);   % byť rovný počtu obr. z danej triedy
ks3 = sum(v3);
ks4 = sum(v4);
ks5 = sum(v5);
ks6 = sum(v6);
ks7 = sum(v7);
ks8 = sum(v8);
ks9 = sum(v9);

% confusion matrix - vyjadrenie percentuálnej úspešnosti klasifikácie
cm = zeros(10);
a = (1:9);
b = a';
cm(1:1,2:10) = a;
cm(2:10,1:1) = b;

cm(2,2) = ks1/s1;    cm(3,3) = ks2/s1;    cm(4,4) = ks3/s1;    cm(5,5) = ks4/s1;
cm(6,6) = ks5/s1;    cm(7,7) = ks6/s1;    cm(8,8) = ks7/s1;    cm(9,9) = ks8/s1;    cm(10,10) = ks9/s1;

cm(3,2) = pch_a2/s1; cm(4,2) = pch_a3/s1; cm(5,2) = pch_a4/s1; cm(6,2) = pch_a5/s1; cm(7,2) = pch_a6/s1; cm(8,2) = pch_a7/s1; cm(9,2) = pch_a8/s1; cm(10,2) = pch_a9/s1;

cm(2,3) = pch_b1/s1; cm(4,3) = pch_b3/s1; cm(5,3) = pch_b4/s1; cm(6,3) = pch_b5/s1; cm(7,3) = pch_b6/s1; cm(8,3) = pch_b7/s1; cm(9,3) = pch_b8/s1; cm(10,3) = pch_b9/s1;

cm(2,4) = pch_c1/s1; cm(3,4) = pch_c2/s1; cm(5,4) = pch_c4/s1; cm(6,4) = pch_c5/s1; cm(7,4) = pch_c6/s1; cm(8,4) = pch_c7/s1; cm(9,4) = pch_c8/s1; cm(10,4) = pch_c9/s1;

cm(2,5) = pch_d1/s1; cm(3,5) = pch_d2/s1; cm(4,5) = pch_d3/s1; cm(6,5) = pch_d5/s1; cm(7,5) = pch_d6/s1; cm(8,5) = pch_d7/s1; cm(9,5) = pch_d8/s1; cm(10,5) = pch_d9/s1;

cm(2,6) = pch_e1/s1; cm(3,6) = pch_e2/s1; cm(4,6) = pch_e3/s1; cm(5,6) = pch_e4/s1; cm(7,6) = pch_e6/s1; cm(8,6) = pch_e7/s1; cm(9,6) = pch_e8/s1; cm(10,6) = pch_e9/s1;

cm(2,7) = pch_f1/s1; cm(3,7) = pch_f2/s1; cm(4,7) = pch_f3/s1; cm(5,7) = pch_f4/s1; cm(6,7) = pch_f5/s1; cm(8,7) = pch_f7/s1; cm(9,7) = pch_f8/s1; cm(10,7) = pch_f9/s1;

cm(2,8) = pch_g1/s1; cm(3,8) = pch_g2/s1; cm(4,8) = pch_g3/s1; cm(5,8) = pch_g4/s1; cm(6,8) = pch_g5/s1; cm(7,8) = pch_g6/s1; cm(9,8) = pch_g8/s1; cm(10,8) = pch_g9/s1;

cm(2,9) = pch_h1/s1; cm(3,9) = pch_h2/s1; cm(4,9) = pch_h3/s1; cm(5,9) = pch_h4/s1; cm(6,9) = pch_h5/s1; cm(7,9) = pch_h6/s1; cm(8,9) = pch_h7/s1; cm(10,9) = pch_h9/s1;

cm(2,10)= pch_i1/s1; cm(3,10)= pch_i2/s1; cm(4,10)= pch_i3/s1; cm(5,10) = pch_i4/s1; cm(6,10) = pch_i5/s1; cm(10,2) = pch_i6/s1; cm(8,10) = pch_i7/s1; cm(9,10) = pch_i8/s1;


cm(2:end, 2:end) = cm(2:end, 2:end)*100;

% výpočet počtu chýb
p_ch = cn(1)*s1-ks1-ks2-ks3-ks4-ks5-ks6-ks7-ks8-ks9;

% celková úspešnosť klasifikácie v %
u_p = (cn(1)*s1-p_ch)/(cn(1)*s1)*100;
%   disp(p_ch)
warning('off')
disp(cm)
disp(u_p)