You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
it is pretty difficult to correctly guess if an ND image is grey. I think ND color conversion would be super useful for skimage, so that is why I'm asking for input for how to deal with this.
I could do something like:
ifrgb.shape[-1] notin [3, 4]:
or
ifrgb.shape[-1] >4:
but that would break things for tiny little images ;)
maybe this kind of automagic behaviour is better suited for an other wrapper function if we want to keep it?
@hmaarrfk yes, our new mission statement will include something to the effect of "we don't like magic", but there is some legacy code that violates that, and guessing RGB is one of these. So, we are in favour of removing this magic, but we have to be careful with backwards compatibility and not breaking people's code.
The concern for tiny axes is very real and iirc someone was bitten by this very problem a while back.
Something like a keyword argument would be useful, but we have to be careful with it:
multichannel=None -> do some magic
multichannel=True -> always assume RGB (raise ValueError if shape incorrect)
multichannel=False -> ????
So, it's a bit wonky for this function. But something along these lines is desirable, yes.
@stefanv, do you think the guess_channels parameter should be kept at infinitum? If not, I would like to give the chance for people that set it to False to not have their code break between a version upgrade.
As such, I think there should be a change in the behaviour of None during one version bump.
Hrm, actually, why introduce a parameter that you mean to deprecate anyway. How about:
0.15 — keep guessing, but add multichannel=None, warn if multichannel is not specified and data looks to be multichannel
0.16 — keep guessing, but error if multichannel is not specified and data looks to be multichannel
0.17 — stop guessing, rely on multichannel
looks at the shape, and looked at dimensions. It doesn't check that image.dims == np.sqeeze(image).dims and then indexes the 2 address assuming that it is the color axis while it could have been squeezed out.
Glad to fix it, but I have a feeling that things like this are going to keep popping out.
@parekhnish thanks for chiming in! I guess it’s still unclear what the best way forward is based on the above discussion, but fixing the documentation to be correct is certainly desirable in the short term! Would you like to submit a pull request? We would be happy to guide you through the process if you aren’t familiar with it yet.
@jni yes, I can do that. Do you have any pull-request guidelines (commit message format, tests to be done before committing and creating the pull request, etc.)?
The are a bit on the long side, but I hope quite clear. Since this is a documentation fix, I don't think you will need to do any testing. Our continuous integration will catch any ReST formatting errors if they creep in. =)
The problem is even worst then it looks: rgb2gray ignores all image's channels with indices greater then 3 and doesn't raise any error if the number of channels is not 3 or 4 (despite what the documentation says:
fromskimage.colorimportrgb2grayimportnumpyasnpx= (np.random.rand(50, 50, 5) *255).astype("uint8")
y=rgb2gray(x) # Supposed to raise an error be because x.shape[-1] is not 3 or 4
Activity
hmaarrfk commentedon Jun 14, 2018
As a followup question, should
gray2rgbattempt to guess if an image is RGB?Also, the logic is a little wonky. it uses squeeze, though, not entirely correctly.
scikit-image/skimage/color/colorconv.py
Line 843 in 4686b7d
hmaarrfk commentedon Jun 14, 2018
Could I add a
multichannelparameter to turn off all this automagic logic?jni commentedon Jun 14, 2018
@hmaarrfk yes, our new mission statement will include something to the effect of "we don't like magic", but there is some legacy code that violates that, and guessing RGB is one of these. So, we are in favour of removing this magic, but we have to be careful with backwards compatibility and not breaking people's code.
The concern for tiny axes is very real and iirc someone was bitten by this very problem a while back.
Something like a keyword argument would be useful, but we have to be careful with it:
multichannel=None-> do some magicmultichannel=True-> always assume RGB (raise ValueError if shape incorrect)multichannel=False-> ????So, it's a bit wonky for this function. But something along these lines is desirable, yes.
hmaarrfk commentedon Jun 14, 2018
I kinda get stuck thinking that these functions should not take a
guess_channels(instead ofmultichannel) parameter later in the project life cycle.guess_channels=Trueold behaviour where it tries to guess in certain places.guess_channels=Falsenew behaviour, where if it works withnumpy, then it is probably the right thing?guess_channels=Noneis always default but changes behaviour depending on the version number0.15:
Nonebehaves likeTrueto retain the old behaviour. Raise a warning ifNoneorTrueare set.0.16:
Nonebehaves likeFalse.guess_channels=Trueis an invalid. Raise an error if it is set toTrue.guess_channelsis explicitly set toFalse0.17:
guess_channelsis an invalid parameter. Let python deal with the warnings/errors.stefanv commentedon Jun 14, 2018
This looks fine, except that the behavior and 0.15 and 0.16 should be the same when the argument is not specified.
hmaarrfk commentedon Jun 14, 2018
@stefanv, do you think the
guess_channelsparameter should be kept at infinitum? If not, I would like to give the chance for people that set it toFalseto not have their code break between a version upgrade.As such, I think there should be a change in the behaviour of
Noneduring one version bump.NoneFalseTrueguess_channelsTrueguess_channelsTrueguess_channelsFalsestefanv commentedon Jun 14, 2018
Hrm, actually, why introduce a parameter that you mean to deprecate anyway. How about:
0.15 — keep guessing, but add multichannel=None, warn if multichannel is not specified and data looks to be multichannel
0.16 — keep guessing, but error if multichannel is not specified and data looks to be multichannel
0.17 — stop guessing, rely on multichannel
hmaarrfk commentedon Jun 15, 2018
My original thoughts were:
For me, the logic for guessing the multichannel will end up being annoying to maintain and test.
Thinking long term, it doesn't make sense to ask future developers to ensure that it continues to hit corner cases.
For example:
looks at the shape, and looked at dimensions. It doesn't check that
image.dims == np.sqeeze(image).dimsand then indexes the2address assuming that it is the color axis while it could have been squeezed out.Glad to fix it, but I have a feeling that things like this are going to keep popping out.
parekhnish commentedon Jan 24, 2020
For
rgb2gray(), the current documentation defines aValueError:scikit-image/skimage/color/colorconv.py
Lines 764 to 768 in b46a496
But, the following snippet does not produce an error:
Then, should we change the documentation to reflect that, indeed, 2-D images will also not produce an error?
jni commentedon Jan 24, 2020
@parekhnish thanks for chiming in! I guess it’s still unclear what the best way forward is based on the above discussion, but fixing the documentation to be correct is certainly desirable in the short term! Would you like to submit a pull request? We would be happy to guide you through the process if you aren’t familiar with it yet.
Thanks!
parekhnish commentedon Jan 27, 2020
@jni yes, I can do that. Do you have any pull-request guidelines (commit message format, tests to be done before committing and creating the pull request, etc.)?
jni commentedon Jan 27, 2020
Yes, you can read our contributing guidelines here:
https://scikit-image.org/docs/dev/contribute.html
The are a bit on the long side, but I hope quite clear. Since this is a documentation fix, I don't think you will need to do any testing. Our continuous integration will catch any ReST formatting errors if they creep in. =)
rfezzani commentedon Jan 28, 2020
The problem is even worst then it looks:
rgb2grayignores all image's channels with indices greater then 3 and doesn't raise any error if the number of channels is not 3 or 4 (despite what the documentation says:scikit-image/skimage/color/colorconv.py
Lines 764 to 768 in b46a496
The problem comes from this line:
scikit-image/skimage/color/colorconv.py
Line 794 in b46a496
rfezzani commentedon Jan 28, 2020
My solution to this issue is may be a little bit radical: I consider that
multichannelis implicitlyTruefor all the functions butgray2rgb,rgb2graydoes not accept gray level images,rgb2graydoes not accept RGBA imagesI implemented It in #4418. It allows to easily add nD support using
Ellipsisand alleviates all the "guessing"...rfezzani commentedon Jan 28, 2020
Oops, this comment is better suited for #3185