-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiTH_extract.m
277 lines (210 loc) · 8.79 KB
/
multiTH_extract.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
function [thresh_h, metric_h] = multiTH_extract(histoCounts, N, histoStructure)
%Marco A., following multithresh()
num_bins = size(histoCounts, 2);
p = histoCounts/sum(histoCounts);
p = p';
minA = histoStructure(1);
maxA = histoStructure(end);
omega = cumsum(p);
mu = cumsum(p .* (1:num_bins)');
mu_t = mu(end);
if (N < 3)
sigma_b_squared = calcFullObjCriteriaMatrix(N, num_bins, omega, mu, mu_t);
% Find the location of the maximum value of sigma_b_squared.
maxval = max(sigma_b_squared(:));
isvalid_maxval = isfinite(maxval);
if isvalid_maxval
% Find the bin with maximum value. If the maximum extends over
% several bins, average together the locations.
switch N
case 1
idx = find(sigma_b_squared == maxval);
% Find the intensity associated with the bin
thresh = mean(idx) - 1;
case 2
[maxR, maxC] = find(sigma_b_squared == maxval);
% Find the intensity associated with the bin
thresh = mean([maxR maxC],1) - 1;
end
else
[isDegenerate, uniqueVals] = checkForDegenerateInput(A, N);
if isDegenerate
warning(message('images:multithresh:degenerateInput',N));
else
warning(message('images:multithresh:noConvergence'));
end
thresh = getDegenerateThresholds(uniqueVals, N);
metric = 0.0;
end
else
% For N >= 3, use search-based optimization of Otsu's objective function
% Set initial thresholds as uniformly spaced
initial_thresh = linspace(0, num_bins-1, N+2);
initial_thresh = initial_thresh(2:end-1); % Retain N thresholds
% Set optimization parameters
options = optimset('TolX',1,'Display','off');
% Find optimum using fminsearch
[thresh, minval] = fminsearch(@(thresh) objCriteriaND(thresh, ...
num_bins, omega, mu, mu_t), initial_thresh, options);
maxval = -minval;
isvalid_maxval = ~(isinf(maxval) || isnan(maxval));
if isvalid_maxval
thresh = round(thresh);
end
end
% Prepare output values
if isvalid_maxval
% Map back to original scale as input A
thresh = map2OriginalScale(thresh, minA, maxA);
% Compute the effectiveness metric
metric = maxval/(sum(p.*(((1:num_bins)' - mu_t).^2)));
else
[isDegenerate, uniqueVals] = checkForDegenerateInput(A, N);
if isDegenerate
warning(message('images:multithresh:degenerateInput',N));
thresh = getDegenerateThresholds(uniqueVals, N);
metric = 0.0;
else
warning(message('images:multithresh:noConvergence'));
% Return latest available solution
thresh = map2OriginalScale(thresh, minA, maxA);
% Compute the effectiveness metric
metric = maxval/(sum(p.*(((1:num_bins)' - mu_t).^2)));
end
end
thresh_h = thresh;
metric_h = metric;
%--------------------------------------------------------------------------
function sigma_b_squared_val = objCriteriaND(thresh, num_bins, omega, mu, mu_t)
% 'thresh' has intensities [0-255], but 'boundaries' are the indices [1
% 256].
boundaries = round(thresh)+1;
% Constrain 'boundaries' to:
% 1. be strictly increasing,
% 2. have the lowest value > 1 (i.e. minimum 2),
% 3. have highest value < num_bins (i.e. maximum num_bins-1).
if (~all(diff([1 boundaries num_bins]) > 0))
sigma_b_squared_val = Inf;
return;
end
boundaries = [boundaries num_bins];
sigma_b_squared_val = omega(boundaries(1)).*((mu(boundaries(1))./omega(boundaries(1)) - mu_t).^2);
for kk = 2:length(boundaries)
omegaKK = omega(boundaries(kk)) - omega(boundaries(kk-1));
muKK = (mu(boundaries(kk)) - mu(boundaries(kk-1)))/omegaKK;
sigma_b_squared_val = sigma_b_squared_val + (omegaKK.*((muKK - mu_t).^2)); % Eqn. 14 in Otsu's paper
end
if (isfinite(sigma_b_squared_val))
sigma_b_squared_val = -sigma_b_squared_val; % To do maximization using fminsearch.
else
sigma_b_squared_val = Inf;
end
end
%--------------------------------------------------------------------------
function sigma_b_squared = calcFullObjCriteriaMatrix(N, num_bins, omega, mu, mu_t)
if (N == 1)
sigma_b_squared = (mu_t * omega - mu).^2 ./ (omega .* (1 - omega));
elseif (N == 2)
% Rows represent thresh(1) (lower threshold) and columns represent
% thresh(2) (higher threshold).
omega0 = repmat(omega,1,num_bins);
mu_0_t = repmat(bsxfun(@minus,mu_t,mu./omega),1,num_bins);
omega1 = bsxfun(@minus, omega.', omega);
mu_1_t = bsxfun(@minus,mu_t,(bsxfun(@minus, mu.', mu))./omega1);
% Set entries corresponding to non-viable solutions to NaN
[allPixR, allPixC] = ndgrid(1:num_bins,1:num_bins);
pixNaN = allPixR >= allPixC; % Enforce thresh(1) < thresh(2)
omega0(pixNaN) = NaN;
omega1(pixNaN) = NaN;
term1 = omega0.*(mu_0_t.^2);
term2 = omega1.*(mu_1_t.^2);
omega2 = 1 - (omega0+omega1);
omega2(omega2 <= 0) = NaN; % Avoid divide-by-zero Infs in term3
term3 = ((omega0.*mu_0_t + omega1.*mu_1_t ).^2)./omega2;
sigma_b_squared = term1 + term2 + term3;
end
end
%--------------------------------------------------------------------------
function sclThresh = map2OriginalScale(thresh, minA, maxA)
normFactor = 255;
sclThresh = double(minA) + thresh/normFactor*(double(maxA) - double(minA));
sclThresh = cast(sclThresh,'like',minA);
end
%--------------------------------------------------------------------------
function [isDegenerate, uniqueVals] = checkForDegenerateInput(A, N)
uniqueVals = unique(A(:))'; % Note: 'uniqueVals' is returned in sorted order.
% Ignore NaNs because they are ignored in computation. Ignore Infs because
% Infs are mapped to extreme bins during histogram computation and are
% therefore not unique values.
uniqueVals(isinf(uniqueVals) | isnan(uniqueVals)) = [];
isDegenerate = (numel(uniqueVals) <= N);
end
%--------------------------------------------------------------------------
function thresh = getThreshForNoPdf(minA, maxA, N)
if isnan(minA)
% If minA = NaN => maxA = NaN. All NaN input condition.
minA = 1;
maxA = 1;
end
if (N == 1)
thresh = minA;
else
if (minA == maxA)
% Flat image, i.e. only one unique value (not counting Infs and
% -Infs) exists
thresh = getDegenerateThresholds(minA, N);
else
% Only scenario: A full of Infs and -Infs => minA = -Inf and maxA =
% Inf
thresh = getDegenerateThresholds([minA maxA], N);
end
end
end
%--------------------------------------------------------------------------
function thresh = getDegenerateThresholds(uniqueVals, N)
% Notes:
% 1) 'uniqueVals' must be in sorted (ascending) order
% 2) For predictable behavior, 'uniqueVals' should not have NaNs
% 3) For predictable behavior for all datatypes including uint8, N must be < 255
if isempty(uniqueVals)
thresh = cast(1:N,'like', uniqueVals);
return;
end
% 'thresh' will always have all the elements of 'uniqueVals' in it.
thresh = uniqueVals;
thNeeded1 = N - numel(thresh);
if (thNeeded1 > 0)
% More values are needed to fill 'thresh'. Start filling 'thresh' from
% the lower end starting with 1.
if (uniqueVals(1) > 1)
% If uniqueVals(1) > 1, we can directly fill some (or maybe all)
% values starting from 1, without checking for uniqueness.
thresh = [cast(1:min(thNeeded1,ceil(uniqueVals(1))-1), 'like', uniqueVals)...
thresh];
end
thNeeded2 = N - numel(thresh);
if (thNeeded2 > 0)
% More values are needed to fill 'thresh'. Use positive integer
% values, as small as possible, which are not in 'thresh' already.
lenThreshOrig = length(thresh);
thresh = [thresh zeros(1,thNeeded2)]; % Create empty entries, thresh datatype preserved
uniqueVals_d = double(uniqueVals); % Needed to convert to double for correct uniqueness check
threshCandidate = max(floor(uniqueVals(1)),0); % Always non-negative, threshCandidate datatype preserved
q = 1;
while q <= thNeeded2
threshCandidate = threshCandidate + 1;
threshCandidate_d = double(threshCandidate); % Needed to convert to double for correct uniqueness check
if any(abs(uniqueVals_d - threshCandidate_d) ...
< eps(threshCandidate_d))
% The candidate value already exists, so don't use it.
continue;
else
thresh(lenThreshOrig + q) = threshCandidate; % Append at the end
q = q + 1;
end
end
thresh = sort(thresh); % Arrange 'thresh' in ascending order
end
end
end
end