-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGetImageStats.m
206 lines (177 loc) · 6.48 KB
/
GetImageStats.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
function GetImageStats(metadata)
% GetImageStats(metadata)
% Set the values of metadata.voxelStats, and metadata.projections, and if
% appropriate, reload saved value of metadata.origin
% INPUT:
% -metadata: MetadataObj
version = '1.0.3';
if nargin ~= 1
help GetImageStats
error('Invalid number of inputs');
end
stackPath = metadata.stackPath;
stackSaveName = metadata.stackSaveName;
saveFileName = sprintf('%s%s_ImageStats.mat', stackPath, stackSaveName);
%Check to see if the information we need is saved:
calcStats = false;
saveImageStats = false;
origin = metadata.origin;
if FileExists(saveFileName)
%load imageStats
load(saveFileName);
if exist('ImageStats', 'var')
% older versions used "ImageStats" instead of "imageStats", but the
% information is compatible, so no need to re-calculate
imageStats = convertOld(ImageStats, metadata);
clear ImageStats
% save in the newer form
saveImageStats = true;
end
if exist('imageStats', 'var')
origin = imageStats.origin;
if thisVersionNewer(version, imageStats.version)
%If we have a new version of GetImageStats, re-do calculations
fprintf(['Recalculating image stats because GetImageStats ', ...
'version changed from %s to %s.\n'], ...
imageStats.version, version)
calcStats = true;
end
elseif exist('VoxelHist', 'var')
% This is to provide compatability with VERY old versions of
% GetImageStats.m It should be removed soon, because it really only
% adds bloat and confusion.
origin = VoxelHist.Origin;
origin(3) = -origin(3); %The z dimension has been reversed
fprintf(['Recalculating image stats because old stack pre-dates ', ...
'version numbers.\n'])
calcStats = true;
else
fprintf(['Recalculating image stats because old stack pre-dates ', ...
'version numbers.\n'])
calcStats = true;
end
else
calcStats = true;
end
if calcStats
imageStats = calcImageStats(metadata, origin);
imageStats.version = version;
saveImageStats = ~strcmp(metadata.stackType, 'lei_mat');
end
if saveImageStats
save(saveFileName, 'imageStats');
end
metadata.origin = imageStats.origin;
metadata.color = imageStats.color;
metadata.voxelStats = imageStats.voxelStats;
metadata.projections = imageStats.projections;
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function imageStats = convertOld(ImageStats, metadata)
imageStats.origin = ImageStats.Origin;
imageStats.voxelStats.hist = ImageStats.Voxels.Hist;
imageStats.voxelStats.blackLevel = ImageStats.Voxels.BlackLevel;
imageStats.voxelStats.whiteLevel = ImageStats.Voxels.WhiteLevel;
imageStats.voxelStats.noiseLevel = ImageStats.NoiseThresh;
imageStats.voxelStats.adjustScale = imageStats.voxelStats.whiteLevel / ...
(imageStats.voxelStats.whiteLevel - imageStats.voxelStats.blackLevel);
projections.x.image = ImageStats.Projections.X;
projections.y.image = ImageStats.Projections.Y;
projections.z.image = ImageStats.Projections.Z;
projections.x.xPhysical = [0, metadata.physical(2)];
projections.x.yPhysical = [0, metadata.physical(3)];
projections.y.xPhysical = [0, metadata.physical(1)];
projections.y.yPhysical = [0, metadata.physical(3)];
projections.z.xPhysical = [0, metadata.physical(1)];
projections.z.yPhysical = [0, metadata.physical(2)];
imageStats.projections.max = projections;
imageStats.color = GetChannelColor(metadata);
imageStats.version = ImageStats.Version;
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function newer = thisVersionNewer(thisVersion, savedVersion)
if strcmp(thisVersion, savedVersion)
newer = false;
return
end
while ~(isempty(thisVersion) && isempty(savedVersion))
[thisPart, thisVersion] = strtok(thisVersion, '.'); %#ok<STTOK>
[savedPart, savedVersion] = strtok(savedVersion, '.'); %#ok<STTOK>
if isempty(thisPart)
thisNum = 0;
else
thisNum = str2double(thisPart);
end
if isempty(savedPart)
savedNum = 0;
else
savedNum = str2double(savedPart);
end
if thisNum > savedNum
newer = true;
return;
elseif thisNum < savedNum
newer = false;
return;
end
end
newer = false;
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function imageStats = calcImageStats(metadata, origin)
% create a stack object so we can get image slices from the stack
stack = StackObj(metadata, true);
logical = stack.metadata.logical;
numZ = logical(3);
intTypeName = stack.metadata.intTypeName;
% Allocate space. Don't need to allocate projections.z
projections.x.image = zeros(logical(3), logical(2), intTypeName);
projections.y.image = zeros(logical(3), logical(1), intTypeName);
% get the first slice
tempImage = stack.getSliceZ(1);
% calculate projections and some voxel histogram info
projections.x.image(1,:) = squeeze(max(tempImage, [], 2))';
projections.y.image(1,:) = squeeze(max(tempImage, [], 1));
projections.z.image = tempImage;
bins = 0:(stack.metadata.numVoxelLevels - 1);
voxelStats.hist = hist(tempImage(:), bins);
xImage = projections.x.image;
yImage = projections.y.image;
zImage = projections.z.image;
vHist = voxelStats.hist;
getZ = @stack.getSliceZ;
parBlock = ParallelBlock();
progStr = ['Calculating image statistics for ', metadata.stackSaveName];
ProgressBar(progStr, numZ - 1, 5, true)
parfor z = 2:numZ
tempImage = feval(getZ, z);
xImage(z,:) = squeeze(max(tempImage, [], 2))';
yImage(z,:) = squeeze(max(tempImage, [], 1));
zImage = max(zImage, tempImage);
vHist = vHist + hist(tempImage(:), bins);
ProgressBar(progStr)
end
parBlock.endBlock();
projections.x.image = xImage;
projections.y.image = yImage;
projections.z.image = zImage;
voxelStats.hist = vHist;
clear xImage yImage zImage vHist stack
% include data on the true physical size of these projection images
projections.x.xPhysical = [0, metadata.physical(2)];
projections.x.yPhysical = [0, metadata.physical(3)];
projections.y.xPhysical = [0, metadata.physical(1)];
projections.y.yPhysical = [0, metadata.physical(3)];
projections.z.xPhysical = [0, metadata.physical(1)];
projections.z.yPhysical = [0, metadata.physical(2)];
lowEnd = round(0.2 * bins(end));
[~, voxelStats.blackLevel] = max(voxelStats.hist(1:lowEnd));
voxelStats.whiteLevel = find(voxelStats.hist > 0, 1, 'last');
voxelStats.adjustScale = voxelStats.whiteLevel / ...
(voxelStats.whiteLevel - voxelStats.blackLevel);
voxelStats.noiseLevel = -1;
imageStats.origin = origin;
imageStats.projections.max = projections;
imageStats.voxelStats = voxelStats;
imageStats.color = GetChannelColor(metadata);
return