function  masscalc
%% vypocet hmotnosti
%funkcia pocita hmotnsot pozadia a objektov v detekovanych obrazoch.

global T; global myGLOBAL INFO
DET = INFO.Detected;
for d = 1:length(DET)
    X=T(DET(d)).IMAGE.ORIG;
    FINAL = myGLOBAL(DET(d)).Images.FINAL;
    BINAR = myGLOBAL(DET(d)).Images.BINAR;
    
    Ef  = T(DET(d)).HEAD.COUNT_EF;    %zberna efektivita
    I = T(DET(d)).HEAD.CURRENT;       %prud vzorkou 
    D   = T(DET(d)).HEAD.DOSE;        %davka
    dx = T(DET(d)).HEAD.PIXELSIZE(1)*T(DET(d)).HEAD.PIXELSIZE(2); %rozmery pixlov
    dt= T(DET(d)).HEAD.PIXELTIM;      %cas snimania pixlu
    e=1.6022e-19;                     %element. naboj elektronu
   

    %% background calculation
    NumB = length(BINAR(BINAR==1)); %pocet pixlov pozadia
    AreaB = NumB*dx; % plocha pozadia
    SumaB=sum(X(BINAR==1)); %suma hodnot pozadia
    RateB=SumaB/(NumB*dt); % priemerny pocet dopadajucich elektronov
    AvIB = (RateB*(1.6e-7))/Ef;  % priemerny prud 

    MassB = 191*SumaB/D*Ef; 
    % hmotnost [kDa] (191 - kalibracny faktor stanoveny 
    %pre dany mikroskop pozorovanim a overeny experimentalne)
    MassBA = MassB/AreaB; % hmotnost / plocha [kDa/nm2]
    ScatB = SumaB / (D*AreaB*Ef); % rozptyl
    meanB = MassB/NumB;
  
    % ukladanie parametrov do jednej strukturovanej premennej
    Background = [];
    Background.PixelNumber = NumB;
    Background.Area = AreaB;
    Background.Sum = SumaB;
    Background.Rate = RateB;
    Background.AvCurrent = AvIB;
    Background.Mass = MassB;
    Background.Mass_Area = MassBA;
    Background.Scattering = ScatB; 
    %% Particles calculation 
    % oznaci castice pomocoi bwlabel, nasledne pocita hmotnost kazdej
    % castice vysledky su zapisane pre kazdu casticu v premennej parts
    [LAB,num] = bwlabel(FINAL);
    parts = regionprops(LAB,'Area','Perimeter','EquivDiameter');
    [parts(:).Mass]=deal([]);           [parts(:).Sum]=deal([]); 
    [parts(:).MassArea]=deal([]);       [parts(:).Mass_Backg]=deal([]);
    [parts(:).MassArea_Backg]=deal([]); [parts(:).AreaPS]=deal([]);
    [parts(:).Rate]=deal([]);           [parts(:).AvCurrent]=deal([]);

    MA=[];
    for i=1:num

   
        AreaF = parts(i).Area*dx; %plocha v nm2
        SumaF=sum(X(LAB==i)); %  suma hodnot castice
        RateF = SumaF/(parts(i).Area*dt); %priemerny pocet dopadajucich elektronov 
        AvIF = (RateF*1.6e-7)/Ef; %priemerny prud
        MassF = (191*SumaF/D*Ef); % hmotnost s pozadim
        MassFA = MassF/AreaF; % hmotnost na plochu s pozadim 
        MassF_B = MassF - meanB*parts(i).Area; % bez pozadia
        MassFA_B = MassF_B/AreaF; % na plochu bez pozadia
        parts(i).Sum=SumaF;
        parts(i).MassFinal=MassF_B;
        parts(i).MassFinal_Area=MassFA_B;
        parts(i).Mass_Backg=MassF;
        parts(i).MassArea_Backg=MassFA;
        parts(i).AreaPS = AreaF; 
        parts(i).Rate = RateF;
        parts(i).AvCurrent = AvIF;
    end
    myGLOBAL(DET(d)).Particles = parts;
    myGLOBAL(DET(d)).Background = Background;
end

% ziskane parametre a hmotnosti pre pozadia a castice su ulozene do
% globalnej premennej myGLOBAL
