function   klasif()
global myGLOBAL;
global T INFO;
%% funkcia klasif
% sluzi za ulozenie vsetkych parametrov (radio/fotometrickych) do
% globalnych premennych. Zaroven klasifikuje do tried na zaklade tvaru,
% smerodajnej odchylky a tym testovani normality objektov. For cyklus
% prebieha len pre obrazky vybrane na detekciu. Vyuziva 3 globalne premenne
% ktore su dalej pouzite v inych funkciach (T,myGLOBAL,INFO)
D = INFO.Detected;
INFO.EXTENT = [];
INFO.AREA=[];
INFO.CIRCULARITY =[];
INFO.PERIMETER = [];
INFO.STD =[];
INFO.MASSPAREA=[];
INFO.MASS = [];
for i= 1:length(D)
    X = T(D(i)).IMAGE.ORIG;
    char = myGLOBAL(D(i)).Properties;
    hmotnosti = myGLOBAL(D(i)).Particles;
    [char(:).Thinnes]=deal([]);
    [char(:).Circularity]=deal([]);
    [char(:).Elongation]=deal([]);
    
    [char(:).IOD]=deal([]);
    [char(:).AOD]=deal([]);
    [char(:).STD]=deal([]);
    
    %% Thinnes Ratio
    % ukazuje pravidelnost objektu. Pocita sa pomocou plochy a obvodu a hodnoty
    % okolo 1 urcuju pravidelnost objektu. Cim vyssia hodnota tym okruhlejsi
    % objekt.
    for i1=1:length(char)
        char(i1).Thinnes(1,1)=4*pi*((char(i1).Area)/((char(i1).Perimeter)^2));
    end
    
    %% Circularity
    %opacna ku Thinnes Ratio. Ukazuje na komplexnost hranic objektu, kde hodnota
    %priblizujuca sa k 1 hovori o okruhlosti objektu a hodnoty vyssie ako 1
    %ukazuju na zlozitejsie hranice. Inverzna ku Thinnes Ratio
    for i1=1:length(char)
        char(i1).Circularity(1,1)=((char(i1).Perimeter)^2)/(4*pi*(char(i1).Area));
    end
    
    %% Elongation
    % hovori o natiahnutosti objektu kde berieme pomer medzi dlhsou osou
    % obkreslenej elipsy a kratsou osou tejto elipsy. Hodnoty 1 hovoria
    % o symetrickosti objektu, vyssie hodnoty urcuju pretiahnutost objektu.
    
    for i1=1:length(char)
        char(i1).Elongation(1,1)=(char(i1).MajorAxisLength)/(char(i1).MinorAxisLength);
    end
    
    
    
    %% Grayscale parametres
    %pocita zakladne parametre nie v binarnom ale v sedotonovom obraze
    %IOD - suma vsetkych intenzit sedych levelov v objekte
    %AOD - priemerna intenzita objektu
    %STD - udava kolisanie intenzit v ramci objektu
    for ja=1:length(char)
        kon=char(ja).Area;
        for jb=1:kon
            MAT(1,jb)=X(char(ja).PixelIdxList(jb));
        end
        IOD=sum(MAT);
        AOD=mean(MAT);
        STD=std(MAT);
        char(ja).IOD=IOD;
        char(ja).AOD=AOD;
        char(ja).STD=STD;
        MAT=[]; AOD=[]; STD=[]; IOD=[];
    end
    %% Vytvaranie pomocnej premennej GlobalProp
    Shape1=[];
    Shape2=[];
    Shape3=[];
    Shape4=[];
    
    for j= 1:length(char)
        GlobalProp(i).AREA(j) = char(j).Area;
        GlobalProp(i).EXTENT(j) = char(j).Extent;
        GlobalProp(i).PERIMETER(j) = char(j).Perimeter;
        GlobalProp(i).FILLEDAREA(j) = char(j).FilledArea;
        GlobalProp(i).SOLIDITY(j) = char(j).Solidity;
        GlobalProp(i).ECCENTICITY(j) = char(j).Eccentricity;
        GlobalProp(i).IOD(j) = char(j).IOD;
        GlobalProp(i).AOD(j) = char(j).AOD;
        GlobalProp(i).STD(j) = char(j).STD;
        GlobalProp(i).THINNES(j) = char(j).Thinnes;
        GlobalProp(i).CIRCULARITY(j) = char(j).Circularity;
        GlobalProp(i).ELONGATION(j) = char(j).Elongation;
        GlobalProp(i).MASS(j) = hmotnosti(j).MassFinal;
        GlobalProp(i).MASSPAREA(j) = hmotnosti(j).MassFinal_Area;
        myGLOBAL(D(i)).GlobalProp = GlobalProp(i);
        BIN=myGLOBAL(D(i)).Images.BINAR;
        myGLOBAL(D(i)).AVBACK = mean(X(BIN==1));
        myGLOBAL(D(i)).AVPART = mean(X(BIN==0));
        
        
    end
    
    %% Klasifikacia tvarom
    % rozhodovaci strom zapisany vo forme podmienok. Ako rozhodovaci
    % parameter je na zaciatku pouzita okruhlost (Thinnes ratio). Nasledne
    % sa este porovnava parameter predlzenosti pre hranate objekty
    % (Elongation) ktory ich deli na stvorcove a obdelnikove. Celkovo su
    % objekty rozdelene do 4 skupin: okruhle, komplexne, stvorcove a
    % obdlznikove.
    
    
    vectorEXTENT = GlobalProp(i).EXTENT;
    THIN = GlobalProp(i).THINNES;
    vectorCIR = GlobalProp(i).CIRCULARITY;
    for jjj = 1:length(char)
        
        if THIN(jjj)>=0.7 && THIN(jjj)<0.85
            if char(jjj).Elongation < 2
                char(jjj).Shape = 2; %stvorcove
                Shape2(1,end+1)=jjj;
            else
                Shape4(1,end+1)=jjj;%predlzene
            end
            
        elseif  THIN(jjj)>=0.85
            char(jjj).Shape = 1; % gulate
            Shape1(1,end+1)=jjj;
            
        elseif THIN(jjj)<0.7
            char(jjj).Shape = 3; %komplexne
            Shape3(1,end+1)=jjj;
        else
            char(jjj).Shape = 3; %komplexne
            Shape3(1,end+1)=jjj;
            disp('Error')
        end
        
    end
    
    
    %% priprava na klasif
    % Tato cast dava dokopy paramtere vsetkych najdenych objektov zo vsetkych
    % spracovanych obrazov. Kompletne parametre su potom pouzite na
    % statistiku, trend a analyzu k-means vo funkcii gui2.m. Funkcia
    % zaroven uklada tvary objektov do globalnej premennej myGLOBAL
    if isempty(myGLOBAL(D(i)).Boundaries)==0
        INFO.EXTENT = [INFO.EXTENT GlobalProp(i).EXTENT];
        INFO.AREA = [INFO.AREA GlobalProp(i).AREA];
        INFO.CIRCULARITY = [INFO.CIRCULARITY GlobalProp(i).CIRCULARITY];
        INFO.PERIMETER =[INFO.PERIMETER GlobalProp(i).PERIMETER];
        INFO.STD = [INFO.STD GlobalProp(i).STD];
        INFO.MASSPAREA= [INFO.MASSPAREA GlobalProp(i).MASSPAREA];
        INFO.MASS =[INFO.MASS GlobalProp(i).MASS];
        
        myGLOBAL(D(i)).char= char;
        myGLOBAL(D(i)).Shape1= Shape1;
        myGLOBAL(D(i)).Shape2= Shape2;
        myGLOBAL(D(i)).Shape3= Shape3;
        myGLOBAL(D(i)).Shape4= Shape4;
        LShape1(1,i)=length(Shape1);
        LShape2(1,i)=length(Shape2);
        LShape3(1,i)=length(Shape3);
        LShape4(1,i)=length(Shape4);
    else
    end
    
end

%% klasifikacia normalnych a abnormalnych objektov
% Nasledujuci usek programu, pocita medze normality vyznamnych parametrov 
% pomocou smerodajnej odchylky. (Pre hmotnost, plochu, STD)
% MASS
MASSav = mean(INFO.MASS);
MASSStd = std(INFO.MASS,1);
MASSav1 = MASSav+1.5*MASSStd; 
MASSav2 = MASSav-1.5*MASSStd;
MASSav = [MASSav1 MASSav2];
INFO.MassClass = MASSav;

a1  = find(INFO.MASS>MASSav2);
a2  = find(INFO.MASS<MASSav1);
at = intersect(a1,a2);
NORM.MASSM = mean (INFO.MASS(at(1:end)));
NORM.AREAM = mean (INFO.AREA(at(1:end)));
NORM.PERIMM = mean (INFO.PERIMETER(at(1:end)));
NORM.STDM = mean (INFO.STD(at(1:end)));
NORM.CIRCULARITYM = mean (INFO.CIRCULARITY(at(1:end)));
NORM.MASSPAREAM = mean (INFO.MASSPAREA(at(1:end)));
NORM.POCETM = length(at);



%AREA
AREAav = mean(INFO.AREA);
AREAStd = std(INFO.AREA,1);
AREAav1 = AREAav+1.5*AREAStd;
AREAav2 = AREAav-1.5*AREAStd;

AREAav = [AREAav1 AREAav2];
INFO.APClass = AREAav;

b1  = find(INFO.AREA>AREAav2);
b2  = find(INFO.AREA<AREAav1);
bt = intersect(b1,b2);
NORM.MASSA = mean (INFO.MASS(bt(1:end)));
NORM.AREAA = mean (INFO.AREA(bt(1:end)));
NORM.PERIMA = mean (INFO.PERIMETER(bt(1:end)));
NORM.STDA = mean (INFO.STD(bt(1:end)));
NORM.CIRCULARITYA = mean (INFO.CIRCULARITY(bt(1:end)));
NORM.MASSPAREAA = mean (INFO.MASSPAREA(bt(1:end)));
NORM.POCETA = length(bt);
%STD
STDav = mean(INFO.STD);
STDStd = std(INFO.STD,1);
STDav = STDav+1.5*STDStd;
INFO.STDClass = STDav;

bt  = find(INFO.STD>STDav);
bl = find(INFO.STD<STDav);
NORM.MASSS = mean (INFO.MASS(bt(1:end)));
NORM.AREAS = mean (INFO.AREA(bt(1:end)));
NORM.PERIMS = mean (INFO.PERIMETER(bt(1:end)));
NORM.STDS = mean (INFO.STD(bt(1:end)));
NORM.CIRCULARITYS = mean (INFO.CIRCULARITY(bt(1:end)));
NORM.MASSPAREAS = mean (INFO.MASSPAREA(bt(1:end)));
NORM.AREAS = mean (INFO.AREA(bt(1:end)));
NORM.POCETS = length(bl);
MassABN = 0;MassN = 0;
StdABN = 0;StdN = 0;
AreaABN = 0;AreaN = 0;

%% for cyklus
% Porovnava parametre (m,A,STD) pre kazdu detekovanu casticu s medzami a nasledne ich
% vyhodnocuje ako normalne / abnormalne. Udaje o poctoch a indexoch
% konkretnych objektov sa ukladaju do globalnej premennej myGLOBAL a
% nasledne su volane do gui2.m a gui1.m kvoli klasifikacii a statistike.
% GPm = myGLOBAL;
% GPa = myGLOBAL;
% GPs = myGLOBAL;

% for kk=1:length(D)
%     hla = myGLOBAL(D(kk)).Properties;
%     
%     posMN = [];
% posAABN = []; posAN = []; posSN = []; posSABN = []; posMABN = [];
%     for ll=1:length(hla)
%         if  myGLOBAL(D(kk)).GlobalProp.MASS(ll)>MASSav1 || myGLOBAL(D(kk)).GlobalProp.MASS(ll)<MASSav2
%             MassABN = MassABN+1;
%             %            posMABN(1,end+1) = kk;
%             GPm(D(kk)).GlobalProp.MASS(ll) = [];
% %              GPm(D(kk)).GlobalProp.MASSPAREA(ll) = [];
%             GPm(D(kk)).GlobalProp.AREA(ll) = [];
%             GPm(D(kk)).GlobalProp.PERIMETER(ll) = [];
%             GPm(D(kk)).GlobalProp.CIRCULARITY(ll) = [];
%             GPm(D(kk)).GlobalProp.EXTENT(ll) = [];
%             GPm(D(kk)).GlobalProp.STD(ll) = [];
%             GPm(D(kk)).GlobalProp.THINNES(ll) = [];
%             GPm(D(kk)).GlobalProp.MASSPAREA(ll) = [];
%             posMABN  = [posMABN ll];
%         else
%             MassN = MassN+1;
%             %             posMN(1,end+1) = kk;
%             posMN  = [posMN ll];
%         end
%         if  myGLOBAL(D(kk)).GlobalProp.STD(ll)>STDav
%             StdABN = StdABN+1;
%             %            posSABN(1,end+1) = kk;
%              posSABN  = [posSABN ll];
%              GPs(D(kk)).GlobalProp.MASS(ll) = [];
% %               GPs(D(kk)).GlobalProp.MASSPAREA(ll) = [];
%             GPs(D(kk)).GlobalProp.AREA(ll) = [];
%             GPs(D(kk)).GlobalProp.PERIMETER(ll) = [];
%             GPs(D(kk)).GlobalProp.CIRCULARITY(ll) = [];
%             GPs(D(kk)).GlobalProp.EXTENT(ll) = [];
%             GPs(D(kk)).GlobalProp.STD(ll) = [];
%             GPs(D(kk)).GlobalProp.THINNES(ll) = [];
%             GPs(D(kk)).GlobalProp.MASSPAREA(ll) = [];
%         else
%             StdN = StdN+1;
%             %             posSN(1,end+1) = kk;
%             posSN  = [posSN ll];
%         end
%         if  myGLOBAL(D(kk)).GlobalProp.AREA(ll)>AREAav1 || myGLOBAL(D(kk)).GlobalProp.AREA(ll)<AREAav2
%             AreaABN = AreaABN+1;
%             %             posAABN(1,end+1) = kk;
%             posAABN  = [posAABN ll];
%              GPa(D(kk)).GlobalProp.MASS(ll) = [];
% %              GPa(D(kk)).GlobalProp.MASSPAREA(ll) = [];
%             GPa(D(kk)).GlobalProp.AREA(ll) = [];
%             GPa(D(kk)).GlobalProp.PERIMETER(ll) = [];
%             GPa(D(kk)).GlobalProp.CIRCULARITY(ll) = [];
%             GPa(D(kk)).GlobalProp.EXTENT(ll) = [];
%             GPa(D(kk)).GlobalProp.STD(ll) = [];
%             GPa(D(kk)).GlobalProp.THINNES(ll) = [];
%             GPa(D(kk)).GlobalProp.MASSPAREA(ll) = [];
%         else
%             AreaN = AreaN+1;
%             %             posAN(1,end+1) = kk;
%             posAN  = [posAN ll];
%         end
%         myGLOBAL(D(kk)).MassN  = MassN;
%         myGLOBAL(D(kk)).MN  = posMN;
%         myGLOBAL(D(kk)).MABN  = posMABN;
%         myGLOBAL(D(kk)).MassABN  = MassABN;
%         
%         myGLOBAL(D(kk)).StdN  = StdN;
%         myGLOBAL(D(kk)).StdABN  = StdABN;
%         myGLOBAL(D(kk)).SABN  =   posSABN;
%         myGLOBAL(D(kk)).SN  =   posSN;
%         myGLOBAL(D(kk)).AreaN  = AreaN;
%         myGLOBAL(D(kk)).AreaABN  = AreaABN;
%         myGLOBAL(D(kk)).AABN  =   posAABN ;
%         myGLOBAL(D(kk)).AN  =  posAN;
%     end
%      
    

LS1 = sum(LShape1(1:end));
LS2 = sum(LShape2(1:end));
LS3 = sum(LShape3(1:end));
LS4 = sum(LShape4(1:end));
disp('OK')

INFO.NORM = NORM;






