When opening RGB image in grayscale, can 2 colors be collapsed in the same gray level?

I am working on Audi A2D2 dataset 2D segmentation.
The segmentation mask are provided as 3-channel, RGB images (despite having only 55 classes).
In order to process the labels faster in my data loader, I’d like to open them as grayscale even though they are RGB. Because there are only 55 different RGB pixel values, it should be possible to convert to gray scale and keeping unique pixel value for the 55 classes, since there are 256 possible shades of grey.

from torchvision.io import read_image
from torchvision.io.image import ImageReadMode

read_image(path, mode=ImageReadMode.RGB)

I’m wondering: is there a risk of 2 RGB pixel being assigned the same grayscale value, when opening an RGB picture with ImageReadMode.GRAY?

Hi Olivier!

Yes, the risk is real, and such an occurrence is in fact quite likely: See the
so-called “birthday paradox.”

If the person who prepared the segmentation masks were kind, then he
could have purposely chosen RGB values that map to distinct grayscale
values. (And if he were diabolical, he could have chosen 55 RGB values
that all map to the same grayscale value.) But if the RGB values were
chosen somewhat “randomly,” then you will likely have such a collision.

So … Do you feel lucky?

Best.

K. Frank

:joy: haha I understand thanks @KFrank ! I think I’ll pre-process myself once before the training (now it’s done in the getitem on the fly and taking up a lot of CPU)