Repository navigation
Add gray2rgba and deprecate RGBA support in gray2rgb - #4424
Conversation
|
|
||
| """ | ||
|
|
||
| arr = dtype.img_as_float(image) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I just realized that I have to check if 0 <= alpha <= 1 :-)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
@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...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...
hmaarrfk
left a comment
There was a problem hiding this comment.
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.
Co-Authored-By: Mark Harfouche <[email protected]>
Co-Authored-By: Mark Harfouche <[email protected]>
|
|
||
| rgba = np.empty(arr.shape + (4, ), dtype=arr.dtype) | ||
| rgba[..., :3] = arr[..., np.newaxis] | ||
| rgba[..., 3] = alpha |
There was a problem hiding this comment.
Do we want to convert alpha to the appropriate units here? Using the convert function
There was a problem hiding this comment.
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....
There was a problem hiding this comment.
I'll have to check the truth table for np.can_cast
There was a problem hiding this comment.
This is the only thing I would like to bring to the attention of the 2nd reviewer.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
I agree with @jni.
Also, I think no explicit conversion needs to be done by us.
|
|
||
| """ | ||
|
|
||
| arr = np.asarray(image) |
There was a problem hiding this comment.
I think this is the job of a static type checker, or a linter. No need to always cast this to an array here.
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.).
|
Anyone can review this PR? It is prerequisite for #4418 |
|
|
||
| rgba = np.empty(arr.shape + (4, ), dtype=arr.dtype) | ||
| rgba[..., :3] = arr[..., np.newaxis] | ||
| rgba[..., 3] = alpha |
There was a problem hiding this comment.
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?
|
|
||
| """ | ||
|
|
||
| arr = np.asarray(image) |
There was a problem hiding this comment.
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.).
|
|
||
| rgba = np.empty(arr.shape + (4, ), dtype=arr.dtype) | ||
| rgba[..., :3] = arr[..., np.newaxis] | ||
| rgba[..., 3] = alpha |
There was a problem hiding this comment.
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(): |
There was a problem hiding this comment.
Should probably add a check to ensure image content is also correct.
There was a problem hiding this comment.
@stefanv, any numerical array like is valid input for gray2rgba, so I don't see any non obvious test for this purpose...
There was a problem hiding this comment.
assert_equal(output[..., :3], gray2rgb(input))
There was a problem hiding this comment.
The requested test are implemented now @stefanv. Thank you for your suggestion.
|
Disregard comments you received on rgb2gray; my brain was having a restful moment. |
Co-Authored-By: Juan Nunez-Iglesias <[email protected]>
Co-Authored-By: Juan Nunez-Iglesias <[email protected]>
|
Anymore requested changes for this PR? Thank you |
|
Thanks, @rfezzani! |
|
🎉 Thank you @stefanv |
Description
This PR adds the
gray2rgbafunction tocolorconvmodule.This is part of the #4418 effort to add nD support to
colorconvsince it helps deprecating RGBA images conversion usinggray2rgb.Checklist
./doc/examples(new features only)./benchmarks, if your changes aren't covered by anexisting benchmark
For reviewers
later.
__init__.py.doc/release/release_dev.rst.@meeseeksdev backport to v0.14.x