From 4d5889964bbda6f4df17449d7141e7937a7f1c08 Mon Sep 17 00:00:00 2001 From: radioxoma Date: Fri, 10 Jan 2014 22:25:48 +0300 Subject: [PATCH] Unit-tests and small fixes --- skimage/filter/tests/test_thresholding.py | 12 ++++++++++++ skimage/filter/thresholding.py | 23 ++++++++++++----------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/skimage/filter/tests/test_thresholding.py b/skimage/filter/tests/test_thresholding.py index a0d39e74..7d42fbcf 100644 --- a/skimage/filter/tests/test_thresholding.py +++ b/skimage/filter/tests/test_thresholding.py @@ -164,5 +164,17 @@ def test_isodata_moon_image(): assert threshold_isodata(moon) == 87 +def test_isodata_moon_image_negative_int(): + moon = data.moon().astype(np.int32) + moon -= 100 + assert threshold_isodata(moon) == -13 + + +def test_isodata_moon_image_negative_float(): + moon = data.moon().astype(np.float64) + moon -= 100 + assert -13 < threshold_isodata(moon) < -12 + + if __name__ == '__main__': np.testing.run_module_suite() diff --git a/skimage/filter/thresholding.py b/skimage/filter/thresholding.py index f2de468f..932089ba 100644 --- a/skimage/filter/thresholding.py +++ b/skimage/filter/thresholding.py @@ -198,14 +198,14 @@ def threshold_isodata(image, nbins=256): Parameters ---------- image : array - Input image float or int of any range. + Input image. nbins : int, optional Number of bins used to calculate histogram. This value is ignored for integer arrays. Returns ------- - threshold : float64 or int64 + threshold : float or int Upper threshold value. All pixels intensities that less or equal of this value assumed as background. @@ -233,18 +233,19 @@ def threshold_isodata(image, nbins=256): if bin_centers.size == 1: return bin_centers[0] # It is not necessary to calculate probability mass function in this case - # since in the l and h fractions it's reduced. - pmf = hist.astype(float)# / hist.sum() - cpmfl = np.cumsum(pmf, dtype=float) # Cumulative probability mass function - cpmfh = np.cumsum(pmf[::-1], dtype=float)[::-1] + # since the l and h fractions are reduced. + pmf = hist.astype(np.float32)# / hist.sum() + cpmfl = np.cumsum(pmf, dtype=np.float32) + cpmfh = np.cumsum(pmf[::-1], dtype=np.float32)[::-1] binnums = np.arange(pmf.size, dtype=np.uint8) - l = np.ma.divide(np.cumsum(pmf * binnums, dtype=float), cpmfl) - h = np.ma.divide(np.cumsum((pmf[::-1] * binnums[::-1]), dtype=float)[::-1], - cpmfh) + l = np.ma.divide(np.cumsum(pmf * binnums, dtype=np.float32), cpmfl) + h = np.ma.divide( + np.cumsum((pmf[::-1] * binnums[::-1]), dtype=np.float32)[::-1], + cpmfh) allmean = (l + h) / 2.0 threshold = bin_centers[np.nonzero(allmean.round() == binnums)[0][0]] - # ImageJ shows *inclusive* threshold. This implementation returns - # threshold, where `background <= threshold_value < foreground`. + # This implementation returns threshold where + # `background <= threshold < foreground`. return threshold