From 9a89fab612cadc46b73a09391d22d401c0e93ac3 Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Thu, 8 Dec 2011 23:34:08 -0500 Subject: [PATCH] Add handling for images with negative integers --- skimage/thresholding/tests/test_thresholding.py | 5 ++--- skimage/thresholding/thresholding.py | 8 ++++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/skimage/thresholding/tests/test_thresholding.py b/skimage/thresholding/tests/test_thresholding.py index 485ffbca..6fc3f020 100644 --- a/skimage/thresholding/tests/test_thresholding.py +++ b/skimage/thresholding/tests/test_thresholding.py @@ -16,10 +16,9 @@ class TestSimpleImage(): def test_otsu(self): assert threshold_otsu(self.image) == 2 - @np.testing.raises(NotImplementedError) - def test_otsu_raises_error(self): + def test_otsu_negative_int(self): image = self.image - 2 - threshold_otsu(image) + assert threshold_otsu(image) == 0 def test_otsu_float_image(self): image = np.float64(self.image) diff --git a/skimage/thresholding/thresholding.py b/skimage/thresholding/thresholding.py index 62af6d8d..e346efba 100644 --- a/skimage/thresholding/thresholding.py +++ b/skimage/thresholding/thresholding.py @@ -89,11 +89,11 @@ def histogram(image, bins): The values at the center of the bins. """ if np.issubdtype(image.dtype, np.integer): + offset = 0 if np.min(image) < 0: - msg = "Images with negative values not allowed" - raise NotImplementedError(msg) - hist = np.bincount(image.flat) - bin_centers = np.arange(len(hist)) + offset = np.min(image) + hist = np.bincount(image.ravel() - offset) + bin_centers = np.arange(len(hist)) + offset # clip histogram to return only non-zero bins idx = np.nonzero(hist)[0][0]