From 278a0d6862e25a479f38a29f5451f33232fdc3cf Mon Sep 17 00:00:00 2001 From: Juan Nunez-Iglesias Date: Mon, 28 Jul 2014 12:38:34 +1000 Subject: [PATCH 1/2] Bug fix: nbins can be >256 in threshold_isodata The threshold_isodata function created an arange of values up to the number of bins, but gave it type np.uint8, limiting the number of bins to 256. Fixes #1085. --- skimage/filter/tests/test_thresholding.py | 6 ++++++ skimage/filter/thresholding.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/skimage/filter/tests/test_thresholding.py b/skimage/filter/tests/test_thresholding.py index f0a68f7d..47555a83 100644 --- a/skimage/filter/tests/test_thresholding.py +++ b/skimage/filter/tests/test_thresholding.py @@ -67,6 +67,12 @@ class TestSimpleImage(): def test_isodata_linspace(self): assert -63.8 < threshold_isodata(np.linspace(-127, 0, 256)) < -63.6 + def test_isodata_16bit(): + np.random.seed(0) + imfloat = np.random.rand(256, 256) + t = threshold_isodata(imfloat, nbins=1024) + assert 0.49 < t < 0.51 + def test_threshold_adaptive_generic(self): def func(arr): return arr.sum() / arr.shape[0] diff --git a/skimage/filter/thresholding.py b/skimage/filter/thresholding.py index 6ccf8f6b..da74fe47 100644 --- a/skimage/filter/thresholding.py +++ b/skimage/filter/thresholding.py @@ -243,7 +243,7 @@ def threshold_isodata(image, nbins=256): cpmfl = np.cumsum(pmf, dtype=np.float32) cpmfh = np.cumsum(pmf[::-1], dtype=np.float32)[::-1] - binnums = np.arange(pmf.size, dtype=np.uint8) + binnums = np.arange(pmf.size, dtype=np.min_scalar_type(nbins)) # l and h contain average value of pixels in sum of bins, calculated # from lower to higher and from higher to lower respectively. l = np.ma.divide(np.cumsum(pmf * binnums, dtype=np.float32), cpmfl) From 838617cb394c9ab9c948ebd64439d0485edfbf0d Mon Sep 17 00:00:00 2001 From: Juan Nunez-Iglesias Date: Mon, 28 Jul 2014 13:01:41 +1000 Subject: [PATCH 2/2] Fix incorrect test function signature --- skimage/filter/tests/test_thresholding.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/filter/tests/test_thresholding.py b/skimage/filter/tests/test_thresholding.py index 47555a83..8464cb21 100644 --- a/skimage/filter/tests/test_thresholding.py +++ b/skimage/filter/tests/test_thresholding.py @@ -67,7 +67,7 @@ class TestSimpleImage(): def test_isodata_linspace(self): assert -63.8 < threshold_isodata(np.linspace(-127, 0, 256)) < -63.6 - def test_isodata_16bit(): + def test_isodata_16bit(self): np.random.seed(0) imfloat = np.random.rand(256, 256) t = threshold_isodata(imfloat, nbins=1024)