-
Notifications
You must be signed in to change notification settings - Fork 1
/
setupPDF.m
69 lines (56 loc) · 2.3 KB
/
setupPDF.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
function [min_pix, max_pix, mean_i, sigma_square] =...
setupPDF(Tagged,result_file, isUpdate)
% This function estimates the main parameters of the intensity of image data amongst all the training data.
% These parameters include minimum and maximum values, and mean and standard deviation.
% PDF = probability density function
% Inputs:
% Tagged – list of files from training data;
% result_file – name of .mat file to store computed results for further use;
% isUpdate – if this value is set to false, then an existing file will be loaded;
% if the isUpdate value is true, then new recomputations will be performed and saved.
% Outputs:
% min_pix, max_pix – minimum and maximum intensity values in the training data;
% mean_i – mean value of intensity;
% sigma_square – standard deviation of intensity.
if (nargin < 3)
isUpdate = true;
end
if (exist(result_file,'file') && isUpdate)
delete(result_file);
else
if(exist(result_file,'file'))
load(result_file);
return;
end
end
min_pix = inf; max_pix = 0;
N = 0;
S = 0; S2 = 0;
for i = 1:length(Tagged)
display(sprintf('PDF settings processing file %d of %d: ',i,length(Tagged)));
% path to the original file with image data
filename = [Tagged(i).path Tagged(i).name '.img'];
data = readImgFile(filename);
% compute sum of pixels
S = S + sum(data(:));
% compute sum of squared pixels
S2 = S2 + sum(data(:).^2);
% compute total number of processed pixels
N = N + numel(data);
% estimate minimum and maximum value
min_level = min(data(:));
max_level = max(data(:));
min_pix = min(min_level,min_pix);
max_pix = max(max_level,max_pix);
end
% compute mean based on sum and count
mean_i = S/N;
% compute standard deviation: sum(x-m)^2/N = m^2 - 2*m*sum(X)/N +
% sum(x*x)/N
sigma_square = mean_i^2 + S2/N - 2*mean_i/N * S;
% save computed data in a .mat file
save(result_file,'min_pix','-mat');
save(result_file,'max_pix','-append','-mat');
save(result_file,'sigma_square','-append','-mat');
save(result_file,'mean_i','-append','-mat');
end