From 6c47bd49ddb90e5196333224a4e6b211fb24d089 Mon Sep 17 00:00:00 2001 From: walter Date: Thu, 16 Jun 2016 10:33:11 +0200 Subject: [PATCH] tests for image types added --- skimage/feature/tests/test_texture.py | 14 ++++++++++++++ skimage/feature/texture.py | 17 +++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/skimage/feature/tests/test_texture.py b/skimage/feature/tests/test_texture.py index 4b88667a..bfa377e6 100644 --- a/skimage/feature/tests/test_texture.py +++ b/skimage/feature/tests/test_texture.py @@ -51,6 +51,20 @@ class TestGLCM(): [0, 0, 2, 0]], dtype=np.uint32) np.testing.assert_array_equal(result[:, :, 0, 0], expected) + def test_error_raise_float(self): + for dtype in [np.float, np.double, np.float16, np.float32, np.float64]: + np.testing.assert_raises(ValueError, greycomatrix, self.image.astype(dtype), [1], [np.pi], 4) + + def test_error_raise_int_types(self): + for dtype in [np.int16, np.int32, np.int64, np.uint16, np.uint32, np.uint64]: + np.testing.assert_raises(ValueError, greycomatrix, self.image.astype(dtype), [1], [np.pi]) + + def test_error_raise_negative(self): + np.testing.assert_raises(ValueError, greycomatrix, self.image.astype(np.int16) - 1, [1], [np.pi], 4) + + def test_error_raise_levels_smaller_max(self): + np.testing.assert_raises(ValueError, greycomatrix, self.image - 1, [1], [np.pi], 3) + def test_image_data_types(self): for dtype in [np.uint16, np.uint32, np.uint64, np.int16, np.int32, np.int64]: img = self.image.astype(dtype) diff --git a/skimage/feature/texture.py b/skimage/feature/texture.py index 5317964d..4de65c76 100644 --- a/skimage/feature/texture.py +++ b/skimage/feature/texture.py @@ -102,7 +102,6 @@ def greycomatrix(image, distances, angles, levels=None, symmetric=False, assert_nD(angles, 1, 'angles') image = np.ascontiguousarray(image) - assert image.min() >= 0 image_max = image.max() @@ -110,17 +109,19 @@ def greycomatrix(image, distances, angles, levels=None, symmetric=False, raise ValueError("Float images are not supported by greycomatrix. " "The image needs to be cast to an unsigned integer type.") - # for 16 bit images (or larger), levels must be set. - if image.dtype != np.uint8: - if levels is None: - raise ValueError("The levels argument is required for data types other than uint8. " - "The resulting matrix will be at least levels ** 2 in size.") + # for image type > 8bit, levels must be set. + if image.dtype not in (np.uint8, np.int8) and levels is None: + raise ValueError("The levels argument is required for data types other than uint8. " + "The resulting matrix will be at least levels ** 2 in size.") - + if image.dtype in (np.int8, np.int16, np.int32, np.int64) and np.any(image < 0): + raise ValueError("Negative valued images are not supported.") + if levels is None: levels = 256 - assert image_max < levels, "The image maximum needs to be smaller than `levels`." + if image_max >= levels: + raise ValueError("The image maximum needs to be smaller than `levels`.") distances = np.ascontiguousarray(distances, dtype=np.float64) angles = np.ascontiguousarray(angles, dtype=np.float64)