% clear all;
% 
a = load('/Users/kylyi/Documents/MATLAB/Bakalarka/BakalarskaPrace/pat_04_2/C3/vert_3_an.mat');
b = load('/Users/kylyi/Documents/MATLAB/Bakalarka/BakalarskaPrace/pat_13_0/C3/vert_3_an.mat');
c = load('/Users/kylyi/Documents/MATLAB/Bakalarka/BakalarskaPrace/pat_17_0/C3/vert_3_an.mat');

d = load('/Users/kylyi/Documents/MATLAB/Bakalarka/BakalarskaPrace/pat_04_2/L3/vert_22_an.mat');
e = load('/Users/kylyi/Documents/MATLAB/Bakalarka/BakalarskaPrace/pat_13_0/L3/vert_22_an.mat');
f = load('/Users/kylyi/Documents/MATLAB/Bakalarka/BakalarskaPrace/pat_17_0/L3/vert_22_an.mat');

g = load('/Users/kylyi/Documents/MATLAB/Bakalarka/BakalarskaPrace/pat_04_2/L5/vert_24_an.mat');
h = load('/Users/kylyi/Documents/MATLAB/Bakalarka/BakalarskaPrace/pat_13_0/L5/vert_24_an.mat');
i = load('/Users/kylyi/Documents/MATLAB/Bakalarka/BakalarskaPrace/pat_17_0/L5/vert_24_an.mat');

j = load('/Users/kylyi/Documents/MATLAB/Bakalarka/BakalarskaPrace/pat_04_2/Th4/vert_11_an.mat');
k = load('/Users/kylyi/Documents/MATLAB/Bakalarka/BakalarskaPrace/pat_13_0/Th4/vert_11_an.mat');
l = load('/Users/kylyi/Documents/MATLAB/Bakalarka/BakalarskaPrace/pat_17_0/Th4/vert_11_an.mat');

m = load('/Users/kylyi/Documents/MATLAB/Bakalarka/BakalarskaPrace/pat_04_2/Th10/vert_17_an.mat');
n = load('/Users/kylyi/Documents/MATLAB/Bakalarka/BakalarskaPrace/pat_13_0/Th10/vert_17_an.mat');
o = load('/Users/kylyi/Documents/MATLAB/Bakalarka/BakalarskaPrace/pat_17_0/Th10/vert_17_an.mat');


data{1} = logical(a.mask_vert(:,:,49));
data{2} = logical(b.mask_vert(:,:,46));
data{3} = logical(c.mask_vert(:,:,38));

data{4} = logical(d.mask_vert(:,:,83));
data{5} = logical(e.mask_vert(:,:,89));
data{6} = logical(f.mask_vert(:,:,88));

data{7} = logical(g.mask_vert(:,:,129));
data{8} = logical(h.mask_vert(:,:,129));
data{9} = logical(i.mask_vert(:,:,142));

data{10} = logical(j.mask_vert(:,:,82));
data{11} = logical(k.mask_vert(:,:,79));
data{12} = logical(l.mask_vert(:,:,84));

data{13} = logical(m.mask_vert(:,:,110));
data{14} = logical(n.mask_vert(:,:,107));
data{15} = logical(o.mask_vert(:,:,105));

% data{1} = VytvorObrys(logical(a.mask_vert(:,:,49)));
% data{2} = VytvorObrys(logical(b.mask_vert(:,:,46)));
% data{3} = VytvorObrys(logical(c.mask_vert(:,:,38)));
% 
% data{4} = VytvorObrys(logical(d.mask_vert(:,:,83)));
% data{5} = VytvorObrys(logical(e.mask_vert(:,:,89)));
% data{6} = VytvorObrys(logical(f.mask_vert(:,:,88)));
% 
% data{7} = VytvorObrys(logical(g.mask_vert(:,:,129)));
% data{8} = VytvorObrys(logical(h.mask_vert(:,:,129)));
% data{9} = VytvorObrys(logical(i.mask_vert(:,:,142)));
% 
% data{10} = VytvorObrys(logical(j.mask_vert(:,:,82)));
% data{11} = VytvorObrys(logical(k.mask_vert(:,:,79)));
% data{12} = VytvorObrys(logical(l.mask_vert(:,:,84)));
% 
% data{13} = VytvorObrys(logical(m.mask_vert(:,:,110)));
% data{14} = VytvorObrys(logical(n.mask_vert(:,:,107)));
% data{15} = VytvorObrys(logical(o.mask_vert(:,:,105)));


for i =1:3:15
    for j = 1:3
       [m,n] = find(data{i+j-1}==1);
       o = min(m);
       p = max(m);
       q = min(n);
       w = max(n);
       data{i+j-1} = data{i+j-1}(o:p, q:w);
    end
    
    ee(i) = max(size(data{i},1), max(size(data{i+1},1), size(data{i+2},1)));
    ff(i) = max(size(data{i},2), max(size(data{i+1},2), size(data{i+2},2)));
end

for i = 1:3:15
    for j = 1:3
        data{i+j-1} =  [zeros(ceil((ee(i) - size(data{i+j-1},1))/2), size(data{i+j-1},2));
            data{i+j-1};
            zeros(floor((ee(i) - size(data{i+j-1},1))/2), size(data{i+j-1},2))];
        
        data{i+j-1} = [zeros(ee(i), ceil((ff(i) - size(data{i+j-1},2))/2)) data{i+j-1} zeros(ee(i), floor((ff(i) - size(data{i+j-1},2))/2))];
        
%         x{:,i+j-1} = data{i+j-1}(:);
    end
end

e = max(ee);
f = max(ff);
for i = 1:15
    data{i} =  [zeros(ceil((e - size(data{i},1))/2), size(data{i},2));
        data{i};
        zeros(floor((e - size(data{i},1))/2), size(data{i},2))];

    data{i} = [zeros(e, ceil((f - size(data{i},2))/2)) data{i} zeros(e, floor((f - size(data{i},2))/2))];

    x{:,i} = data{i}(:);
    
end
  
clear('w');
uh = 1;    
for i = 1:3:15
    x_trida{uh} = data{i};
    w{uh} = x{:,i};
    uh = uh + 1;
end






N          = size(x{1},1); % delka vstupniho vektoru
MTest      = size(x,2); % pocet testovacich vstupnich obrazu

% figure % kresba 1.vzoru
% imshow(x_trida{1})
% title('Referen?n? C3')
% xlabel('Vzorky')
% ylabel('Vzorky')
% 
% figure % kresba 2.vzoru
% imshow(x_trida2)
% title('Referen?n? L3')   
% xlabel('Vzorky')
% ylabel('Vzorky')
% 
% figure % kresba 3.vzoru
% imshow(x_trida3)
% title('Referen?n? L5')
% xlabel('Vzorky')
% ylabel('Vzorky')
% 
% figure % kresba 3.vzoru
% imshow(x_trida3)
% title('Referen?n? Th4')
% xlabel('Vzorky')
% ylabel('Vzorky')
% 
% figure % kresba 3.vzoru
% imshow(x_trida3)
% title('Referen?n? Th10')
% xlabel('Vzorky')
% ylabel('Vzorky')

    

%% B. VYPOCET ODPOVEDI SITE NA PREDLOZENE VSTUPNI VZORY (ROZPOZNAVANI VZORU)

for i = 1:MTest
    y_p1       = neuron_hamming(x{:,i},w{1}, N); % vypocet vystupu 1.neuronu
    ham_p1     = N - y_p1;                % vypocet hammingove vzdalenosti na vystupu 1.neuronu
    y_p2    = neuron_hamming(x{:,i},w{2}, N); % vypocet vystupu 2.neuronu
    ham_p2  = N - y_p2;                   % vypocet hammingove vzdalenosti na vystupu 2.neuronu
    y_p3   = neuron_hamming(x{:,i},w{3}, N); % vypocet vystupu 3.neuronu
    ham_p3 = N - y_p3;            % vypocet hammingove vzdalenosti na vystupu 3.neuronu
    y_p4   = neuron_hamming(x{:,i},w{4}, N); % vypocet vystupu 3.neuronu
    ham_p4 = N - y_p4;            % vypocet hammingove vzdalenosti na vystupu 3.neuronu
    y_p5   = neuron_hamming(x{:,i},w{5}, N); % vypocet vystupu 3.neuronu
    ham_p5 = N - y_p5;            % vypocet hammingove vzdalenosti na vystupu 3.neuronu
    
    ham = [ham_p1 ham_p2 ham_p3 ham_p4 ham_p5]; 
    [min_ham, imin] = min(ham);                           % vyhledavani minimalni vzdalenosti
    
    if imin==1                                            % prirazeni urcite tridy na zaklade vypoctene minimalni vzdalenosti
        trida = 'Obratel C3';
    elseif imin==2
        trida = 'Obratel L3';
    elseif imin==3
        trida = 'Obratel L5';
    elseif imin==4
        trida = 'Obratel TH4';
    elseif imin==5
        trida = 'Obratel Th10';
    end
    
    y(i) = imin;
end





