Module: tf.image

Image ops.

The tf.image module contains various functions for image processing and decoding-encoding Ops.

Many of the encoding/decoding functions are also available in the core tf.io module.

Image processing

Resizing

The resizing Ops accept input images as tensors of several types. They always output resized images as float32 tensors.

The convenience function tf.image.resize supports both 4-D and 3-D tensors as input and output. 4-D tensors are for batches of images, 3-D tensors for individual images.

Resized images will be distorted if their original aspect ratio is not the same as size. To avoid distortions see tf.image.resize_with_pad.

The Class tf.image.ResizeMethod provides various resize methods like bilinear, nearest_neighbor.

Converting Between Colorspaces

Image ops work either on individual images or on batches of images, depending on the shape of their input Tensor.

If 3-D, the shape is [height, width, channels], and the Tensor represents one image. If 4-D, the shape is [batch_size, height, width, channels], and the Tensor represents batch_size images.

Currently, channels can usefully be 1, 2, 3, or 4. Single-channel images are grayscale, images with 3 channels are encoded as either RGB or HSV. Images with 2 or 4 channels include an alpha channel, which has to be stripped from the image before passing the image to most image processing functions (and can be re-attached later).

Internally, images are either stored in as one float32 per channel per pixel (implicitly, values are assumed to lie in [0,1)) or one uint8 per channel per pixel (values are assumed to lie in [0,255]).

TensorFlow can convert between images in RGB or HSV or YIQ.

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.

Working with Bounding Boxes

Cropping

Flipping, Rotating and Transposing

Image decoding and encoding

TensorFlow provides Ops to decode and encode JPEG and PNG formats. Encoded images are represented by scalar string Tensors, decoded images by 3-D uint8 tensors of shape [height, width, channels]. (PNG also supports uint16.)

Note: decode_gif returns a 4-D array [num_frames, height, width, 3]

The encode and decode Ops apply to one image at a time. Their input and output are all of variable size. If you need fixed size images, pass the output of the decode Ops to one of the cropping and resizing Ops.

Classes

class ResizeMethod: See tf.image.resize for details.

Functions

adjust_brightness(...): Adjust the brightness of RGB or Grayscale images.

adjust_contrast(...): Adjust contrast of RGB or grayscale images.

adjust_gamma(...): Performs Gamma Correction.

adjust_hue(...): Adjust hue of RGB images.

adjust_jpeg_quality(...): Adjust jpeg encoding quality of an image.

adjust_saturation(...): Adjust saturation of RGB images.

central_crop(...): Crop the central region of the image(s).

combined_non_max_suppression(...): Greedily selects a subset of bounding boxes in descending order of score.

convert_image_dtype(...): Convert image to dtype, scaling its values if needed.

crop_and_resize(...): Extracts crops from the input image tensor and resizes them.

crop_to_bounding_box(...): Crops an image to a specified bounding box.

decode_and_crop_jpeg(...): Decode and Crop a JPEG-encoded image to a uint8 tensor.

decode_bmp(...): Decode the first frame of a BMP-encoded image to a uint8 tensor.

decode_gif(...): Decode the frame(s) of a GIF-encoded image to a uint8 tensor.

decode_image(...): Function for decode_bmp, decode_gif, decode_jpeg, and decode_png.

decode_jpeg(...): Decode a JPEG-encoded image to a uint8 tensor.

decode_png(...): Decode a PNG-encoded image to a uint8 or uint16 tensor.

draw_bounding_boxes(...): Draw bounding boxes on a batch of images.

encode_jpeg(...): JPEG-encode an image.

encode_png(...): PNG-encode an image.

extract_glimpse(...): Extracts a glimpse from the input tensor.

extract_jpeg_shape(...): Extract the shape information of a JPEG-encoded image.

extract_patches(...): Extract patches from images.

flip_left_right(...): Flip an image horizontally (left to right).

flip_up_down(...): Flip an image vertically (upside down).

generate_bounding_box_proposals(...): Generate bounding box proposals from encoded bounding boxes.

grayscale_to_rgb(...): Converts one or more images from Grayscale to RGB.

hsv_to_rgb(...): Convert one or more images from HSV to RGB.

image_gradients(...): Returns image gradients (dy, dx) for each color channel.

is_jpeg(...): Convenience function to check if the 'contents' encodes a JPEG image.

non_max_suppression(...): Greedily selects a subset of bounding boxes in descending order of score.

non_max_suppression_overlaps(...): Greedily selects a subset of bounding boxes in descending order of score.

non_max_suppression_padded(...): Greedily selects a subset of bounding boxes in descending order of score.

non_max_suppression_with_scores(...): Greedily selects a subset of bounding boxes in descending order of score.

pad_to_bounding_box(...): Pad image with zeros to the specified height and width.

per_image_standardization(...): Linearly scales each image in image to have mean 0 and variance 1.

psnr(...): Returns the Peak Signal-to-Noise Ratio between a and b.

random_brightness(...): Adjust the brightness of images by a random factor.

random_contrast(...): Adjust the contrast of an image or images by a random factor.

random_crop(...): Randomly crops a tensor to a given size.

random_flip_left_right(...): Randomly flip an image horizontally (left to right).

random_flip_up_down(...): Randomly flips an image vertically (upside down).

random_hue(...): Adjust the hue of RGB images by a random factor.

random_jpeg_quality(...): Randomly changes jpeg encoding quality for inducing jpeg noise.

random_saturation(...): Adjust the saturation of RGB images by a random factor.

resize(...): Resize images to size using the specified method.

resize_with_crop_or_pad(...): Crops and/or pads an image to a target width and height.

resize_with_pad(...): Resizes and pads an image to a target width and height.

rgb_to_grayscale(...): Converts one or more images from RGB to Grayscale.

rgb_to_hsv(...): Converts one or more images from RGB to HSV.

rgb_to_yiq(...): Converts one or more images from RGB to YIQ.

rgb_to_yuv(...): Converts one or more images from RGB to YUV.

rot90(...): Rotate image(s) counter-clockwise by 90 degrees.

sample_distorted_bounding_box(...): Generate a single randomly distorted bounding box for an image.

sobel_edges(...): Returns a tensor holding Sobel edge maps.

ssim(...): Computes SSIM index between img1 and img2.

ssim_multiscale(...): Computes the MS-SSIM between img1 and img2.

stateless_random_brightness(...): Adjust the brightness of images by a random factor deterministically.

stateless_random_contrast(...): Adjust the contrast of images by a random factor deterministically.

stateless_random_crop(...): Randomly crops a tensor to a given size in a deterministic manner.

stateless_random_flip_left_right(...): Randomly flip an image horizontally (left to right) deterministically.

stateless_random_flip_up_down(...): Randomly flip an image vertically (upside down) deterministically.

stateless_random_hue(...): Adjust the hue of RGB images by a random factor deterministically.

stateless_random_jpeg_quality(...): Deterministically radomize jpeg encoding quality for inducing jpeg noise.

stateless_random_saturation(...): Adjust the saturation of RGB images by a random factor deterministically.

stateless_sample_distorted_bounding_box(...): Generate a randomly distorted bounding box for an image deterministically.

total_variation(...): Calculate and return the total variation for one or more images.

transpose(...): Transpose image(s) by swapping the height and width dimension.

yiq_to_rgb(...): Converts one or more images from YIQ to RGB.

yuv_to_rgb(...): Converts one or more images from YUV to RGB.

© 2020 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 3.0.
Code samples licensed under the Apache 2.0 License.
https://www.tensorflow.org/versions/r2.4/api_docs/python/tf/image