-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultithresh_fca.m
463 lines (376 loc) · 15 KB
/
multithresh_fca.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
function [thresh, metric] = multithresh_fca(varargin)
%MULTITHRESH Multi-level image thresholds using Otsu's method.
% THRESH = MULTITHRESH(A) computes a single threshold for image A using
% Otsu's method and returns it in THRESH. THRESH can be used to convert A
% into a two-level image using IMQUANTIZE.
%
% THRESH = MULTITHRESH(A, N) computes N thresholds for image A using the
% Otsu's method and returns them in THRESH. THRESH is a 1xN vector which
% can be used to convert A into an image with (N+1) discrete levels using
% IMQUANTIZE. The maximum value allowed for N is 20.
%
% [THRESH, METRIC] = MULTITHRESH(A,...) returns the effectiveness metric
% as the second output argument. METRIC is in the range [0 1] and a
% higher value indicates greater effectiveness of the thresholds in
% separating the input image into N+1 regions based on Otsu's objective
% criterion.
%
% Class Support
% -------------
% The input image A is an array of one of the following classes: uint8,
% uint16, int16, single, or double. It must be nonsparse. N is a positive
% integer between 1 and 20, and can be any numeric class. THRESH is a 1xN
% numeric vector of the same class as A. METRIC is a scalar of class
% double.
%
% Notes
% -----
% 1. A can be an array of any dimension. MULTITHRESH finds the thresholds
% based on the aggregate histogram of the entire array. An RGB image
% is considered a 3D numeric array and the thresholds are computed
% for the combined data from all the three color planes.
%
% 2. MULTITHRESH uses the range of the input A [min(A(:)) max(A(:))] as
% the limits for computing image histogram which is used in subsequent
% computations. Any NaNs in A are ignored in computation. Any Infs and
% -Infs in A are counted in the first and last bin of the histogram,
% respectively.
%
% 3. For N > 2, MULTITHRESH uses search-based optimization of Otsu's
% criterion to find the thresholds. The search-based optimization
% guarantees only locally optimal results. Since the chance of
% converging to local optimum increases with N, it is preferrable to
% use smaller values of N, typically N < 10.
%
% 4. For degenerate inputs where the number of unique values in A is less
% than or equal to N, there is no viable solution using Otsu's method.
% For such inputs, MULTITHRESH returns THRESH such that it includes
% all the unique values from A and possibly some extra values that are
% chosen arbitrarily.
%
% 5. The thresholds (THRESH) returned by MULTITHRESH are in the same
% range as the input image A. This is unlike GRAYTHRESH which returns
% a normalized threshold in the range [0, 1].
%
% Example 1
% ---------
% This example computes multiple thresholds for an image using
% MULTITHRESH and applies those thresholds to the image using IMQUANTIZE
% to get segment labels.
%
% I = imread('circlesBrightDark.png');
% imshow(I, [])
% title('Original Image');
%
% % Compute the thresholds
% thresh = multithresh(I,2);
%
% % Apply the thresholds to obtain segmented image
% seg_I = imquantize(I,thresh);
%
% % Show the various segments in the segmented image in color
% RGB = label2rgb(seg_I);
% figure, imshow(RGB)
% title('Segmented Image');
%
% See also GRAYTHRESH, IMBINARIZE, IMQUANTIZE, RGB2IND.
% Copyright 2012-2015 The MathWorks, Inc.
% Reference
% ---------
% N. Otsu, "A Threshold Selection Method from Gray-Level Histograms," IEEE
% Transactions on Systems, Man, and Cybernetics, Vol. 9, No. 1, pp. 62-66,
% 1979.
narginchk(1,2);
[A, N] = parse_inputs(varargin{:});
if (isempty(A))
warning(message('images:multithresh:degenerateInput',N))
thresh = getDegenerateThresholds(A, N);
metric = 0.0;
return;
end
% Variables are named similar to the formulae in Otsu's paper.
num_bins = 256;
[p, minA, maxA] = getpdf(A, num_bins);
if (isempty(p))
% Input image pdf could not be computed
warning(message('images:multithresh:degenerateInput',N))
thresh = getThreshForNoPdf(minA, maxA, N);
metric = 0.0;
return;
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);
if nargout > 1
% Compute the effectiveness metric
metric = maxval/(sum(p.*(((1:num_bins)' - mu_t).^2)));
end
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);
if nargout > 1
% Compute the effectiveness metric
metric = maxval/(sum(p.*(((1:num_bins)' - mu_t).^2)));
end
end
end
end
%--------------------------------------------------------------------------
function [A, N] = parse_inputs(varargin)
A = varargin{1};
validateattributes(A,{'uint8','uint16','int16','double','single'}, ...
{'nonsparse', 'real'}, mfilename,'A',1);
if (nargin == 2)
N = varargin{2};
validateattributes(N,{'numeric'},{'integer','scalar','positive','<=',20}, ...
mfilename,'N',2);
else
N = 1; % Default N
end
end
%--------------------------------------------------------------------------
function [p, minA, maxA] = getpdf(A,num_bins)
% Vectorize A for faster histogram computation
A = A(:);
if isfloat(A)
% If A is an float images then scale the data to the range [0 1] while
% taking care of special cases such as Infs and NaNs.
% Remove NaNs from consideration.
% A cannot be empty here because we checked for it earlier.
A(isnan(A)) = [];
if isempty(A)
% The case when A was full of only NaNs.
minA = NaN;
maxA = NaN;
p = [];
return;
end
% Scale A to [0-1]
idxFinite = isfinite(A);
% If there are finite elements, then scale them between [0-1]. Maintain
% Infs and -Infs as is so that they get included in the pdf.
if any(idxFinite)
minA = min(A(idxFinite));
maxA = max(A(idxFinite));
if(minA == maxA)
p = [];
return;
end
% Call to BSXFUN below is equivalent to A = (A - minA)/(maxA - minA);
A = bsxfun(@rdivide,bsxfun(@minus, A, minA),maxA - minA);
else
% One of many possibilities: all Infs, all -Infs, mixture of Infs
% and -Infs, mixture of Infs with NaNs.
minA = min(A);
maxA = max(A);
p = [];
return;
end
else
% If A is an integer image then no need to handle special cases for
% Infs and NaNs.
minA = min(A);
maxA = max(A);
if(minA == maxA)
p = [];
return;
else
% Call to BSXFUN below is equivalent to A = single(A - minA)./single(maxA - minA);
A = bsxfun(@rdivide,single(bsxfun(@minus, A, minA)),single(maxA - minA));
end
end
% Convert to uint8 for fastest histogram computation.
% A = uint8(255*A);
% A = grayto8mex(A);
counts = hist(255*A,num_bins);
p = log(max(counts,1)) / sum(log(max(counts,1)));
p=p.';
end
%--------------------------------------------------------------------------
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 presevrved
uniqueVals_d = double(uniqueVals); % Needed to convert to double for correct uniquness check
threshCandidate = max(floor(uniqueVals(1)),0); % Always non-negative, threshCandidate datatype presevrved
q = 1;
while q <= thNeeded2
threshCandidate = threshCandidate + 1;
threshCandidate_d = double(threshCandidate); % Needed to convert to double for correct uniquness 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