This function is based on the standard SSIM implementation from:
Wang, Z., Bovik, A. C., Sheikh, H. R., & Simoncelli, E. P. (2004). Image
quality assessment: from error visibility to structural similarity. IEEE
transactions on image processing.
Details
11x11 Gaussian filter of width 1.5 is used.
k1 = 0.01, k2 = 0.03 as in the original paper.
The image sizes must be at least 11x11 because of the filter size.
Example:
# Read images (of size 255 x 255) from file.im1=tf.image.decode_image(tf.io.read_file('path/to/im1.png'))im2=tf.image.decode_image(tf.io.read_file('path/to/im2.png'))tf.shape(im1)# `img1.png` has 3 channels; shape is `(255, 255, 3)`tf.shape(im2)# `img2.png` has 3 channels; shape is `(255, 255, 3)`# Add an outer batch for each image.im1=tf.expand_dims(im1,axis=0)im2=tf.expand_dims(im2,axis=0)# Compute SSIM over tf.uint8 Tensors.ssim1=tf.image.ssim(im1,im2,max_val=255,filter_size=11,filter_sigma=1.5,k1=0.01,k2=0.03)# Compute SSIM over tf.float32 Tensors.im1=tf.image.convert_image_dtype(im1,tf.float32)im2=tf.image.convert_image_dtype(im2,tf.float32)ssim2=tf.image.ssim(im1,im2,max_val=1.0,filter_size=11,filter_sigma=1.5,k1=0.01,k2=0.03)# ssim1 and ssim2 both have type tf.float32 and are almost equal.
Args
img1
First image batch. 4-D Tensor of shape [batch, height, width,
channels] with only Positive Pixel Values.
img2
Second image batch. 4-D Tensor of shape [batch, height, width,
channels] with only Positive Pixel Values.
max_val
The dynamic range of the images (i.e., the difference between the
maximum the and minimum allowed values).
filter_size
Default value 11 (size of gaussian filter).
filter_sigma
Default value 1.5 (width of gaussian filter).
k1
Default value 0.01
k2
Default value 0.03 (SSIM is less sensitivity to K2 for lower values, so
it would be better if we took the values in the range of 0 < K2 < 0.4).
return_index_map
If True returns local SSIM map instead of the global mean.
Returns
A tensor containing an SSIM value for each image in batch or a tensor
containing an SSIM value for each pixel for each image in batch if
return_index_map is True. Returned SSIM values are in range (-1, 1], when
pixel values are non-negative. Returns a tensor with shape:
broadcast(img1.shape[:-3], img2.shape[:-3]) or broadcast(img1.shape[:-1],
img2.shape[:-1]).
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.image.ssim\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/image_ops_impl.py#L4385-L4476) |\n\nComputes SSIM index between img1 and img2.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.image.ssim`](https://www.tensorflow.org/api_docs/python/tf/image/ssim)\n\n\u003cbr /\u003e\n\n tf.image.ssim(\n img1,\n img2,\n max_val,\n filter_size=11,\n filter_sigma=1.5,\n k1=0.01,\n k2=0.03,\n return_index_map=False\n )\n\nThis function is based on the standard SSIM implementation from:\nWang, Z., Bovik, A. C., Sheikh, H. R., \\& Simoncelli, E. P. (2004). Image\nquality assessment: from error visibility to structural similarity. IEEE\ntransactions on image processing.\n| **Note:** The true SSIM is only defined on grayscale. This function does not perform any colorspace transform. (If the input is already YUV, then it will compute YUV SSIM average.)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Details ------- ||\n|---|---|\n| \u003cbr /\u003e - 11x11 Gaussian filter of width 1.5 is used. - k1 = 0.01, k2 = 0.03 as in the original paper. ||\n\n\u003cbr /\u003e\n\nThe image sizes must be at least 11x11 because of the filter size.\n\n#### Example:\n\n # Read images (of size 255 x 255) from file.\n im1 = tf.image.decode_image(tf.io.read_file('path/to/im1.png'))\n im2 = tf.image.decode_image(tf.io.read_file('path/to/im2.png'))\n tf.shape(im1) # `img1.png` has 3 channels; shape is `(255, 255, 3)`\n tf.shape(im2) # `img2.png` has 3 channels; shape is `(255, 255, 3)`\n # Add an outer batch for each image.\n im1 = tf.expand_dims(im1, axis=0)\n im2 = tf.expand_dims(im2, axis=0)\n # Compute SSIM over tf.uint8 Tensors.\n ssim1 = tf.image.ssim(im1, im2, max_val=255, filter_size=11,\n filter_sigma=1.5, k1=0.01, k2=0.03)\n\n # Compute SSIM over tf.float32 Tensors.\n im1 = tf.image.convert_image_dtype(im1, tf.float32)\n im2 = tf.image.convert_image_dtype(im2, tf.float32)\n ssim2 = tf.image.ssim(im1, im2, max_val=1.0, filter_size=11,\n filter_sigma=1.5, k1=0.01, k2=0.03)\n # ssim1 and ssim2 both have type tf.float32 and are almost equal.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|\n| `img1` | First image batch. 4-D Tensor of shape `[batch, height, width, channels]` with only Positive Pixel Values. |\n| `img2` | Second image batch. 4-D Tensor of shape `[batch, height, width, channels]` with only Positive Pixel Values. |\n| `max_val` | The dynamic range of the images (i.e., the difference between the maximum the and minimum allowed values). |\n| `filter_size` | Default value 11 (size of gaussian filter). |\n| `filter_sigma` | Default value 1.5 (width of gaussian filter). |\n| `k1` | Default value 0.01 |\n| `k2` | Default value 0.03 (SSIM is less sensitivity to K2 for lower values, so it would be better if we took the values in the range of 0 \\\u003c K2 \\\u003c 0.4). |\n| `return_index_map` | If True returns local SSIM map instead of the global mean. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A tensor containing an SSIM value for each image in batch or a tensor containing an SSIM value for each pixel for each image in batch if return_index_map is True. Returned SSIM values are in range (-1, 1\\], when pixel values are non-negative. Returns a tensor with shape: broadcast(img1.shape\\[:-3\\], img2.shape\\[:-3\\]) or broadcast(img1.shape\\[:-1\\], img2.shape\\[:-1\\]). ||\n\n\u003cbr /\u003e"]]