Skip to content

ND multichannel arrays in color conversion #3185

Closed

Description

@hmaarrfk

if arr.ndim not in [3, 4] or arr.shape[-1] != 3:

The line above only allows you to have 4D multichannel arrays.

Would you accept patches that ensures that we can work with ND multichannel arrays?

This is useful for example when you are trying to stitch tiles of acquired images with a microscope.

You might have large arrays, each RGB (3 dimensions) tiled in the x, y, and time dimension.
6 dimensions! I'm sure somebody could think of more.

I know there is talk about using an other backend for color conversion, but the skimage color backend seems to do the trick for now.

Activity

  1. jni commented on Jun 13, 2018

    @jni
    Member

    Yes that line is super hacky! I was actually just thinking of this very line of code!!! LOL

    If you can find a way to fix it without breaking backwards compatibility, we would welcome that very much. Otherwise we might have to go through a deprecation path.

    The main question is whether we want to keep the behaviour of gray2rgb(rgbimage) being a no-op. It's kind of nice and I've used it a few times, but it's hard to implement if we want to support arbitrarily-shaped nd arrays.

    But, either way, that line has to go. It's very magical behaviour, and, what's worse, there's no way to turn the magic off.

  2. hmaarrfk commented on Jun 13, 2018

    @hmaarrfk
    MemberAuthor

    it seems that the parameter multichannel has been used to simplify the guessing logic.
    Maybe we can issue warnings to deprecate the old behaviour

    Warning: your array has much more than 3 dimensions in the `-1` index. Are you sure the array is RGB?
    

    For example: I'm thinking of the following docstring for rgb2gray

    def rgb2gray(rgb):
        """Compute luminance of an RGB image.
    
        Parameters
        ----------
        rgb : array_like
            The image in RGB format, in a N-D array that can be sliced as
            ``[..., :3]``. For RGBA images, this the alpha channel is
            dropped.
    
        Returns
        -------
        out : ndarray
            The luminance image - an array which is the same size as the input
            array, but with the channel dimension removed.
    
        Raises
        ------
        ValueError
            If `rgb2gray` cannot be sliced as:
            ``[..., :3]``.
    
        References
        ----------
        .. [1] http://www.poynton.com/PDFs/ColorFAQ.pdf
    
        Notes
        -----
        The weights used in this conversion are calibrated for contemporary
        CRT phosphors::
    
            Y = 0.2125 R + 0.7154 G + 0.0721 B
    
        If there is an alpha channel present, it is ignored.
    
        Examples
        --------
        >>> from skimage.color import rgb2gray
        >>> from skimage import data
        >>> img = data.astronaut()
        >>> img_gray = rgb2gray(img)
        """
        pass
  3. hmaarrfk commented on Jun 13, 2018

    @hmaarrfk
    MemberAuthor

    Why is this logic the way it is in the function guess_spatial_dimensions? It seems in both cases it should be None maybe?

        if image.ndim == 3 and image.shape[-1] == 3:
            return None
        if image.ndim == 4 and image.shape[-1] == 3:
            return 3

    https://github.com/scikit-image/scikit-image/blob/master/skimage/color/colorconv.py#L87

  4. stefanv commented on Jun 13, 2018

    @stefanv
    Member

    I tend to agree.

  5. hmaarrfk commented on Jun 13, 2018

    @hmaarrfk
    MemberAuthor

    Seems like it is only used in filters.gaussian

    def gaussian(image, sigma=1, output=None, mode='nearest', cval=0,
                 multichannel=None, preserve_range=False, truncate=4.0):
        spatial_dims = guess_spatial_dimensions(image)
        if spatial_dims is None and multichannel is None:
            msg = ("Images with dimensions (M, N, 3) are interpreted as 2D+RGB "
                   "by default. Use `multichannel=False` to interpret as "
                   "3D image with last dimension of length 3.")
            warn(RuntimeWarning(msg))
            multichannel = True
  6. rfezzani commented on Jan 28, 2020

    @rfezzani
    Member

    My solution to this issue is may be a little bit radical: I consider that

    • multichannel is implicitly True for all the functions but gray2rgb,
    • rgb2gray does not accept gray level images,
    • rgb2gray does not accept RGBA images

    I implemented It in #4418. It allows to easily add nD support using Ellipsis and alleviates all the "guessing"...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ⏩ type: Enhancement💬 Discussion

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions