Signup/Sign In
LAST UPDATED: SEPTEMBER 1, 2021

Random Brightness Image Augmentation - Keras ImageDataGenerator

    Random Brightness Image augmentation is used to generate images with varied brightness levels for feeding our deep learning model. We will be using Keras ImageDataGenerator class, along with providing the brightness_range argument.

    In the previous articles of the Image Augmentation series, we have already covered random rotation data augmentation, horizontal/vertical flip augmentation, and horizontal/vertical shift augmentation, so if you haven't checked these, please do check them out.

    Code: The code for this article, can be downloaded from the following GitHub link: https://github.com/shekharpandey89/data_augmentation

    Random Brightness Image Augmentation

    In this method, we will train the system with different color intensity images, so that the model can generalize the images which are not trained even on different lighting conditions.

    So this method can make the images a little darker, brighter, or both, depending upon the argument we pass to the ImageDataGenerator class constructor. We can achieve this through the parameter brightness_range. We pass the max and min value as a float which treats that value as a percentage to apply to the image.

    The brightness and darkness depend upon one boundary value which is 1.0. If the value less than 1.0 then it will darken the image like [0.5,1.0], whereas a value greater than 1.0 makes the image brighter like [1.0, 2.0].

    Here keep in mind that the value 1.0 has no effect because 1.0 is the boundary of darkness and brightness, which stands for the original image.

    Example 1: Random Brightness Image Augmentation Python Implementation

    To run this program we have to keep the image (any image) in the same folder where you will keep this python file. Because then only it will read the image. If your image is at another location then specify the full path to the load_img(/path/of/the/image) function.

    # python program to demonstrate the brightness of the image with the brightness_range argument
    
    # we import all our required libraries
    from numpy import expand_dims
    from keras.preprocessing.image import load_img
    from keras.preprocessing.image import img_to_array
    from keras.preprocessing.image import ImageDataGenerator
    from matplotlib import pyplot
    
    # we first load the image
    image = load_img('parrot.jpg')
    # we converting the image which is in PIL format into the numpy array, so that we can apply deep learning methods
    dataImage = img_to_array(image)
    # print(dataImage)
    # expanding dimension of the load image
    imageNew = expand_dims(dataImage, 0)
    # now here below we creating the object of the data augmentation class
    imageDataGen = ImageDataGenerator(brightness_range=[0.2,1.0])
    # because as we alreay load image into the memory, so we are using flow() function, to apply transformation
    iterator = imageDataGen.flow(imageNew, batch_size=1)
    # below we generate augmented images and plotting for visualization
    for i in range(9):
    	# we are below define the subplot
    	pyplot.subplot(330 + 1 + i)
    	# generating images of each batch
    	batch = iterator.next()
    	# again we convert back to the unsigned integers value of the image for viewing
    	image = batch[0].astype('uint8')
    	# we plot here raw pixel data
    	pyplot.imshow(image)
    # visualize the the figure
    pyplot.show()

    Line 4 to 8: We are importing our required packages to create our code.

    Line 11: We have loaded the image from our local drive in the variable image.

    Line 13: In this line, we converting the PIL image format to NumPy array so that we can use that it in further image processing.

    Line 16: We have then expanded our NumPy array to axis = 0 which means column side.

    Line 18: We then created the object (imageDataGen) for the class ImageDataGenerator and pass the argument brightness_range = [0.2,1.0]

    Line 20: We have then created the iterator to perform the transformation on the batch.

    Line 22 to 32: Then the iterator is called as per the iteration value and we got our transformed images as shown below in the result.

    Output:

    After executing the above python code, we got the below output as we desired. From the output, we can see that the input image now becomes darker. And all the images are not with the same darkness because the range is from 0.2 to 1.0 and augmentation picks some random value and applies that to the image.

    Random darkness image augmentation example

    After running the code, we got nine different augmented images with different darkness.

    Also Read: Different Methods of Image Augmentation

    Example 2: Random Brightness Image Augmentation Python Implementation

    Now, we are going to create another python program and save that code with the name randombrightness.py. In this program, we are going to pass the value [1.0,2.0] where the value is greater than 1.0. So this code will increase the brightness of the input image. So let's run the code and see the results.

    # python program to demonstrate the brightness of the image with the brightness_range argument
    
    # we import all our required libraries
    from numpy import expand_dims
    from keras.preprocessing.image import load_img
    from keras.preprocessing.image import img_to_array
    from keras.preprocessing.image import ImageDataGenerator
    from matplotlib import pyplot
    
    # we first load the image
    image = load_img('parrot.jpg')
    # we converting the image which is in PIL format into the numpy array, so that we can apply deep learning methods
    dataImage = img_to_array(image)
    # print(dataImage)
    # expanding dimension of the load image
    imageNew = expand_dims(dataImage, 0)
    # now here below we creating the object of the data augmentation class
    imageDataGen = ImageDataGenerator(brightness_range=[1.0,2.0])
    # because as we alreay load image into the memory, so we are using flow() function, to apply transformation
    iterator = imageDataGen.flow(imageNew, batch_size=1)
    # below we generate augmented images and plotting for visualization
    for i in range(9):
    	# we are below define the subplot
    	pyplot.subplot(330 + 1 + i)
    	# generating images of each batch
    	batch = iterator.next()
    	# again we convert back to the unsigned integers value of the image for viewing
    	image = batch[0].astype('uint8')
    	# we plot here raw pixel data
    	pyplot.imshow(image)
    # visualize the the figure
    pyplot.show()

    The code above is similar to the first python program (randomDark.py) just the brightness_range value is changed (on line 18).

    Output:

    We can see the result in the below screenshot all the augmented nine images are bright because we passed the range value greater than 1.0.

    Random brightness Image augmentation example

    Conclusion:

    In this article, we studied random brightness augmentation and now we know how to make the images either brighter or darker depending upon the value range which we pass into the imageDataGenerator object.

    Applying these transformations to the input image will not change the actual class label of the image data. But each transformed image will be treated as a new image that the training model has not seen before, so in this way, we are doing another type of regularization technique that works on the training datasets.

    So let's continue to study data augmentation in the next blog. In the next blog, we are going to explain Random Zoom augmentation.

    You may also like:

    I am an Artificial Intelligence Engineer. Doing research work in Virtual Reality and Augmented Reality. I would like to write article or share my work with others apart from my professional life.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS