...

/

TF‘s Image Processing Module (Part 2)

TF‘s Image Processing Module (Part 2)

Learn to apply color transformations and color space conversion using TF’s tf.image module.

The TF framework provides us with the tf.image module to perform image processing. This module consists of numerous image processing functions for geometric and color transformations. Let’s explore the functions of the tf.image module for color transformations.

Color transformations

Color transformations change the pixel values of images without altering their geometry. Some of the color transformations are given below.

Brightness adjustment

The tf.image.adjust_brightness function adjusts image brightness according to the value of the delta argument. A delta=0.3 value adds 30% brightness to the image.

The tf.image.random_brightness function adjusts the image brightness using a random factor max_delta. This function picks the delta in the interval [-max_delta, max_delta).

Press + to interact
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds
image_path = '/usr/local/notebooks/datasets/baboon.png'
image = plt.imread(image_path)
adjusted_brightness = tf.image.adjust_brightness(image, delta = 0.1)
random_brightness = tf.image.random_brightness(image, max_delta = 0.2)
# Displaying images
plt.subplot(1, 2, 1)
plt.imshow(tf.clip_by_value(adjusted_brightness, 0, 1)) # Clipping values outside the (0, 1) range
plt.title('Adjusted brightness')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(tf.clip_by_value(random_brightness, 0, 1))
plt.title('Random brightness')
plt.axis('off')
plt.savefig('output/output_image.png', dpi = 300)

Contrast and gamma adjustment

The following are some of the functions of the tf.image module for contrast and gamma adjustment:

  • The tf.image.adjust_contrast function adjusts image contrast by computing the mean values of pixels in each image channel. It uses the contrast_factor argument as (xmean)×contrast ...