albumentations icon indicating copy to clipboard operation
albumentations copied to clipboard

HueSaturationValue with 4-channel RGBA images with transparency

Open JJrodny opened this issue 1 year ago • 0 comments

First of all, I need to say Albumentations is amazing, and I appreciate everything y'all have done! 10/10!

One problem I came to: albumentations.augmentations.transforms.HueSaturationValue only works for RGB or greyscale images

I get the following error when running HueSaturationValue transform over an RGBA image: TypeError: HueSaturationValue transformation expects 1-channel or 3-channel images.

`

def apply(self, image, hue_shift=0, sat_shift=0, val_shift=0, **params):
    if not is_rgb_image(image) and not is_grayscale_image(image):
        raise TypeError("HueSaturationValue transformation expects 1-channel or 3-channel images.")
    return F.shift_hsv(image, hue_shift, sat_shift, val_shift)

` https://github.com/albumentations-team/albumentations/blob/e3b47b3a127f92541cfeb16abbb44a6f8bf79cc8/albumentations/augmentations/transforms.py#L1005C21-L1005C21

I'd like to simply:

  1. Create my transform function with all of my transforms (including HueSaturationValue)
  2. Run the RGBA image through that transform function

If I have multiple transforms is the only solution to:

  1. Convert a copy of RGBA image to RGB image
  2. Create a transform function with a single HueSaturationValue transform
  3. Run the RGB image through that transform function
  4. Apply the mask from the RGBA image on the newly transformed RGB image, creating a new RGBA image with HueSaturationValue transformed
  5. Create another transform function with the rest of our transforms
  6. Run the RGBA image through that second transform function

Can we run the HueSaturationValue on the RGB channels only and ignore the alpha channel?

I haven't tested it yet, but if we could treat it like an np array, could we do something like this?:

`

def apply(self, image, hue_shift=0, sat_shift=0, val_shift=0, **params):
    if not is_rgb_image(image) and not is_grayscale_image(image) and not is_rgba_image(image):
        raise TypeError("HueSaturationValue transformation expects 1-channel, 3-channel, or 4-channel images.")
    if is_rgba_image(image):
         return np.concatenate((F.shift_hsv(image[0:3], hue_shift, sat_shift, val_shift), image[3]), axis=0)
    else:
         return F.shift_hsv(image, hue_shift, sat_shift, val_shift)

`

Of course I don't know albumentations perfectly, maybe there's a good reason why it shouldn't work with RGBA images? Please let me know!

Again, thanks so much for y'alls hard work! Albumentations is amazing!

JJrodny avatar Aug 15 '23 21:08 JJrodny