albumentations icon indicating copy to clipboard operation
albumentations copied to clipboard

ambiguity of albumentations.ToGray()

Open defensetongxue opened this issue 2 years ago • 2 comments

🐛 Bug

albumentations.augmentations.transforms.ToGray return a 3 channels image from

# albumentations/albumentations/augmentations/funtional.py
def to_gray(img):
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    return cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)

which means this funtion repeat the gray scale channel for 3 times. i thinl it would be suitable to return 'gray' directly or add some annotation to disambiguation

To Reproduce

Steps to reproduce the behavior:

  1. call the albumentations.ToGray() for any 3 channnel image

Expected behavior

defensetongxue avatar Feb 26 '23 06:02 defensetongxue

Maybe an argument to specify 1/3 channels to return would be enough?

def to_gray(img, gray2rgb=False):
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    if gray2rgb:
        return cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
    else:
        return gray

mikel-brostrom avatar Mar 04 '23 18:03 mikel-brostrom

May be it can refer to the torchvision.transforms.Grayscale(num_output_channels=1)

def to_gray(img, num_output_channels=3):
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    if num_output_channels==3:
        return cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
    elif num_output_channel==1:
        return gray
    else:
        raise ImplementError# rasie an error

By setting the default argument 'num_output_channels' to 3, we can maintain the same visualization as before.

defensetongxue avatar Mar 10 '23 04:03 defensetongxue