Merge pull request #1086 from jni/thresh-iso

Bug fix: nbins can be >256 in threshold_isodata
This commit is contained in:
Johannes Schönberger
2014-08-02 01:23:28 -04:00
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(self):
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)