/
thresholding.py
1339 lines (1122 loc) · 46.8 KB
/
thresholding.py
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import inspect
import itertools
import math
from collections import OrderedDict
from collections.abc import Iterable
import numpy as np
from scipy import ndimage as ndi
from .._shared.filters import gaussian
from .._shared.utils import _supported_float_type, warn
from .._shared.version_requirements import require
from ..exposure import histogram
from ..filters._multiotsu import (
_get_multiotsu_thresh_indices,
_get_multiotsu_thresh_indices_lut,
)
from ..transform import integral_image
from ..util import dtype_limits
from ._sparse import _correlate_sparse, _validate_window_size
__all__ = [
'try_all_threshold',
'threshold_otsu',
'threshold_yen',
'threshold_isodata',
'threshold_li',
'threshold_local',
'threshold_minimum',
'threshold_mean',
'threshold_niblack',
'threshold_sauvola',
'threshold_triangle',
'apply_hysteresis_threshold',
'threshold_multiotsu',
]
def _try_all(image, methods=None, figsize=None, num_cols=2, verbose=True):
"""Returns a figure comparing the outputs of different methods.
Parameters
----------
image : (M, N) ndarray
Input image.
methods : dict, optional
Names and associated functions.
Functions must take and return an image.
figsize : tuple, optional
Figure size (in inches).
num_cols : int, optional
Number of columns.
verbose : bool, optional
Print function name for each method.
Returns
-------
fig, ax : tuple
Matplotlib figure and axes.
"""
from matplotlib import pyplot as plt
# Compute the image histogram for better performances
nbins = 256 # Default in threshold functions
hist = histogram(image.reshape(-1), nbins, source_range='image')
# Handle default value
methods = methods or {}
num_rows = math.ceil((len(methods) + 1.0) / num_cols)
fig, ax = plt.subplots(
num_rows, num_cols, figsize=figsize, sharex=True, sharey=True
)
ax = ax.reshape(-1)
ax[0].imshow(image, cmap=plt.cm.gray)
ax[0].set_title('Original')
i = 1
for name, func in methods.items():
# Use precomputed histogram for supporting functions
sig = inspect.signature(func)
_kwargs = dict(hist=hist) if 'hist' in sig.parameters else {}
ax[i].set_title(name)
try:
ax[i].imshow(func(image, **_kwargs), cmap=plt.cm.gray)
except Exception as e:
ax[i].text(
0.5,
0.5,
f"{type(e).__name__}",
ha="center",
va="center",
transform=ax[i].transAxes,
)
i += 1
if verbose:
print(func.__orifunc__)
for a in ax:
a.axis('off')
fig.tight_layout()
return fig, ax
@require("matplotlib", ">=3.3")
def try_all_threshold(image, figsize=(8, 5), verbose=True):
"""Returns a figure comparing the outputs of different thresholding methods.
Parameters
----------
image : (M, N) ndarray
Input image.
figsize : tuple, optional
Figure size (in inches).
verbose : bool, optional
Print function name for each method.
Returns
-------
fig, ax : tuple
Matplotlib figure and axes.
Notes
-----
The following algorithms are used:
* isodata
* li
* mean
* minimum
* otsu
* triangle
* yen
Examples
--------
.. testsetup::
>>> import pytest; _ = pytest.importorskip('matplotlib')
>>> from skimage.data import text
>>> fig, ax = try_all_threshold(text(), figsize=(10, 6), verbose=False)
"""
def thresh(func):
"""
A wrapper function to return a thresholded image.
"""
def wrapper(im):
return im > func(im)
try:
wrapper.__orifunc__ = func.__orifunc__
except AttributeError:
wrapper.__orifunc__ = func.__module__ + '.' + func.__name__
return wrapper
# Global algorithms.
methods = OrderedDict(
{
'Isodata': thresh(threshold_isodata),
'Li': thresh(threshold_li),
'Mean': thresh(threshold_mean),
'Minimum': thresh(threshold_minimum),
'Otsu': thresh(threshold_otsu),
'Triangle': thresh(threshold_triangle),
'Yen': thresh(threshold_yen),
}
)
return _try_all(image, figsize=figsize, methods=methods, verbose=verbose)
def threshold_local(
image, block_size=3, method='gaussian', offset=0, mode='reflect', param=None, cval=0
):
"""Compute a threshold mask image based on local pixel neighborhood.
Also known as adaptive or dynamic thresholding. The threshold value is
the weighted mean for the local neighborhood of a pixel subtracted by a
constant. Alternatively the threshold can be determined dynamically by a
given function, using the 'generic' method.
Parameters
----------
image : (M, N[, ...]) ndarray
Grayscale input image.
block_size : int or sequence of int
Odd size of pixel neighborhood which is used to calculate the
threshold value (e.g. 3, 5, 7, ..., 21, ...).
method : {'generic', 'gaussian', 'mean', 'median'}, optional
Method used to determine adaptive threshold for local neighborhood in
weighted mean image.
* 'generic': use custom function (see ``param`` parameter)
* 'gaussian': apply gaussian filter (see ``param`` parameter for custom\
sigma value)
* 'mean': apply arithmetic mean filter
* 'median': apply median rank filter
By default, the 'gaussian' method is used.
offset : float, optional
Constant subtracted from weighted mean of neighborhood to calculate
the local threshold value. Default offset is 0.
mode : {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}, optional
The mode parameter determines how the array borders are handled, where
cval is the value when mode is equal to 'constant'.
Default is 'reflect'.
param : {int, function}, optional
Either specify sigma for 'gaussian' method or function object for
'generic' method. This functions takes the flat array of local
neighborhood as a single argument and returns the calculated
threshold for the centre pixel.
cval : float, optional
Value to fill past edges of input if mode is 'constant'.
Returns
-------
threshold : (M, N[, ...]) ndarray
Threshold image. All pixels in the input image higher than the
corresponding pixel in the threshold image are considered foreground.
References
----------
.. [1] Gonzalez, R. C. and Wood, R. E. "Digital Image Processing
(2nd Edition)." Prentice-Hall Inc., 2002: 600--612.
ISBN: 0-201-18075-8
Examples
--------
>>> from skimage.data import camera
>>> image = camera()[:50, :50]
>>> binary_image1 = image > threshold_local(image, 15, 'mean')
>>> func = lambda arr: arr.mean()
>>> binary_image2 = image > threshold_local(image, 15, 'generic',
... param=func)
"""
if np.isscalar(block_size):
block_size = (block_size,) * image.ndim
elif len(block_size) != image.ndim:
raise ValueError("len(block_size) must equal image.ndim.")
block_size = tuple(block_size)
if any(b % 2 == 0 for b in block_size):
raise ValueError(
f'block_size must be odd! Given block_size '
f'{block_size} contains even values.'
)
float_dtype = _supported_float_type(image.dtype)
image = image.astype(float_dtype, copy=False)
thresh_image = np.zeros(image.shape, dtype=float_dtype)
if method == 'generic':
ndi.generic_filter(
image, param, block_size, output=thresh_image, mode=mode, cval=cval
)
elif method == 'gaussian':
if param is None:
# automatically determine sigma which covers > 99% of distribution
sigma = tuple([(b - 1) / 6.0 for b in block_size])
else:
sigma = param
gaussian(image, sigma=sigma, out=thresh_image, mode=mode, cval=cval)
elif method == 'mean':
ndi.uniform_filter(image, block_size, output=thresh_image, mode=mode, cval=cval)
elif method == 'median':
ndi.median_filter(image, block_size, output=thresh_image, mode=mode, cval=cval)
else:
raise ValueError(
"Invalid method specified. Please use `generic`, "
"`gaussian`, `mean`, or `median`."
)
return thresh_image - offset
def _validate_image_histogram(image, hist, nbins=None, normalize=False):
"""Ensure that either image or hist were given, return valid histogram.
If hist is given, image is ignored.
Parameters
----------
image : array or None
Grayscale image.
hist : array, 2-tuple of array, or None
Histogram, either a 1D counts array, or an array of counts together
with an array of bin centers.
nbins : int, optional
The number of bins with which to compute the histogram, if `hist` is
None.
normalize : bool
If hist is not given, it will be computed by this function. This
parameter determines whether the computed histogram is normalized
(i.e. entries sum up to 1) or not.
Returns
-------
counts : 1D array of float
Each element is the number of pixels falling in each intensity bin.
bin_centers : 1D array
Each element is the value corresponding to the center of each intensity
bin.
Raises
------
ValueError : if image and hist are both None
"""
if image is None and hist is None:
raise Exception("Either image or hist must be provided.")
if hist is not None:
if isinstance(hist, tuple | list):
counts, bin_centers = hist
else:
counts = hist
bin_centers = np.arange(counts.size)
if counts[0] == 0 or counts[-1] == 0:
# Trim histogram from both ends by removing starting and
# ending zeroes as in histogram(..., source_range="image")
cond = counts > 0
start = np.argmax(cond)
end = cond.size - np.argmax(cond[::-1])
counts, bin_centers = counts[start:end], bin_centers[start:end]
else:
counts, bin_centers = histogram(
image.reshape(-1), nbins, source_range='image', normalize=normalize
)
return counts.astype('float32', copy=False), bin_centers
def threshold_otsu(image=None, nbins=256, *, hist=None):
"""Return threshold value based on Otsu's method.
Either image or hist must be provided. If hist is provided, the actual
histogram of the image is ignored.
Parameters
----------
image : (M, N[, ...]) ndarray, optional
Grayscale input image.
nbins : int, optional
Number of bins used to calculate histogram. This value is ignored for
integer arrays.
hist : array, or 2-tuple of arrays, optional
Histogram from which to determine the threshold, and optionally a
corresponding array of bin center intensities. If no hist provided,
this function will compute it from the image.
Returns
-------
threshold : float
Upper threshold value. All pixels with an intensity higher than
this value are assumed to be foreground.
References
----------
.. [1] Wikipedia, https://en.wikipedia.org/wiki/Otsu's_Method
Examples
--------
>>> from skimage.data import camera
>>> image = camera()
>>> thresh = threshold_otsu(image)
>>> binary = image <= thresh
Notes
-----
The input image must be grayscale.
"""
if image is not None and image.ndim > 2 and image.shape[-1] in (3, 4):
warn(
f'threshold_otsu is expected to work correctly only for '
f'grayscale images; image shape {image.shape} looks like '
f'that of an RGB image.'
)
# Check if the image has more than one intensity value; if not, return that
# value
if image is not None:
first_pixel = image.reshape(-1)[0]
if np.all(image == first_pixel):
return first_pixel
counts, bin_centers = _validate_image_histogram(image, hist, nbins)
# class probabilities for all possible thresholds
weight1 = np.cumsum(counts)
weight2 = np.cumsum(counts[::-1])[::-1]
# class means for all possible thresholds
mean1 = np.cumsum(counts * bin_centers) / weight1
mean2 = (np.cumsum((counts * bin_centers)[::-1]) / weight2[::-1])[::-1]
# Clip ends to align class 1 and class 2 variables:
# The last value of ``weight1``/``mean1`` should pair with zero values in
# ``weight2``/``mean2``, which do not exist.
variance12 = weight1[:-1] * weight2[1:] * (mean1[:-1] - mean2[1:]) ** 2
idx = np.argmax(variance12)
threshold = bin_centers[idx]
return threshold
def threshold_yen(image=None, nbins=256, *, hist=None):
"""Return threshold value based on Yen's method.
Either image or hist must be provided. In case hist is given, the actual
histogram of the image is ignored.
Parameters
----------
image : (M, N[, ...]) ndarray
Grayscale input image.
nbins : int, optional
Number of bins used to calculate histogram. This value is ignored for
integer arrays.
hist : array, or 2-tuple of arrays, optional
Histogram from which to determine the threshold, and optionally a
corresponding array of bin center intensities.
An alternative use of this function is to pass it only hist.
Returns
-------
threshold : float
Upper threshold value. All pixels with an intensity higher than
this value are assumed to be foreground.
References
----------
.. [1] Yen J.C., Chang F.J., and Chang S. (1995) "A New Criterion
for Automatic Multilevel Thresholding" IEEE Trans. on Image
Processing, 4(3): 370-378. :DOI:`10.1109/83.366472`
.. [2] Sezgin M. and Sankur B. (2004) "Survey over Image Thresholding
Techniques and Quantitative Performance Evaluation" Journal of
Electronic Imaging, 13(1): 146-165, :DOI:`10.1117/1.1631315`
http://www.busim.ee.boun.edu.tr/~sankur/SankurFolder/Threshold_survey.pdf
.. [3] ImageJ AutoThresholder code, http://fiji.sc/wiki/index.php/Auto_Threshold
Examples
--------
>>> from skimage.data import camera
>>> image = camera()
>>> thresh = threshold_yen(image)
>>> binary = image <= thresh
"""
counts, bin_centers = _validate_image_histogram(image, hist, nbins)
# On blank images (e.g. filled with 0) with int dtype, `histogram()`
# returns ``bin_centers`` containing only one value. Speed up with it.
if bin_centers.size == 1:
return bin_centers[0]
# Calculate probability mass function
pmf = counts.astype('float32', copy=False) / counts.sum()
P1 = np.cumsum(pmf) # Cumulative normalized histogram
P1_sq = np.cumsum(pmf**2)
# Get cumsum calculated from end of squared array:
P2_sq = np.cumsum(pmf[::-1] ** 2)[::-1]
# P2_sq indexes is shifted +1. I assume, with P1[:-1] it's help avoid
# '-inf' in crit. ImageJ Yen implementation replaces those values by zero.
crit = np.log(((P1_sq[:-1] * P2_sq[1:]) ** -1) * (P1[:-1] * (1.0 - P1[:-1])) ** 2)
return bin_centers[crit.argmax()]
def threshold_isodata(image=None, nbins=256, return_all=False, *, hist=None):
"""Return threshold value(s) based on ISODATA method.
Histogram-based threshold, known as Ridler-Calvard method or inter-means.
Threshold values returned satisfy the following equality::
threshold = (image[image <= threshold].mean() +
image[image > threshold].mean()) / 2.0
That is, returned thresholds are intensities that separate the image into
two groups of pixels, where the threshold intensity is midway between the
mean intensities of these groups.
For integer images, the above equality holds to within one; for floating-
point images, the equality holds to within the histogram bin-width.
Either image or hist must be provided. In case hist is given, the actual
histogram of the image is ignored.
Parameters
----------
image : (M, N[, ...]) ndarray
Grayscale input image.
nbins : int, optional
Number of bins used to calculate histogram. This value is ignored for
integer arrays.
return_all : bool, optional
If False (default), return only the lowest threshold that satisfies
the above equality. If True, return all valid thresholds.
hist : array, or 2-tuple of arrays, optional
Histogram to determine the threshold from and a corresponding array
of bin center intensities. Alternatively, only the histogram can be
passed.
Returns
-------
threshold : float or int or array
Threshold value(s).
References
----------
.. [1] Ridler, TW & Calvard, S (1978), "Picture thresholding using an
iterative selection method"
IEEE Transactions on Systems, Man and Cybernetics 8: 630-632,
:DOI:`10.1109/TSMC.1978.4310039`
.. [2] Sezgin M. and Sankur B. (2004) "Survey over Image Thresholding
Techniques and Quantitative Performance Evaluation" Journal of
Electronic Imaging, 13(1): 146-165,
http://www.busim.ee.boun.edu.tr/~sankur/SankurFolder/Threshold_survey.pdf
:DOI:`10.1117/1.1631315`
.. [3] ImageJ AutoThresholder code,
http://fiji.sc/wiki/index.php/Auto_Threshold
Examples
--------
>>> from skimage.data import coins
>>> image = coins()
>>> thresh = threshold_isodata(image)
>>> binary = image > thresh
"""
counts, bin_centers = _validate_image_histogram(image, hist, nbins)
# image only contains one unique value
if len(bin_centers) == 1:
if return_all:
return bin_centers
else:
return bin_centers[0]
counts = counts.astype('float32', copy=False)
# csuml and csumh contain the count of pixels in that bin or lower, and
# in all bins strictly higher than that bin, respectively
csuml = np.cumsum(counts)
csumh = csuml[-1] - csuml
# intensity_sum contains the total pixel intensity from each bin
intensity_sum = counts * bin_centers
# l and h contain average value of all pixels in that bin or lower, and
# in all bins strictly higher than that bin, respectively.
# Note that since exp.histogram does not include empty bins at the low or
# high end of the range, csuml and csumh are strictly > 0, except in the
# last bin of csumh, which is zero by construction.
# So no worries about division by zero in the following lines, except
# for the last bin, but we can ignore that because no valid threshold
# can be in the top bin.
# To avoid the division by zero, we simply skip over the last element in
# all future computation.
csum_intensity = np.cumsum(intensity_sum)
lower = csum_intensity[:-1] / csuml[:-1]
higher = (csum_intensity[-1] - csum_intensity[:-1]) / csumh[:-1]
# isodata finds threshold values that meet the criterion t = (l + m)/2
# where l is the mean of all pixels <= t and h is the mean of all pixels
# > t, as calculated above. So we are looking for places where
# (l + m) / 2 equals the intensity value for which those l and m figures
# were calculated -- which is, of course, the histogram bin centers.
# We only require this equality to be within the precision of the bin
# width, of course.
all_mean = (lower + higher) / 2.0
bin_width = bin_centers[1] - bin_centers[0]
# Look only at thresholds that are below the actual all_mean value,
# for consistency with the threshold being included in the lower pixel
# group. Otherwise, can get thresholds that are not actually fixed-points
# of the isodata algorithm. For float images, this matters less, since
# there really can't be any guarantees anymore anyway.
distances = all_mean - bin_centers[:-1]
thresholds = bin_centers[:-1][(distances >= 0) & (distances < bin_width)]
if return_all:
return thresholds
else:
return thresholds[0]
# Computing a histogram using np.histogram on a uint8 image with bins=256
# doesn't work and results in aliasing problems. We use a fully specified set
# of bins to ensure that each uint8 value false into its own bin.
_DEFAULT_ENTROPY_BINS = tuple(np.arange(-0.5, 255.51, 1))
def _cross_entropy(image, threshold, bins=_DEFAULT_ENTROPY_BINS):
"""Compute cross-entropy between distributions above and below a threshold.
Parameters
----------
image : array
The input array of values.
threshold : float
The value dividing the foreground and background in ``image``.
bins : int or array of float, optional
The number of bins or the bin edges. (Any valid value to the ``bins``
argument of ``np.histogram`` will work here.) For an exact calculation,
each unique value should have its own bin. The default value for bins
ensures exact handling of uint8 images: ``bins=256`` results in
aliasing problems due to bin width not being equal to 1.
Returns
-------
nu : float
The cross-entropy target value as defined in [1]_.
Notes
-----
See Li and Lee, 1993 [1]_; this is the objective function ``threshold_li``
minimizes. This function can be improved but this implementation most
closely matches equation 8 in [1]_ and equations 1-3 in [2]_.
References
----------
.. [1] Li C.H. and Lee C.K. (1993) "Minimum Cross Entropy Thresholding"
Pattern Recognition, 26(4): 617-625
:DOI:`10.1016/0031-3203(93)90115-D`
.. [2] Li C.H. and Tam P.K.S. (1998) "An Iterative Algorithm for Minimum
Cross Entropy Thresholding" Pattern Recognition Letters, 18(8): 771-776
:DOI:`10.1016/S0167-8655(98)00057-9`
"""
histogram, bin_edges = np.histogram(image, bins=bins, density=True)
bin_centers = np.convolve(bin_edges, [0.5, 0.5], mode='valid')
t = np.flatnonzero(bin_centers > threshold)[0]
m0a = np.sum(histogram[:t]) # 0th moment, background
m0b = np.sum(histogram[t:])
m1a = np.sum(histogram[:t] * bin_centers[:t]) # 1st moment, background
m1b = np.sum(histogram[t:] * bin_centers[t:])
mua = m1a / m0a # mean value, background
mub = m1b / m0b
nu = -m1a * np.log(mua) - m1b * np.log(mub)
return nu
def threshold_li(image, *, tolerance=None, initial_guess=None, iter_callback=None):
"""Compute threshold value by Li's iterative Minimum Cross Entropy method.
Parameters
----------
image : (M, N[, ...]) ndarray
Grayscale input image.
tolerance : float, optional
Finish the computation when the change in the threshold in an iteration
is less than this value. By default, this is half the smallest
difference between intensity values in ``image``.
initial_guess : float or Callable[[array[float]], float], optional
Li's iterative method uses gradient descent to find the optimal
threshold. If the image intensity histogram contains more than two
modes (peaks), the gradient descent could get stuck in a local optimum.
An initial guess for the iteration can help the algorithm find the
globally-optimal threshold. A float value defines a specific start
point, while a callable should take in an array of image intensities
and return a float value. Example valid callables include
``numpy.mean`` (default), ``lambda arr: numpy.quantile(arr, 0.95)``,
or even :func:`skimage.filters.threshold_otsu`.
iter_callback : Callable[[float], Any], optional
A function that will be called on the threshold at every iteration of
the algorithm.
Returns
-------
threshold : float
Upper threshold value. All pixels with an intensity higher than
this value are assumed to be foreground.
References
----------
.. [1] Li C.H. and Lee C.K. (1993) "Minimum Cross Entropy Thresholding"
Pattern Recognition, 26(4): 617-625
:DOI:`10.1016/0031-3203(93)90115-D`
.. [2] Li C.H. and Tam P.K.S. (1998) "An Iterative Algorithm for Minimum
Cross Entropy Thresholding" Pattern Recognition Letters, 18(8): 771-776
:DOI:`10.1016/S0167-8655(98)00057-9`
.. [3] Sezgin M. and Sankur B. (2004) "Survey over Image Thresholding
Techniques and Quantitative Performance Evaluation" Journal of
Electronic Imaging, 13(1): 146-165
:DOI:`10.1117/1.1631315`
.. [4] ImageJ AutoThresholder code, http://fiji.sc/wiki/index.php/Auto_Threshold
Examples
--------
>>> from skimage.data import camera
>>> image = camera()
>>> thresh = threshold_li(image)
>>> binary = image > thresh
"""
# Remove nan:
image = image[~np.isnan(image)]
if image.size == 0:
return np.nan
# Make sure image has more than one value; otherwise, return that value
# This works even for np.inf
if np.all(image == image.flat[0]):
return image.flat[0]
# At this point, the image only contains np.inf, -np.inf, or valid numbers
image = image[np.isfinite(image)]
# if there are no finite values in the image, return 0. This is because
# at this point we *know* that there are *both* inf and -inf values,
# because inf == inf evaluates to True. We might as well separate them.
if image.size == 0:
return 0.0
# Li's algorithm requires positive image (because of log(mean))
image_min = np.min(image)
image -= image_min
if image.dtype.kind in 'iu':
tolerance = tolerance or 0.5
else:
tolerance = tolerance or np.min(np.diff(np.unique(image))) / 2
# Initial estimate for iteration. See "initial_guess" in the parameter list
if initial_guess is None:
t_next = np.mean(image)
elif callable(initial_guess):
t_next = initial_guess(image)
elif np.isscalar(initial_guess): # convert to new, positive image range
t_next = initial_guess - float(image_min)
image_max = np.max(image) + image_min
if not 0 < t_next < np.max(image):
msg = (
f'The initial guess for threshold_li must be within the '
f'range of the image. Got {initial_guess} for image min '
f'{image_min} and max {image_max}.'
)
raise ValueError(msg)
t_next = image.dtype.type(t_next)
else:
raise TypeError(
'Incorrect type for `initial_guess`; should be '
'a floating point value, or a function mapping an '
'array to a floating point value.'
)
# initial value for t_curr must be different from t_next by at
# least the tolerance. Since the image is positive, we ensure this
# by setting to a large-enough negative number
t_curr = -2 * tolerance
# Callback on initial iterations
if iter_callback is not None:
iter_callback(t_next + image_min)
# Stop the iterations when the difference between the
# new and old threshold values is less than the tolerance
# or if the background mode has only one value left,
# since log(0) is not defined.
if image.dtype.kind in 'iu':
hist, bin_centers = histogram(image.reshape(-1), source_range='image')
hist = hist.astype('float32', copy=False)
while abs(t_next - t_curr) > tolerance:
t_curr = t_next
foreground = bin_centers > t_curr
background = ~foreground
mean_fore = np.average(bin_centers[foreground], weights=hist[foreground])
mean_back = np.average(bin_centers[background], weights=hist[background])
if mean_back == 0:
break
t_next = (mean_back - mean_fore) / (np.log(mean_back) - np.log(mean_fore))
if iter_callback is not None:
iter_callback(t_next + image_min)
else:
while abs(t_next - t_curr) > tolerance:
t_curr = t_next
foreground = image > t_curr
mean_fore = np.mean(image[foreground])
mean_back = np.mean(image[~foreground])
if mean_back == 0.0:
break
t_next = (mean_back - mean_fore) / (np.log(mean_back) - np.log(mean_fore))
if iter_callback is not None:
iter_callback(t_next + image_min)
threshold = t_next + image_min
return threshold
def threshold_minimum(image=None, nbins=256, max_num_iter=10000, *, hist=None):
"""Return threshold value based on minimum method.
The histogram of the input ``image`` is computed if not provided and
smoothed until there are only two maxima. Then the minimum in between is
the threshold value.
Either image or hist must be provided. In case hist is given, the actual
histogram of the image is ignored.
Parameters
----------
image : (M, N[, ...]) ndarray, optional
Grayscale input image.
nbins : int, optional
Number of bins used to calculate histogram. This value is ignored for
integer arrays.
max_num_iter : int, optional
Maximum number of iterations to smooth the histogram.
hist : array, or 2-tuple of arrays, optional
Histogram to determine the threshold from and a corresponding array
of bin center intensities. Alternatively, only the histogram can be
passed.
Returns
-------
threshold : float
Upper threshold value. All pixels with an intensity higher than
this value are assumed to be foreground.
Raises
------
RuntimeError
If unable to find two local maxima in the histogram or if the
smoothing takes more than 1e4 iterations.
References
----------
.. [1] C. A. Glasbey, "An analysis of histogram-based thresholding
algorithms," CVGIP: Graphical Models and Image Processing,
vol. 55, pp. 532-537, 1993.
.. [2] Prewitt, JMS & Mendelsohn, ML (1966), "The analysis of cell
images", Annals of the New York Academy of Sciences 128: 1035-1053
:DOI:`10.1111/j.1749-6632.1965.tb11715.x`
Examples
--------
>>> from skimage.data import camera
>>> image = camera()
>>> thresh = threshold_minimum(image)
>>> binary = image > thresh
"""
def find_local_maxima_idx(hist):
# We can't use scipy.signal.argrelmax
# as it fails on plateaus
maximum_idxs = list()
direction = 1
for i in range(hist.shape[0] - 1):
if direction > 0:
if hist[i + 1] < hist[i]:
direction = -1
maximum_idxs.append(i)
else:
if hist[i + 1] > hist[i]:
direction = 1
return maximum_idxs
counts, bin_centers = _validate_image_histogram(image, hist, nbins)
smooth_hist = counts.astype('float32', copy=False)
for counter in range(max_num_iter):
smooth_hist = ndi.uniform_filter1d(smooth_hist, 3)
maximum_idxs = find_local_maxima_idx(smooth_hist)
if len(maximum_idxs) < 3:
break
if len(maximum_idxs) != 2:
raise RuntimeError('Unable to find two maxima in histogram')
elif counter == max_num_iter - 1:
raise RuntimeError('Maximum iteration reached for histogram' 'smoothing')
# Find the lowest point between the maxima
threshold_idx = np.argmin(smooth_hist[maximum_idxs[0] : maximum_idxs[1] + 1])
return bin_centers[maximum_idxs[0] + threshold_idx]
def threshold_mean(image):
"""Return threshold value based on the mean of grayscale values.
Parameters
----------
image : (M, N[, ...]) ndarray
Grayscale input image.
Returns
-------
threshold : float
Upper threshold value. All pixels with an intensity higher than
this value are assumed to be foreground.
References
----------
.. [1] C. A. Glasbey, "An analysis of histogram-based thresholding
algorithms," CVGIP: Graphical Models and Image Processing,
vol. 55, pp. 532-537, 1993.
:DOI:`10.1006/cgip.1993.1040`
Examples
--------
>>> from skimage.data import camera
>>> image = camera()
>>> thresh = threshold_mean(image)
>>> binary = image > thresh
"""
return np.mean(image)
def threshold_triangle(image, nbins=256):
"""Return threshold value based on the triangle algorithm.
Parameters
----------
image : (M, N[, ...]) ndarray
Grayscale input image.
nbins : int, optional
Number of bins used to calculate histogram. This value is ignored for
integer arrays.
Returns
-------
threshold : float
Upper threshold value. All pixels with an intensity higher than
this value are assumed to be foreground.
References
----------
.. [1] Zack, G. W., Rogers, W. E. and Latt, S. A., 1977,
Automatic Measurement of Sister Chromatid Exchange Frequency,
Journal of Histochemistry and Cytochemistry 25 (7), pp. 741-753
:DOI:`10.1177/25.7.70454`
.. [2] ImageJ AutoThresholder code,
http://fiji.sc/wiki/index.php/Auto_Threshold
Examples
--------
>>> from skimage.data import camera
>>> image = camera()
>>> thresh = threshold_triangle(image)
>>> binary = image > thresh
"""
# nbins is ignored for integer arrays
# so, we recalculate the effective nbins.
hist, bin_centers = histogram(image.reshape(-1), nbins, source_range='image')
nbins = len(hist)
# Find peak, lowest and highest gray levels.
arg_peak_height = np.argmax(hist)
peak_height = hist[arg_peak_height]
arg_low_level, arg_high_level = np.flatnonzero(hist)[[0, -1]]
if arg_low_level == arg_high_level:
# Image has constant intensity.
return image.ravel()[0]
# Flip is True if left tail is shorter.
flip = arg_peak_height - arg_low_level < arg_high_level - arg_peak_height
if flip:
hist = hist[::-1]
arg_low_level = nbins - arg_high_level - 1
arg_peak_height = nbins - arg_peak_height - 1
# If flip == True, arg_high_level becomes incorrect
# but we don't need it anymore.
del arg_high_level
# Set up the coordinate system.
width = arg_peak_height - arg_low_level
x1 = np.arange(width)
y1 = hist[x1 + arg_low_level]
# Normalize.
norm = np.sqrt(peak_height**2 + width**2)
peak_height /= norm
width /= norm
# Maximize the length.
# The ImageJ implementation includes an additional constant when calculating
# the length, but here we omit it as it does not affect the location of the
# minimum.
length = peak_height * x1 - width * y1
arg_level = np.argmax(length) + arg_low_level
if flip:
arg_level = nbins - arg_level - 1
return bin_centers[arg_level]
def _mean_std(image, w):
"""Return local mean and standard deviation of each pixel using a
neighborhood defined by a rectangular window size ``w``.