plenoptic.process.ssim_map#
- plenoptic.process.ssim_map(img1, img2)[source]#
Structural similarity index map.
As described in Wang et al., 2004 [5], the structural similarity index (SSIM) is a perceptual distance metric, giving the distance between two images. SSIM is based on three comparison measurements between the two images: luminance, contrast, and structure. All of these are computed convolutionally across the images. See the references for more information.
This implementation follows the original implementation, as found online [6], as well as providing the option to use the weighted version used in Wang and Simoncelli, 2008 [8] (which was shown to consistently improve the image quality prediction on the LIVE database). More info can be found online [7].
Note that this is a similarity metric (not a distance), and so 1 means the two images are identical and 0 means they’re very different. When the two images are negatively correlated, SSIM can be negative. SSIM is bounded between -1 and 1.
This function returns the SSIM map, showing the SSIM values across the image. For the mean SSIM (a single value metric), call
ssim.- Parameters:
img1 (
Tensor) – The first image or batch of images, of shape (batch, channel, height, width).img2 (
Tensor) – The second image or batch of images, of shape (batch, channel, height, width). The heights and widths ofimg1andimg2must be the same. The numbers of batches and channels ofimg1andimg2need to be broadcastable: either they are the same or one of them is 1. The output will be computed separately for each channel (so channels are treated in the same way as batches). Both images should have values between 0 and 1. Otherwise, the result may be inaccurate, and we will raise a warning (but will still compute it).
- Return type:
- Returns:
ssim_map – 4d tensor containing the map of SSIM values.
- Raises:
ValueError – If either
img1orimg2is not 4d.ValueError – If
img1andimg2have different height or width.ValueError – If
img1andimg2have different batch or channel, unless one of them has a 1 there, so they can be broadcast.ValueError – If
img1andimg2have different dtypes.
- Warns:
UserWarning – If either
img1orimg2has multiple channels, as SSIM was designed for grayscale images.UserWarning – If at least one scale from either
img1orimg2has height or width of less than 11, since SSIM uses an 11x11 convolutional kernel.
References
Examples
>>> import plenoptic as po >>> import torch >>> po.set_seed(0) >>> img = po.data.einstein() >>> ssim_map = po.process.ssim_map(img, img + torch.rand_like(img)) >>> ssim_map.shape torch.Size([1, 1, 246, 246])