ssim
Structural similarity (SSIM) index for measuring image quality
Description
calculates the SSIM, using name-value pairs to control aspects of the
computation.ssimval
= ssim(A
,ref
,Name,Value
)
Examples
Calculate Structural Similarity Index (SSIM)
Read an image into the workspace. Create another version of the image, applying a blurring filter.
ref = imread("pout.tif"); H = fspecial("Gaussian",[11 11],1.5); A = imfilter(ref,H,"replicate");
Display both images as a montage. The images differ most along sharp high-contrast regions, such as the edges of the trellis.
montage({ref,A})
title("Reference Image (Left) vs. Blurred Image (Right)")
Calculate the global SSIM value for the image and local SSIM values for each pixel.
[ssimval,ssimmap] = ssim(A,ref);
Display the local SSIM map. Include the global SSIM value in the figure title. Small values of local SSIM appear as dark pixels in the local SSIM map. Regions with small local SSIM value correspond to areas where the blurred image noticeably differs from the reference image. Large values of local SSIM value appear as bright pixels. Regions with large local SSIM correspond to uniform regions of the reference image, where blurring has less of an impact on the image.
imshow(ssimmap,[])
title("Local SSIM Map with Global SSIM Value: "+ssimval)
Calculate SSIM for dlarray
Input
Read an image into the workspace. Create another version of the image, applying a blurring filter.
ref = imread("pout.tif"); A = imgaussfilt(ref,1.5,FilterSize=11,Padding="replicate");
Display both images as a montage.
montage({ref,A})
title("Reference Image (Left) vs. Blurred Image (Right)")
Simulate batches of images by replicating the reference image and the blurred image 16 times along the 4th dimension.
A = repmat(A,[1 1 1 16]); ref = repmat(ref,[1 1 1 16]);
Create formatted dlarray
objects for the reference image batch and the blurred image batch. The format is "SSCB
", for spatial-spatial-channel-batch.
A = dlarray(single(A),"SSCB"); ref = dlarray(single(ref),"SSCB");
Calculate the global SSIM value for the image and local SSIM values for each pixel. ssimVal
returns a scalar SSIM value for each image in the batch. ssimMap
returns a map of SSIM values, the same size as the image, for each image in the batch.
[ssimVal,ssimMap] = ssim(A,ref); size(ssimVal)
ans = 1×4
1 1 1 16
size(ssimMap)
ans = 1×4
291 240 1 16
Input Arguments
A
— Image for quality measurement
numeric array | dlarray
object
Image for quality measurement, specified as a numeric array or a dlarray
(Deep Learning Toolbox) object. If A
is not a 2-D
grayscale image or 3-D grayscale volume, such as an RGB image or stack of
grayscale images, specify the DataFormat
name-value
argument. Do not specify the DataFormat
name-value
argument if A
is a formatted dlarray
object.
Data Types: single
| double
| int16
| uint8
| uint16
ref
— Reference image
numeric array | dlarray
object
Reference image against which to measure quality, specified as a numeric
array or a dlarray
(Deep Learning Toolbox) object of the same size and data type as
A
. If ref
is not a 2-D
grayscale image or 3-D grayscale volume, such as an RGB image or stack of
grayscale images, specify the DataFormat
name-value
argument. Do not specify the DataFormat
name-value
argument if ref
is a formatted dlarray
object.
Data Types: single
| double
| int16
| uint8
| uint16
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: ssim(A,ref,"DynamicRange",100)
DataFormat
— Dimension labels
string scalar | character vector
Dimension labels of the input images A
and
ref
, specified as a string scalar or character
vector. Each character in DataFormat
must be one of
these labels:
S
— SpatialC
— ChannelB
— Batch observations
The format cannot include more than one channel label or batch label.
Do not specify the DataFormat
name-value argument
when the input images are formatted dlarray
objects.
Example: "SSC"
indicates that the array has two
spatial dimensions and one channel dimension, appropriate for 2-D RGB
image data.
Example: "SSCB"
indicates that the array has two
spatial dimensions, one channel dimension, and one batch dimension,
appropriate for a sequence of 2-D RGB image data.
Data Types: char
| string
DynamicRange
— Dynamic range of the input image
diff(getrangefromclass(A))
(default) | positive scalar
Dynamic range of the input image, specified as a positive scalar. The default value of
"DynamicRange"
depends on the data type of image
A
, and is calculated as diff(
. For example, the
default dynamic range is getrangefromclass
(A))255
for images of data type
uint8
, and the default is 1
for images of data type double
or
single
with pixel values in the range [0,
1].
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
Exponents
— Exponents for luminance, contrast, and structural terms
[1 1 1]
(default) | 3-element vector of nonnegative numbers
Exponents for the luminance, contrast, and structural terms, specified
as a 3-element vector of nonnegative numbers of the form [alpha
beta gamma]
.
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
Radius
— Standard deviation of isotropic Gaussian function
1.5
(default) | positive number
Standard deviation of isotropic Gaussian function, specified as a positive number. This value is used for weighting the neighborhood pixels around a pixel for estimating local statistics. This weighting is used to avoid blocking artifacts in estimating local statistics.
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
RegularizationConstants
— Regularization constants for luminance, contrast, and structural terms
3-element vector of nonnegative numbers
Regularization constants for the luminance, contrast, and structural
terms, specified as a 3-element vector of nonnegative numbers of the
form [c1 c2 c3]
. The ssim
function
uses these regularization constants to avoid instability for image
regions where the local mean or standard deviation is close to zero.
Therefore, small non-zero values should be used for these
constants.
By default,
C1 = (0.01*L).^2
, whereL
is the specifiedDynamicRange
value.C2 = (0.03*L).^2
, whereL
is the specifiedDynamicRange
value.C3 = C2/2
Data Types: single
| double
| int8
| int16
| int32
| uint8
| uint16
| uint32
Output Arguments
ssimval
— SSIM index
numeric scalar | numeric array | dlarray
object
SSIM index, returned as one of these values.
Input Image Type | SSIM Value |
---|---|
| Numeric scalar with a single SSIM measurement. |
| Scalar |
| Numeric array of the same dimensionality as the input
images. The spatial dimensions of
ssimval are singleton
dimensions. There is one SSIM measurement for each
element along any channel or batch dimension. |
| dlarray object of the same
dimensionality as the input images. The spatial
dimensions of ssimval are singleton
dimensions. There is one SSIM measurement for each
element along any channel or batch dimension. |
ssimval
is of data type double
except when A
is of data type
single
, in which case ssimval
is of
data type single
.
The value of ssimval
is typically in the range [0,
1]. The value 1 indicates the highest quality and occurs when
A
and ref
are equivalent.
Smaller values correspond to poorer quality. For some combinations of inputs
and name-value pair arguments, ssimval
can be negative.
ssimmap
— Local values of SSIM index
numeric array | dlarray
object
Local values of the SSIM index, returned as one of these values.
Input Image Type | SSIM Value |
---|---|
| Numeric array the same size as the input images. There is one SSIM measurement for each element in the input image. |
| dlarray object the same size as the
input images. There is one SSIM measurement for each
element in the input image. |
| Numeric array the same size as the input images. Each spatial element in the input image has an SSIM measurement along any channel or batch dimension. |
| dlarray object the same size as the
input images. Each spatial element in the input image
has an SSIM measurement along any channel or batch
dimension. |
ssimmap
is of data type double
except when A
is of data type
single
, in which case ssimmap
is of
data type single
.
More About
Structural Similarity Index
An image quality metric that assesses the visual impact of three characteristics of an image: luminance, contrast and structure.
Tips
Algorithms
The SSIM Index quality assessment index is based on the computation of three terms, namely the luminance term, the contrast term and the structural term. The overall index is a multiplicative combination of the three terms.
where
where μx, μy,
σx,σy, and σxy
are the local means, standard deviations, and cross-covariance for images x,
y. If α = β = γ = 1 (the default for Exponents
), and
C3 = C2/2 (default selection of
C3) the index simplifies to:
When you specify a noninteger value for "Exponents"
, the
ssim
function prevents complex valued outputs by clamping the
intermediate luminance, contrast, and structural terms to the range [0,
inf
].
References
[1] Zhou, W., A. C. Bovik, H. R. Sheikh, and E. P. Simoncelli. "Image Quality Assessment: From Error Visibility to Structural Similarity." IEEE Transactions on Image Processing. Vol. 13, Issue 4, April 2004, pp. 600–612.
Extended Capabilities
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Image Processing on a GPU.
Version History
Introduced in R2014aR2021b: Support for GPU acceleration
ssim
now supports GPU acceleration
(requires Parallel Computing Toolbox™).
R2021b: Support for deep learning arrays and specifying dimensions of computation
The ssim
function accepts dlarray
input for
deep learning applications.
This function also supports formatted data with dimension labels of
'S'
(spatial), 'C'
(channel), and
'B'
(batch). The function returns a separate result for each
index along the channel and batch dimensions.
See Also
psnr
| immse
| multissim
| multissim3
Topics
- Compare Image Quality at Various Compression Levels
- List of Functions with dlarray Support (Deep Learning Toolbox)
- Define Custom Training Loops, Loss Functions, and Networks (Deep Learning Toolbox)
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)