%close all; clear all; clc
function [Save_txt_file_as] = Particle_counter_v5_3(p,Program_path,baseFileNames)
tic; % Start the timer
format compact;


c=0;
Save_to_path = Program_path(1:end);
Save_txt_file_as = 'Results_v5.txt';
Img_Matrix = zeros(1024, 1280);
Dummy_Matrix1 = zeros(1024, 1280);
Dummy_Matrix2 = zeros(1024, 1280);

% Collect data for average image
for i=1:p;
            c=c+1;
            %display(i)
            File_name = [Program_path,'\',baseFileNames(i).name] %['B0000' num2str(i) '.tif']
            Dummy_Matrix1 = imread(File_name);
            Dummy_Matrix2 = double(Dummy_Matrix1);
            Img_Matrix = Img_Matrix + Dummy_Matrix2;
            Dummy_Matrix1 = zeros(1024, 1280);
end


% Generate average image
AVG_img = (1/p)*Img_Matrix;
% Save average image as a different format in order to be able to
% subtract it from images in next steps
AVG_img_2 = AVG_img / 255;
AVG_img_2 = im2uint8(AVG_img_2);
% Export average image so it can be used in Img_histogram and Img_analysis program
setappdata(0,'AVG_img_2',AVG_img_2);



% Loop through all images in set and deliver a histogram data using
% Img_histogram_vX.Y.m
n=0;
for i=1:p;
            n=n+1;
            display(i);
            File_name = [Program_path,'\',baseFileNames(i).name];
            [Sum_of_particles_hist_15_temp, Sum_of_particles_hist_31_temp, Sum_of_particles_hist_47_temp, Sum_of_particles_hist_63_temp, Sum_of_particles_hist_79_temp, Sum_of_particles_hist_95_temp, Sum_of_particles_hist_111_temp, Sum_of_particles_hist_127_temp,Sum_of_particles_hist_143_temp, Sum_of_particles_hist_159_temp, Sum_of_particles_hist_175_temp, Sum_of_particles_hist_191_temp, Sum_of_particles_hist_207_temp, Sum_of_particles_hist_223_temp, Sum_of_particles_hist_239_temp, Sum_of_particles_hist_250_temp] = Img_histogram_v5_3(i, File_name);
            Sum_of_particles_hist_15(n) = Sum_of_particles_hist_15_temp;
            Sum_of_particles_hist_31(n) = Sum_of_particles_hist_31_temp;
            Sum_of_particles_hist_47(n) = Sum_of_particles_hist_47_temp;
            Sum_of_particles_hist_63(n) = Sum_of_particles_hist_63_temp;
            Sum_of_particles_hist_79(n) = Sum_of_particles_hist_79_temp;
            Sum_of_particles_hist_95(n) = Sum_of_particles_hist_95_temp;
            Sum_of_particles_hist_111(n) = Sum_of_particles_hist_111_temp;
            Sum_of_particles_hist_127(n) = Sum_of_particles_hist_127_temp;
            Sum_of_particles_hist_143(n) = Sum_of_particles_hist_143_temp;
            Sum_of_particles_hist_159(n) = Sum_of_particles_hist_159_temp;
            Sum_of_particles_hist_175(n) = Sum_of_particles_hist_175_temp;
            Sum_of_particles_hist_191(n) = Sum_of_particles_hist_191_temp;
            Sum_of_particles_hist_207(n) = Sum_of_particles_hist_207_temp;
            Sum_of_particles_hist_223(n) = Sum_of_particles_hist_223_temp;
            Sum_of_particles_hist_239(n) = Sum_of_particles_hist_239_temp;
            Sum_of_particles_hist_250(n) = Sum_of_particles_hist_250_temp;
            Save_as_name{n} = File_name;
end

% Calculate 'median value' at each treshold and print to txt file
medianSum_of_particles_hist_15 = median(Sum_of_particles_hist_15,'omitnan');
medianSum_of_particles_hist_31 = median(Sum_of_particles_hist_31,'omitnan');
medianSum_of_particles_hist_47 = median(Sum_of_particles_hist_47,'omitnan');
medianSum_of_particles_hist_63 = median(Sum_of_particles_hist_63,'omitnan');
medianSum_of_particles_hist_79 = median(Sum_of_particles_hist_79,'omitnan');
medianSum_of_particles_hist_95 = median(Sum_of_particles_hist_95,'omitnan');
medianSum_of_particles_hist_111 = median(Sum_of_particles_hist_111,'omitnan');
medianSum_of_particles_hist_127 = median(Sum_of_particles_hist_127,'omitnan');
medianSum_of_particles_hist_143 = median(Sum_of_particles_hist_143,'omitnan');
medianSum_of_particles_hist_159 = median(Sum_of_particles_hist_159,'omitnan');
medianSum_of_particles_hist_175 = median(Sum_of_particles_hist_175,'omitnan');
medianSum_of_particles_hist_191 = median(Sum_of_particles_hist_191,'omitnan');
medianSum_of_particles_hist_207 = median(Sum_of_particles_hist_207,'omitnan');
medianSum_of_particles_hist_223 = median(Sum_of_particles_hist_223,'omitnan');
medianSum_of_particles_hist_239 = median(Sum_of_particles_hist_239,'omitnan');
medianSum_of_particles_hist_250 = median(Sum_of_particles_hist_250,'omitnan');

% From data above calculate how many particles are in each group
Sum_of_particles_in_group1  = medianSum_of_particles_hist_15 -  medianSum_of_particles_hist_31;
Sum_of_particles_in_group2  = medianSum_of_particles_hist_31 -  medianSum_of_particles_hist_47;
Sum_of_particles_in_group3  = medianSum_of_particles_hist_47 -  medianSum_of_particles_hist_63;
Sum_of_particles_in_group4  = medianSum_of_particles_hist_63 -  medianSum_of_particles_hist_79;
Sum_of_particles_in_group5  = medianSum_of_particles_hist_79 -  medianSum_of_particles_hist_95;
Sum_of_particles_in_group6  = medianSum_of_particles_hist_95 -  medianSum_of_particles_hist_111;
Sum_of_particles_in_group7  = medianSum_of_particles_hist_111 - medianSum_of_particles_hist_127;
Sum_of_particles_in_group8  = medianSum_of_particles_hist_127 - medianSum_of_particles_hist_143;
Sum_of_particles_in_group9  = medianSum_of_particles_hist_143 - medianSum_of_particles_hist_159;
Sum_of_particles_in_group10 = medianSum_of_particles_hist_159 - medianSum_of_particles_hist_175;
Sum_of_particles_in_group11 = medianSum_of_particles_hist_175 - medianSum_of_particles_hist_191;
Sum_of_particles_in_group12 = medianSum_of_particles_hist_191 - medianSum_of_particles_hist_207;
Sum_of_particles_in_group13 = medianSum_of_particles_hist_207 - medianSum_of_particles_hist_223;
Sum_of_particles_in_group14 = medianSum_of_particles_hist_223 - medianSum_of_particles_hist_239;
Sum_of_particles_in_group15 = medianSum_of_particles_hist_239 - medianSum_of_particles_hist_250;
Sum_of_particles_in_group16 = medianSum_of_particles_hist_250;
% Sent results to Img_analysis_vX.Y.m
setappdata(0,'Sum_of_particles_in_group1',Sum_of_particles_in_group1);
setappdata(0,'Sum_of_particles_in_group2',Sum_of_particles_in_group2);
setappdata(0,'Sum_of_particles_in_group3',Sum_of_particles_in_group3);
setappdata(0,'Sum_of_particles_in_group4',Sum_of_particles_in_group4);
setappdata(0,'Sum_of_particles_in_group5',Sum_of_particles_in_group5);
setappdata(0,'Sum_of_particles_in_group6',Sum_of_particles_in_group6);
setappdata(0,'Sum_of_particles_in_group7',Sum_of_particles_in_group7);
setappdata(0,'Sum_of_particles_in_group8',Sum_of_particles_in_group8);
setappdata(0,'Sum_of_particles_in_group9',Sum_of_particles_in_group9);
setappdata(0,'Sum_of_particles_in_group10',Sum_of_particles_in_group10);
setappdata(0,'Sum_of_particles_in_group11',Sum_of_particles_in_group11);
setappdata(0,'Sum_of_particles_in_group12',Sum_of_particles_in_group12);
setappdata(0,'Sum_of_particles_in_group13',Sum_of_particles_in_group13);
setappdata(0,'Sum_of_particles_in_group14',Sum_of_particles_in_group14);
setappdata(0,'Sum_of_particles_in_group15',Sum_of_particles_in_group15);
setappdata(0,'Sum_of_particles_in_group16',Sum_of_particles_in_group16);


% Loop through the images once again and analyze them using
% Img_analysis_vX.Y.m
n=0;
for i=1:p;
            n=n+1;
            display(i);
            File_name = [Program_path,'\',baseFileNames(i).name];
            [Sum_of_particles_temp, Mean_brightness_temp, SNR_temp, Avg_partcl_diameter_in_mikrons_temp] = Img_analysis_v5_3(i, File_name);
            Sum_of_particles(n) = Sum_of_particles_temp;
            Save_as_name{n} = File_name;
            Mean_brightness(n) = Mean_brightness_temp;
            SNR(n) = SNR_temp;
            %MostFrequent(n) = MostFrequent_temp;
            Avg_partcl_diameter_in_mikrons(n) = Avg_partcl_diameter_in_mikrons_temp;
            %Saturated(n) = Saturated_pixels_temp;
end


% Stop the timer
wtime = toc;

% Print results of 'Particle_counter.m' to txt file
t = datetime('now','TimeZone','local','Format','d-MMM-y HH:mm');
DateString = datestr(t);
fid = fopen([Save_txt_file_as] ,'a+');       % a+ will add new results into the existing Results.txt file
fprintf(fid, '%s\r', Program_path);
fprintf(fid, 'Datestamp %s\r', DateString');
fprintf(fid, 'Elapsed time = %.0f seconds\r', wtime );
fprintf(fid, 'FilterIndex = 15 \r\r');
fprintf(fid,  'VARIABLES = "Name", "Amount of particles", "Mean brightness", "SNR", "Avg particle diameter in mikrons^2"\r');
%fprintf(fid, ['ZONE T = "A1" \r']);
        % Prints values for each picture
        % for m=1:n
        %     fprintf(fid, '%s, %.0f, %.2f, %.5f, %.2f,\r', Save_as_name{m}, Sum_of_particles(m), Mean_brightness(m), SNR(m), Avg_partcl_diameter_in_mikrons(m));
        % end



% Compute 'median value' and print to txt file
medianSum_of_particles = median(Sum_of_particles,'omitnan');
medianMean_brightness = median(Mean_brightness,'omitnan');
medianSNR = median(SNR,'omitnan');
medianAvg_partcl_diameter_in_mikrons = median(Avg_partcl_diameter_in_mikrons,'omitnan');

fprintf(fid, 'Median values\r');
fprintf(fid, 'Median, %.0f, %.2f, %.5f, %.2f\r', medianSum_of_particles, medianMean_brightness, medianSNR, medianAvg_partcl_diameter_in_mikrons);

% Compute 'standard deviation' and print to txt file
stddevSum_of_particles = std(Sum_of_particles,'omitnan');
stddevMean_brightness = std(Mean_brightness,'omitnan');
stddevSNR = std(SNR,'omitnan');
stddevAvg_partcl_diameter_in_mikrons = std(Avg_partcl_diameter_in_mikrons,'omitnan');
fprintf(fid, 'Standard deviation\r');
fprintf(fid, 'Stddev, %.0f, %.2f, %.5f, %.2f\r', stddevSum_of_particles, stddevMean_brightness, stddevSNR, stddevAvg_partcl_diameter_in_mikrons);

% Print results of 'Img_histogram.m' to txt file
fprintf(fid, 'Histogram tresholds\r');
fprintf(fid, 'Tresholds, 15, 31, 47, 63, 79, 95, 111, 127, 143, 159, 175, 195, 215, 235, 250\r'); %15,31,47,63,79,95,111,127,143,159,175,195,215,235,250
% Prints values for each picture
% for m=1:n
%     fprintf(fid, '%s, %.0f, %.0f, %.0f, %.0f, %.0f, %.0f, %.0f, %.0f,\r', Save_as_name{m}, Sum_of_particles_hist_15(m), Sum_of_particles_hist_50(m), Sum_of_particles_hist_85(m), Sum_of_particles_hist_120(m), Sum_of_particles_hist_155(m), Sum_of_particles_hist_190(m), Sum_of_particles_hist_225(m), Sum_of_particles_hist_250(m));
% end

fprintf(fid, 'Histogram data\r');
fprintf(fid, 'Histogram, %.0f, %.0f, %.0f, %.0f, %.0f, %.0f, %.0f, %.0f, %.0f, %.0f, %.0f, %.0f, %.0f, %.0f, %.0f, %.0f\r', Sum_of_particles_in_group1, Sum_of_particles_in_group2, Sum_of_particles_in_group3, Sum_of_particles_in_group4, Sum_of_particles_in_group5, Sum_of_particles_in_group6, Sum_of_particles_in_group7, Sum_of_particles_in_group8, Sum_of_particles_in_group9, Sum_of_particles_in_group10, Sum_of_particles_in_group11, Sum_of_particles_in_group12, Sum_of_particles_in_group13, Sum_of_particles_in_group14, Sum_of_particles_in_group15, Sum_of_particles_in_group16);
fprintf(fid, 'Mean particle brightness =, %.2f\r\r\r\r\r', medianMean_brightness);

% Close printed txt file
fclose(fid);
display('DONE - STARTER PROGRAM end');