% clear all; 
clear('data');
clc; close 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');
% hh = load('/Users/kylyi/Documents/MATLAB/Bakalarka/BakalarskaPrace/pat_13_0/L5/vert_24_an.mat');
% ii = 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');
% 
% obratel{1} = logical(a.mask_vert(:,:,49));
% obratel{2} = logical(b.mask_vert(:,:,46));
% obratel{3} = logical(c.mask_vert(:,:,38));
% 
% obratel{4} = logical(d.mask_vert(:,:,83));
% obratel{5} = logical(e.mask_vert(:,:,89));
% obratel{6} = logical(f.mask_vert(:,:,88));
% 
% obratel{7} = logical(g.mask_vert(:,:,129));
% obratel{8} = logical(hh.mask_vert(:,:,129));
% obratel{9} = logical(ii.mask_vert(:,:,142));
% 
% obratel{10} = logical(j.mask_vert(:,:,82));
% obratel{11} = logical(k.mask_vert(:,:,79));
% obratel{12} = logical(l.mask_vert(:,:,84));
% 
% obratel{13} = logical(m.mask_vert(:,:,110));
% obratel{14} = logical(n.mask_vert(:,:,107));
% obratel{15} = logical(o.mask_vert(:,:,105));
%% Generovani zakladnich tvaru
% ctverec = VytvorCtverec(234);
% obdelnik = VytvorObdelnik(100,200);
% kruh = VytvorKruh(226);
% trojuhelnik = VytvorTrojuhelnik([200 224 224]);
% kosoctverec = VytvorKosoctverec(238, 45);
% mnohouhelnik = VytvorMnohouhelnik(5, 200);
% obj2 = dvaobjekty( VytvorMnohouhelnik(3,300), VytvorMnohouhelnik(4,200) ); % Porovnani dvou obrazu o
                                                             % jinem rozliseni

% %% Obrazova data nahodneho tvaru
% 
% obraz = imread('obrazky/bunky.tif');
% % nahodny_tvar = load ('obrazky/data_3pyl.mat');
% % obraz = obraz(:,:,1);
% 
% I2 = obraz(:,:,2);
% I1 = obraz(:,:,1);
% level = graythresh(I2);
% bw2 = im2bw(I2, level);
% 
% level = graythresh(I1);
% bw = im2bw(I1, level);
% 
% alive = bw2 - bw;
% dead = ~bw;
% dead(alive==1)= 0;
% 
% alive = imclose(alive, strel('disk',3));
% dead = imclose(dead, strel('disk', 3));
% 
% obraz_cb = dead;
% % obraz = obraz.x(:,1);
% % im3age_after_histeq = histeq(image); % Roztahuje histogram obrazu
% 
% if size(obraz, 2) == 1
%     obraz = reshape(obraz(:,1),[sqrt(length(obraz)),sqrt(length(obraz))]); % prepis obrazu
%                                                         % do matice
% end


%% Vyvazeni pozadi

% obraz = vyvazeni_pozadi(obraz);
% load('/Users/kylyi/Documents/MATLAB/Bakalarka/Bc/pat_17_0/Th10/vert_17_an.mat');
%% Prevod do cernobile (binarni) reprezentace


for mnop = 1:1
obraz = aVytvorKruh(51); %imread('obrazkysplash.jpg');
    if mnop == 1
        obraz(78,79) = 0; %P1C3
    elseif mnop==9
        obraz(150,71) = 0; %P3L5
        obraz(149,69) = 0; %P3L5
        obraz(148,68) = 0; %P3L5
        obraz(147,67) = 0; %P3L5
    end


% obraz(151:152, 74) = 0;
% obraz(145,132) = 0;

% level = graythresh(obraz); % automaticke nastaveni prahu
obraz_cb = im2bw(obraz, 0.5); % prevod do cb
obraz_cb = bwareaopen(obraz_cb, 1); % male objekty ignorovany
% obraz_cb = ~obraz_cb; % invertovani barev


%% Separace objektu v obraze
obj = obraz_cb;  % obj2; 

% BW1 = filtrace(alive);
% BW2 = filtrace(dead);

[objekty, cc] = regiony(obj); % definice jednotlivych objektu
graindata = region_data(obj, cc, objekty); % informace o objektech 

%% Deskriptory
h = 1;
for i = 1:size(objekty,2)
    obj = objekty{1,i};
    [obrys, rada_obrys, vektory_obrysu] = VytvorObrys(obj); % popis je uvnitr fce
    [pocet_hran, delka_hran, smer_hran] = hrany(vektory_obrysu); % popis uvnitr
    smer_hran(smer_hran==0) = [];
    
    % Fourierka
    
    F = fft2(obj);
    F = fftshift(F); % Center FFT
    F = abs(F); % Get the magnitude
    F = log(F+1); % Use log, for perceptual scaling, and +1 since log(0) is undefined
    F = mat2gray(F); % Use mat2gray to scale the image between 0 and 1
    
%     figure;
%     imshow(F,[]); % Display the result
%     title('2D-FT');
    
%     [~, AOH, PhiOH] = Zernikmoment(logical(obj),4,2);      % Call Zernikemoment fuction
%     figure;
%     imshow(logical(obj));
%     xlabel({['Z = ' num2str(AOH)]; ['\phi = ' num2str(PhiOH)]});
%     figure
%     subplot(1,2,i)
%     imshow(obj);
%     subplot(1,2,i)
%     imshow(obj);

    rgb = label2rgb(rada_obrys); % Vektory obrysu jsou obarveny podle smeru
    obj_data = geom_deskr( obj ); % geometricke deskriptory
%     figure
%     imshow(RGB_label); hold on;
%     plot(obj_data.Centroid(1), obj_data.Centroid(2), 'oblack'); % vykresli centroid
    

    [zernikMoments, fi] = zernikeMoments(obj);


    disp(['Obraz ma ' num2str(pocet_hran) ' hran, jejich delky jsou: ' ...
        num2str(delka_hran) '.']);
    
    vzdalenost = vzdalenostOdCentroidu(rada_obrys, obj_data);
    
    
    data.Obsah{h} = obj_data.Area;
    data.Obvod{h} = obj_data.Perimeter;
    data.Orientace{h} = obj_data.Orientation;
    data.DelkaHlOsy{h} = obj_data.MajorAxisLength;
    data.DelkaVedlOsy{h} = obj_data.MinorAxisLength;
    data.Centroidy{h} = obj_data.Centroid + 1;
    data.Kulatost{h} = sum(abs(vzdalenost.Vzdalenosti-vzdalenost.prumernaVzdalenost)/vzdalenost.prumernaVzdalenost);
    data.Kompaktnost{h} = data.Obvod{h}^2/(4*pi*data.Obsah{h});
    data.ID{h} = i;
    data.DelkyHran{h} = delka_hran;
    data.PocetHran{h} = pocet_hran;
    data.SmerHran{h} = vektory_obrysu;
    data.Obrys{h} = obrys;
    data.minVzd{h} = vzdalenost.MinVzdalenost;
    data.maxVzd{h} = vzdalenost.MaxVzdalenost;
%     data.minVzdPozice{h} = vzdalenost.MinVzdalenostPozice;
%     data.maxVzdPozice{h} = vzdalenost.MaxVzdalenostPozice;
    data.prumVzdalenost{h} = vzdalenost.prumernaVzdalenost;
    data.Vzdalenosti{h} = round(vzdalenost.Vzdalenosti);
    data.Hranatost{h} = data.Obsah{h} / (((size(obj,1)-2)*(size(obj,2)-2)));
    data.FD{h} = log(round(abs(fft(round(data.Vzdalenosti{h})))));
    data.FD{h}(data.FD{h} < 0) = 0;
    data.ZM{h} = zernikMoments;
    data.rgb{h} = rgb;
    data.obj{h} = obj;
    data.smerHran{h} = smer_hran;
    data.x{h} = vzdalenost.Vzdalenosti;
    
    
%     if hh == 1
%        figure(lol)
%     imshow(obj);
%     title(sprintf('%s.', p(lol)));
%     saveas(figure(lol),sprintf('obrazky/%s%s%s.jpg', u,uu,num2str(h)))
%     lol = lol + 1;
%     
%     
%     figure(lol);
%     imshow(rgb); hold on;
%     plot(data.Centroidy{h}(1), data.Centroidy{h}(2), 'oblack');
%     title(sprintf('%s.', p(lol)));
%     saveas(figure(lol),sprintf('obrazky/%s%sOb%s.jpg', u,uu,num2str(h)))
%     lol = lol + 1;
%     
%     
%     figure(lol);
%     plot(data.x{h});
%     title(sprintf('%s.', p(lol)));
%     xlabel('Stupne [-]');
%     ylabel('Vzdalenost [px]');
%     ylim([min(data.x{h}-20) max(data.x{h}+20)])
%     saveas(figure(lol),sprintf('obrazky/%s%sVzd%s.jpg', u,uu,num2str(h)))
%     lol = lol + 1;
%     
%     
%     figure(lol)
%     stem(data.FD{h});
%     title(sprintf('%s.', p(lol)));
%     saveas(figure(lol),sprintf('obrazky/%s%sFd%s.jpg', u,uu,num2str(h)))
%     lol = lol + 1;
%     
%         
%     end
    h = h+1;
end

data_celkem{mnop} = data;
end

data