ElvinFox
ElvinFox

Reputation: 71

Change colors with ImageDataGenerator

I'm using keras ImageDataGenerator for preprocessing training images and need some kind of color change function (random color, hue change).

My code for generator looks like this:

image_generator = tf.keras.preprocessing.image.ImageDataGenerator(                                 
                              horizontal_flip = True,
                              brightness_range= [0.7, 1.3],
                              rotation_range = 10,
                              zoom_range = [0.8, 1.2],
                              width_shift_range=0.2,
                              height_shift_range=0.2,
                             fill_mode="nearest")

I tried to go throught keras manual for datagerator and the best i found was - channel_shift_range but it works more like brightness/contrast.

enter image description here

Upvotes: 4

Views: 6885

Answers (2)

Aaditya Vikram
Aaditya Vikram

Reputation: 21

Found something while reading an article on Medium. It might be of help :-

import cv2
import numpy as np
from PIL import Image
def myFunc(image):
    image = np.array(image)
    hsv_image = cv2.cvtColor(image,cv2.COLOR_RGB2HSV)
    return Image.fromarray(hsv_image)

train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True,
    preprocessing_function = myFunc
    )

Source :-

https://medium.com/@halmubarak/changing-color-space-hsv-lab-while-reading-from-directory-in-keras-c8ca243e2d57

Upvotes: 0

Baptiste Pouthier
Baptiste Pouthier

Reputation: 572

Maybe this can help. You can define a customize function to use it in the ImageDataGenerator in order to modify the image colors.

For example:

import cv2
import numpy as np
from PIL import Image

def myFunc(image):
    image = np.array(image)
    hsv_image = cv2.cvtColor(image,cv2.COLOR_RGB2HSV)
    return Image.fromarray(hsv_image)

train_datagen = ImageDataGenerator(
                rescale=1. / 255,
                rotation_range=20,
                width_shift_range=0.2,
                height_shift_range=0.2,
                horizontal_flip=True,
                preprocessing_function = myFunc
                )

Upvotes: 1

Related Questions