Add handling for images with negative integers

This commit is contained in:
Tony S Yu
2011-12-08 23:34:08 -05:00
parent f3e5a30125
commit 9a89fab612
2 changed files with 6 additions and 7 deletions
@@ -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)
+4 -4
View File
@@ -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]