Skip to content

Add gray2rgba and deprecate RGBA support in gray2rgb - #4424

Merged
stefanv merged 20 commits into
scikit-image:masterfrom
rfezzani:Add_gray2rgba
Apr 8, 2020
Merged

stefanv merged 20 commits into
scikit-image:masterfrom
rfezzani:Add_gray2rgba

Conversation

@rfezzani

Copy link
Copy Markdown
Member

Description

This PR adds the gray2rgba function to colorconv module.
This is part of the #4418 effort to add nD support to colorconv since it helps deprecating RGBA images conversion using gray2rgb.

Checklist

For reviewers

  • Check that the PR title is short, concise, and will make sense 1 year
    later.
  • Check that new functions are imported in corresponding __init__.py.
  • Check that new features, API changes, and deprecations are mentioned in
    doc/release/release_dev.rst.
  • Consider backporting the PR with @meeseeksdev backport to v0.14.x

Comment thread skimage/color/colorconv.py Outdated

"""

arr = dtype.img_as_float(image)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you so interested in making the image a float? I don't think there is any loss of information if you leave this as the input dtype.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really know how to manage alpha channel if image.dtype is float and image values are not between 0 and 1 :-S.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that I have to check if 0 <= alpha <= 1 :-)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I would simply not worry about that.

Junk in means junk out for me.

At the very least, if dtype is uint8, which many images are, then it is very likely OK to keep it as that.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not in favor of this "junk in junk out" behavior when it can be avoided.
I understand the uint8 case, but what about the int case? The user will end up with an alpha channel filled with 9223372036854775807!
The majority of the functions of this module converts the input image to float, so I guess that this conversion will not be that surprising for the user.
But well, if a majority votes for no conversion...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rfezzani int images are a big problem for us in general. I recommend you read #3009 for some background if you haven't already. The converse problem is that converting to float will probably result in very tiny values if the image is higher depth than 16-bit.

But I very much would like to preserve the type for uint8 images at least, which is a common use case. Probably 16-bit as well...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

according to our data types document, setting the alpha channel to 2^32-1 is the right thing to do:
https://scikit-image.org/docs/dev/user_guide/data_types.html

The thing is that data validation isn't cheap, especially with image data. If you want to validate at every step, you are essentially guaranteeing that nobody that cares about performance will use these functions.

For batch image processing, everybody will start to look elsewhere if calls to min and max are performed multiple times in the library without recourse to remove them.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that 2^32-1 is the correct value, I am just saying that in this case, "junk in" results in "huge 💩 "!
😄
I am not in favor of validation, but conversion is fine since it is used elsewhere in multiple functions in skimage.
By the way, I removed the call to img_as_float...

@rfezzani rfezzani mentioned this pull request Jan 30, 2020
3 of 9 tasks
@rfezzani rfezzani added the 🔽 Deprecation Involves deprecation label Jan 31, 2020
Comment thread skimage/color/colorconv.py Resolved
Comment thread skimage/color/colorconv.py Resolved
Comment thread skimage/color/colorconv.py Resolved
Comment thread skimage/color/colorconv.py Resolved
Comment thread skimage/color/colorconv.py Resolved

@hmaarrfk hmaarrfk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, I would remove certain checks you have on floating point numbers.

For scalars, I would maybe warn users that they are specifying with a floating point type, but not more than that.

@rfezzani rfezzani changed the title Add gray2rgba Add gray2rgba and deprecate RGBA support in gray2rgb Feb 10, 2020

rgba = np.empty(arr.shape + (4, ), dtype=arr.dtype)
rgba[..., :3] = arr[..., np.newaxis]
rgba[..., 3] = alpha

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to convert alpha to the appropriate units here? Using the convert function

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i sticked to what you advised before: "junk in -> junk out"...
I am not against the conversion here, I was even more strict in the first release of this PR since it was raising an error if alpha is an array of different dtype then image....

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have to check the truth table for np.can_cast

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only thing I would like to bring to the attention of the 2nd reviewer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, my take is that we shouldn't raise for out of range (e.g. alpha=2.5 in a float image), but we should raise on under/overflow, such as alpha=4096 for a uint8 image. What do you think about this suggestion @rfezzani?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @jni.

Also, I think no explicit conversion needs to be done by us.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jni and @stefanv, this is already checked here ;-)


"""

arr = np.asarray(image)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the job of a static type checker, or a linter. No need to always cast this to an array here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All right, but image has to be documented as array, or ndarray then... This also prevent for example from passing only one value or a small list of values...
The only thing preventing from using asarray is if we want to use dask, but does this function really need to be accelerated?
I think that the small overhead of asarray worth it...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that we don't really want to do this unless necessary. That said, this problem is so pervasive across all of scikit-image that, for now, it's probably safer to make the asarray call (we don't generally support dask arrays, e.g.).

@rfezzani rfezzani mentioned this pull request Feb 14, 2020
2 of 11 tasks
@rfezzani

Copy link
Copy Markdown
Member Author

Anyone can review this PR? It is prerequisite for #4418

@jni jni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm into it! Thank you @rfezzani! I've made some very minor suggestions. I would like to let this sit for a bit to allow other members from @scikit-image/core to comment, as deprecating gray2rgb with alpha is a major decision. @stefanv what do you think of this one?

Comment thread skimage/color/colorconv.py Resolved
Comment thread skimage/color/colorconv.py Resolved

rgba = np.empty(arr.shape + (4, ), dtype=arr.dtype)
rgba[..., :3] = arr[..., np.newaxis]
rgba[..., 3] = alpha

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, my take is that we shouldn't raise for out of range (e.g. alpha=2.5 in a float image), but we should raise on under/overflow, such as alpha=4096 for a uint8 image. What do you think about this suggestion @rfezzani?

Comment thread skimage/color/colorconv.py Resolved

"""

arr = np.asarray(image)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that we don't really want to do this unless necessary. That said, this problem is so pervasive across all of scikit-image that, for now, it's probably safer to make the asarray call (we don't generally support dask arrays, e.g.).

Comment thread skimage/color/colorconv.py Resolved

rgba = np.empty(arr.shape + (4, ), dtype=arr.dtype)
rgba[..., :3] = arr[..., np.newaxis]
rgba[..., 3] = alpha

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @jni.

Also, I think no explicit conversion needs to be done by us.

assert gray2rgba(img).dtype == img.dtype


def test_gray2rgba_alpha():

@stefanv stefanv Mar 29, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably add a check to ensure image content is also correct.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stefanv, any numerical array like is valid input for gray2rgba, so I don't see any non obvious test for this purpose...

@stefanv stefanv Mar 30, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert_equal(output[..., :3], gray2rgb(input))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requested test are implemented now @stefanv. Thank you for your suggestion.

@stefanv

stefanv commented Mar 29, 2020

Copy link
Copy Markdown
Member

Disregard comments you received on rgb2gray; my brain was having a restful moment.

@rfezzani
rfezzani requested a review from stefanv April 6, 2020 20:56
@rfezzani

rfezzani commented Apr 7, 2020

Copy link
Copy Markdown
Member Author

Anymore requested changes for this PR? Thank you

@stefanv
stefanv merged commit 77726dd into scikit-image:master Apr 8, 2020
@stefanv

stefanv commented Apr 8, 2020

Copy link
Copy Markdown
Member

Thanks, @rfezzani!

@rfezzani

rfezzani commented Apr 8, 2020

Copy link
Copy Markdown
Member Author

🎉 Thank you @stefanv

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants