Image Adjustments

TensorFlow provides functions to adjust images in various ways: brightness, contrast, hue, and saturation. Each adjustment can be done with predefined parameters or with random parameters picked from predefined intervals. Random adjustments are often useful to expand a training set and reduce overfitting.

If several adjustments are chained it is advisable to minimize the number of redundant conversions by first converting the images to the most natural data type and representation (RGB or HSV).

tf.image.adjust_brightness(image, delta)

Adjust the brightness of RGB or Grayscale images.

This is a convenience method that converts an RGB image to float representation, adjusts its brightness, and then converts it back to the original data type. If several adjustments are chained it is advisable to minimize the number of redundant conversions.

The value delta is added to all components of the tensor image. Both image and delta are converted to float before adding (and image is scaled appropriately if it is in fixed-point representation). For regular images, delta should be in the range [0,1), as it is added to the image in floating point representation, where pixel values are in the [0,1) range.

Args:
  • image: A tensor.
  • delta: A scalar. Amount to add to the pixel values.
Returns:

A brightness-adjusted tensor of the same shape and type as image.


tf.image.random_brightness(image, max_delta, seed=None)

Adjust the brightness of images by a random factor.

Equivalent to adjust_brightness() using a delta randomly picked in the interval [-max_delta, max_delta).

Args:
  • image: An image.
  • max_delta: float, must be non-negative.
  • seed: A Python integer. Used to create a random seed. See set_random_seed for behavior.
Returns:

The brightness-adjusted image.

Raises:
  • ValueError: if max_delta is negative.

tf.image.adjust_contrast(images, contrast_factor)

Adjust contrast of RGB or grayscale images.

This is a convenience method that converts an RGB image to float representation, adjusts its contrast, and then converts it back to the original data type. If several adjustments are chained it is advisable to minimize the number of redundant conversions.

images is a tensor of at least 3 dimensions. The last 3 dimensions are interpreted as [height, width, channels]. The other dimensions only represent a collection of images, such as [batch, height, width, channels].

Contrast is adjusted independently for each channel of each image.

For each channel, this Op computes the mean of the image pixels in the channel and then adjusts each component x of each pixel to (x - mean) * contrast_factor + mean.

Args:
  • images: Images to adjust. At least 3-D.
  • contrast_factor: A float multiplier for adjusting contrast.
Returns:

The contrast-adjusted image or images.


tf.image.random_contrast(image, lower, upper, seed=None)

Adjust the contrast of an image by a random factor.

Equivalent to adjust_contrast() but uses a contrast_factor randomly picked in the interval [lower, upper].

Args:
  • image: An image tensor with 3 or more dimensions.
  • lower: float. Lower bound for the random contrast factor.
  • upper: float. Upper bound for the random contrast factor.
  • seed: A Python integer. Used to create a random seed. See set_random_seed for behavior.
Returns:

The contrast-adjusted tensor.

Raises:
  • ValueError: if upper <= lower or if lower < 0.

tf.image.adjust_hue(image, delta, name=None)

Adjust hue of an RGB image.

This is a convenience method that converts an RGB image to float representation, converts it to HSV, add an offset to the hue channel, converts back to RGB and then back to the original data type. If several adjustments are chained it is advisable to minimize the number of redundant conversions.

image is an RGB image. The image hue is adjusted by converting the image to HSV and rotating the hue channel (H) by delta. The image is then converted back to RGB.

delta must be in the interval [-1, 1].

Args:
  • image: RGB image or images. Size of the last dimension must be 3.
  • delta: float. How much to add to the hue channel.
  • name: A name for this operation (optional).
Returns:

Adjusted image(s), same shape and DType as image.


tf.image.random_hue(image, max_delta, seed=None)

Adjust the hue of an RGB image by a random factor.

Equivalent to adjust_hue() but uses a delta randomly picked in the interval [-max_delta, max_delta].

max_delta must be in the interval [0, 0.5].

Args:
  • image: RGB image or images. Size of the last dimension must be 3.
  • max_delta: float. Maximum value for the random delta.
  • seed: An operation-specific seed. It will be used in conjunction with the graph-level seed to determine the real seeds that will be used in this operation. Please see the documentation of set_random_seed for its interaction with the graph-level random seed.
Returns:

3-D float tensor of shape [height, width, channels].

Raises:
  • ValueError: if max_delta is invalid.

tf.image.adjust_saturation(image, saturation_factor, name=None)

Adjust saturation of an RGB image.

This is a convenience method that converts an RGB image to float representation, converts it to HSV, add an offset to the saturation channel, converts back to RGB and then back to the original data type. If several adjustments are chained it is advisable to minimize the number of redundant conversions.

image is an RGB image. The image saturation is adjusted by converting the image to HSV and multiplying the saturation (S) channel by saturation_factor and clipping. The image is then converted back to RGB.

Args:
  • image: RGB image or images. Size of the last dimension must be 3.
  • saturation_factor: float. Factor to multiply the saturation by.
  • name: A name for this operation (optional).
Returns:

Adjusted image(s), same shape and DType as image.


tf.image.random_saturation(image, lower, upper, seed=None)

Adjust the saturation of an RGB image by a random factor.

Equivalent to adjust_saturation() but uses a saturation_factor randomly picked in the interval [lower, upper].

Args:
  • image: RGB image or images. Size of the last dimension must be 3.
  • lower: float. Lower bound for the random saturation factor.
  • upper: float. Upper bound for the random saturation factor.
  • seed: An operation-specific seed. It will be used in conjunction with the graph-level seed to determine the real seeds that will be used in this operation. Please see the documentation of set_random_seed for its interaction with the graph-level random seed.
Returns:

Adjusted image(s), same shape and DType as image.

Raises:
  • ValueError: if upper <= lower or if lower < 0.

tf.image.per_image_whitening(image)

Linearly scales image to have zero mean and unit norm.

This op computes (x - mean) / adjusted_stddev, where mean is the average of all values in image, and adjusted_stddev = max(stddev, 1.0/sqrt(image.NumElements())).

stddev is the standard deviation of all values in image. It is capped away from zero to protect against division by 0 when handling uniform images.

Note that this implementation is limited: It only whitens based on the statistics of an individual image. It does not take into account the covariance structure.

Args:
  • image: 3-D tensor of shape [height, width, channels].
Returns:

The whitened image with same shape as image.

Raises:
  • ValueError: if the shape of 'image' is incompatible with this function.