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.
This commit is contained in:
Juan Nunez-Iglesias
2014-07-28 12:38:34 +10:00
parent 85a5cea533
commit 278a0d6862
2 changed files with 7 additions and 1 deletions
@@ -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]
+1 -1
View File
@@ -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)