From 7ad4b91d6566b8f4951975552f879d911bb7dd85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Fri, 24 Aug 2012 12:42:35 +0200 Subject: [PATCH 1/3] Add support for boolean dtype conversion --- skimage/util/dtype.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/skimage/util/dtype.py b/skimage/util/dtype.py index 9697e627..35f37889 100644 --- a/skimage/util/dtype.py +++ b/skimage/util/dtype.py @@ -1,7 +1,8 @@ from __future__ import division import numpy as np -__all__ = ['img_as_float', 'img_as_int', 'img_as_uint', 'img_as_ubyte'] +__all__ = ['img_as_float', 'img_as_int', 'img_as_uint', 'img_as_ubyte', + 'img_as_bool'] from .. import get_log log = get_log('dtype_converter') @@ -15,7 +16,8 @@ dtype_range = {np.uint8: (0, 255), integer_types = (np.uint8, np.uint16, np.int8, np.int16) -_supported_types = (np.uint8, np.uint16, np.uint32, +_supported_types = (np.bool_, np.bool8, + np.uint8, np.uint16, np.uint32, np.int8, np.int16, np.int32, np.float32, np.float64) @@ -145,6 +147,10 @@ def convert(image, dtype, force_copy=False, uniform=False): kind_in = dtypeobj_in.kind itemsize = dtypeobj.itemsize itemsize_in = dtypeobj_in.itemsize + + if kind == 'b' or kind_in == 'b': + return dtype(image) + if kind in 'ui': imin = np.iinfo(dtype).min imax = np.iinfo(dtype).max @@ -322,3 +328,26 @@ def img_as_ubyte(image, force_copy=False): """ return convert(image, np.uint8, force_copy) + + +def img_as_bool(image, force_copy=False): + """Convert an image to boolean format. + + Parameters + ---------- + image : ndarray + Input image. + force_copy : bool + Force a copy of the data, irrespective of its current dtype. + + Returns + ------- + out : ndarray of bool (bool_) + Output image. + + Notes + ----- + All non-zero elements are treated as True. + + """ + return convert(image, np.bool_, force_copy) From 4b303f36ccd69b9374f19b2eebd8107059c63674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Fri, 24 Aug 2012 12:44:00 +0200 Subject: [PATCH 2/3] Update test cases for boolean dtype conversion --- skimage/util/tests/test_dtype.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/skimage/util/tests/test_dtype.py b/skimage/util/tests/test_dtype.py index 816d5d4c..9803e0af 100644 --- a/skimage/util/tests/test_dtype.py +++ b/skimage/util/tests/test_dtype.py @@ -1,7 +1,7 @@ import numpy as np from numpy.testing import assert_equal, assert_raises from skimage import img_as_int, img_as_float, \ - img_as_uint, img_as_ubyte + img_as_uint, img_as_ubyte, img_as_bool from skimage.util.dtype import convert @@ -86,5 +86,19 @@ def test_copy(): assert y is x assert z is not x + +def test_bool(): + img_ = np.zeros((10, 10), np.bool_) + img8 = np.zeros((10, 10), np.bool8) + img_[1, 1] = True + img8[1, 1] = True + funcs = (img_as_float, img_as_int, img_as_ubyte, img_as_uint, img_as_bool) + for func in funcs: + converted_ = func(img_) + assert np.sum(converted_) == 1 + converted8 = func(img8) + assert np.sum(converted8) == 1 + + if __name__ == '__main__': np.testing.run_module_suite() From 3e6a51cc2a00baa162c79cd28cd9ab2e0c88fba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Fri, 24 Aug 2012 23:17:32 +0200 Subject: [PATCH 3/3] Add bool dtype range --- skimage/util/dtype.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/skimage/util/dtype.py b/skimage/util/dtype.py index 35f37889..4a5a6f23 100644 --- a/skimage/util/dtype.py +++ b/skimage/util/dtype.py @@ -7,7 +7,9 @@ __all__ = ['img_as_float', 'img_as_int', 'img_as_uint', 'img_as_ubyte', from .. import get_log log = get_log('dtype_converter') -dtype_range = {np.uint8: (0, 255), +dtype_range = {np.bool_: (False, True), + np.bool8: (False, True), + np.uint8: (0, 255), np.uint16: (0, 65535), np.int8: (-128, 127), np.int16: (-32768, 32767),