From 1c116a9905beddc188c0bef2a25fc0bdbe42872b Mon Sep 17 00:00:00 2001 From: Juan Nunez-Iglesias Date: Mon, 20 May 2013 16:42:06 +1000 Subject: [PATCH] Add function to guesstimate number of spatial dimensions --- skimage/color/__init__.py | 2 ++ skimage/color/colorconv.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/skimage/color/__init__.py b/skimage/color/__init__.py index 9202bee9..19620cb1 100644 --- a/skimage/color/__init__.py +++ b/skimage/color/__init__.py @@ -1,4 +1,5 @@ from .colorconv import (convert_colorspace, + guess_spatial_dimensions, rgb2hsv, hsv2rgb, rgb2xyz, @@ -45,6 +46,7 @@ from .colorlabel import color_dict, label2rgb __all__ = ['convert_colorspace', + 'guess_spatial_dimensions', 'rgb2hsv', 'hsv2rgb', 'rgb2xyz', diff --git a/skimage/color/colorconv.py b/skimage/color/colorconv.py index 540c5a75..5225f7ea 100644 --- a/skimage/color/colorconv.py +++ b/skimage/color/colorconv.py @@ -49,6 +49,37 @@ from ..util import dtype from skimage._shared.utils import deprecated +def guess_spatial_dimensions(image): + """Make an educated guess about whether an image has a channels dimension. + + Parameters + ---------- + image : ndarray + The input image. + + Returns + ------- + spatial_dims : int or None + The number of spatial dimensions of `image`. If ambiguous, the value + is `None`. + + Raises + ------ + ValueError + If the image array has less than two or more than four dimensions. + """ + if image.ndim == 2: + return 2 + if image.ndim == 3 and image.shape[-1] != 3: + return 3 + if image.ndim == 3 and image.shape[-1] == 3: + return None + if image.ndim == 4 and image.shape[-1] == 3: + return 3 + else: + raise ValueError("Expected 2D, 3D, or 4D array, got %iD." % image.ndim) + + @deprecated() def is_rgb(image): """Test whether the image is RGB or RGBA.