8000 Add a channel_axis argument to functions in the skimage.color module … · scikit-image/scikit-image@293fb93 · GitHub
[go: up one dir, main page]

Skip to content

Commit 293fb93

Browse files
grlee77mkcorrfezzani
authored
Add a channel_axis argument to functions in the skimage.color module (#5462)
* TST: channel_axis tests for rgba2rgb * Add channel_axis kwarg to rgb2hsv and hsv2rgb TST: test rgb2hsv and hsv2rgb with variable channel_axis * use existing channel_as_last_axis decorator for hsv2rgb and rgb2hsv * add channel_axis to rgb2xyz, xyz2rgb add channel_axis to rgb2rgbcie, rgbcie2rgb add channel_axis to gray2rgb, gray2rgba, rgb2gray, rgba2gray * add channel_axis to rgb2luv, luv2rgb, xyz2luv, luv2xyz add channel_axis to rgb2lab, lab2rgb, xyz2lab, lab2xyz * add channel_axis to combine_stains, separate_stains, rgb2hed, hed2rgb * add channel_axis support to remaining functions in colorconv.py * support user-specified channel_axis for label2rgb * support channel_axis for deltaE_* * add channel_axis option to adapt_rgb decorator, each_channel and hsv_value * support use of channel_axis in functions wrapped by adapt_rgb support channel_axis for hsv_value and each_channel decorators * DOC: update docstrings to describe channel_axis add channel_axis support to convert_colorspace * BUG: fix label2rgb when an RGB image is provided and channel_axis != -1 * Revert "support use of channel_axis in functions wrapped by adapt_rgb" This reverts commit 8063049. * Revert "add channel_axis option to adapt_rgb decorator, each_channel and hsv_value" This reverts commit 3d82e2c. * update shape description in labl2rgb docstring * Apply suggestions from code review Co-authored-by: Marianne Corvellec <marianne.corvellec@ens-lyon.org> * switch from ValueError to np.AxisError for invalid axis in rgba2rgb * raise TypeError on non-integer axis * TST: test additional negative channel_axis values Co-authored-by: Riadh Fezzani <rfezzani@gmail.com> * STYLE: rename _reshape_nd to reshape_nd * move identity helper to skimage._shared.utils Co-authored-by: Marianne Corvellec <marianne.corvellec@ens-lyon.org> Co-authored-by: Riadh Fezzani <rfezzani@gmail.com>
1 parent 6ba3375 commit 293fb93

File tree

8 files changed

+879
-324
lines changed

8 files changed

+879
-324
lines changed

skimage/_shared/utils.py

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import inspect
22
import functools
3-
import numbers
43
import sys
54
import warnings
65
from collections.abc import Iterable
76

87
import numpy as np
9-
from numpy.lib import NumpyVersion
108
import scipy
9+
from numpy.lib import NumpyVersion
1110

1211
from ..util import img_as_float
1312
from ._warnings import all_warnings, warn
1413

1514
__all__ = ['deprecated', 'get_bound_method_class', 'all_warnings',
16-
'safe_as_int', 'check_nD', 'check_shape_equality', 'warn']
15+
'safe_as_int', 'check_shape_equality', 'check_nD', 'warn',
16+
'reshape_nd', 'identity']
1717

1818

1919
class skimage_deprecation(Warning):
@@ -211,7 +211,6 @@ def fixed_func(*args, **kwargs):
211211
convert = {True: -1, False: None}
212212
kwargs['channel_axis'] = convert[kwargs.pop('multichannel')]
213213

214-
215214
# Call the function with the fixed arguments
216215
return func(*args, **kwargs)
217216
return fixed_func
@@ -457,6 +456,41 @@ def slice_at_axis(sl, axis):
457456
return (slice(None),) * axis + (sl,) + (...,)
458457

459458

459+
def reshape_nd(arr, ndim, dim):
460+
"""Reshape a 1D array to have n dimensions, all singletons but one.
461+
462+
Parameters
463+
----------
464+
arr : array, shape (N,)
465+
Input array
466+
ndim : int
467+
Number of desired dimensions of reshaped array.
468+
dim : int
469+
Which dimension/axis will not be singleton-sized.
470+
471+
Returns
472+
-------
473+
arr_reshaped : array, shape ([1, ...], N, [1,...])
474+
View of `arr` reshaped to the desired shape.
475+
476+
Examples
477+
--------
478+
>>> rng = np.random.default_rng()
479+
>>> arr = rng.random(7)
480+
>>> _reshape_nd(arr, 2, 0).shape
481+
(7, 1)
482+
>>> _reshape_nd(arr, 3, 1).shape
483+
(1, 7, 1)
484+
>>> _reshape_nd(arr, 4, -1).shape
485+
(1, 1, 1, 7)
486+
"""
487+
if arr.ndim != 1:
488+
raise ValueError("arr must be a 1D array")
489+
new_shape = [1] * ndim
490+
new_shape[dim] = -1
491+
return np.reshape(arr, new_shape)
492+
493+
460494
def check_nD(array, ndim, arg_name='image'):
461495
"""
462496
Verify an array meets the desired ndims and array isn't empty.
@@ -478,8 +512,10 @@ def check_nD(array, ndim, arg_name='image'):
478512
ndim = [ndim]
479513
if array.size == 0:
480514
raise ValueError(msg_empty_array % (arg_name))
481-
if not array.ndim in ndim:
482-
raise ValueError(msg_incorrect_dim % (arg_name, '-or-'.join([str(n) for n in ndim])))
515+
if array.ndim not in ndim:
516+
raise ValueError(
517+
msg_incorrect_dim % (arg_name, '-or-'.join([str(n) for n in ndim]))
518+
)
483519

484520

485521
def convert_to_float(image, preserve_range):
@@ -627,3 +663,8 @@ def _supported_float_type(input_dtype, allow_complex=False):
627663
if not allow_complex and input_dtype.kind == 'c':
628664
raise ValueError("complex valued input is not supported")
629665
return new_float_type.get(input_dtype.char, np.float64)
666+
667+
668+
def identity(image, *args, **kwargs):
669+
"""Returns the first argument unmodified."""
670+
return image

skimage/color/adapt_rgb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
__all__ = ['adapt_rgb', 'hsv_value', 'each_channel']
1010

1111

12-
def is_rgb_like(image):
12+
def is_rgb_like(image, channel_axis=-1):
1313
"""Return True if the image *looks* like it's RGB.
1414
1515
This function should not be public because it is only intended to be used
1616
for functions that don't accept volumes as input, since checking an image's
1717
shape is fragile.
1818
"""
19-
return (image.ndim == 3) and (image.shape[2] in (3, 4))
19+
return (image.ndim == 3) and (image.shape[channel_axis] in (3, 4))
2020

2121

2222
def adapt_rgb(apply_to_rgb):

0 commit comments

Comments
 (0)
0