Image quality#
- kornia.metrics.psnr(image, target, max_val)[source]#
Create a function that calculates the PSNR between 2 images.
PSNR is Peek Signal to Noise Ratio, which is similar to mean squared error. Given an m x n image, the PSNR is:
\[\text{PSNR} = 10 \log_{10} \bigg(\frac{\text{MAX}_I^2}{MSE(I,T)}\bigg)\]where
\[\text{MSE}(I,T) = \frac{1}{mn}\sum_{i=0}^{m-1}\sum_{j=0}^{n-1} [I(i,j) - T(i,j)]^2\]and \(\text{MAX}_I\) is the maximum possible input value (e.g for floating point images \(\text{MAX}_I=1\)).
- Parameters:
- Return type:
- Returns:
the computed loss as a scalar.
Examples
>>> ones = torch.ones(1) >>> psnr(ones, 1.2 * ones, 2.) # 10 * log(4/((1.2-1)**2)) / log(10) tensor(20.0000)
- kornia.metrics.ssim(img1, img2, window_size, max_val=1.0, eps=1e-12, padding='same')[source]#
Compute the Structural Similarity (SSIM) index map between two images.
Measures the (SSIM) index between each element in the input x and target y.
The index can be described as:
\[\text{SSIM}(x, y) = \frac{(2\mu_x\mu_y+c_1)(2\sigma_{xy}+c_2)} {(\mu_x^2+\mu_y^2+c_1)(\sigma_x^2+\sigma_y^2+c_2)}\]- where:
\(c_1=(k_1 L)^2\) and \(c_2=(k_2 L)^2\) are two variables to stabilize the division with weak denominator.
\(L\) is the dynamic range of the pixel-values (typically this is \(2^{\#\text{bits per pixel}}-1\)).
- Parameters:
img1 (
Tensor) – the first input image with shape \((B, C, H, W)\).img2 (
Tensor) – the second input image with shape \((B, C, H, W)\).window_size (
int) – the size of the gaussian kernel to smooth the images.max_val (
float, optional) – the dynamic range of the images. Default:1.0eps (
float, optional) – Small value for numerically stability when dividing. Default:1e-12padding (
str, optional) –'same'|'valid'. Whether to only use the “valid” convolution area to compute SSIM to match the MATLAB implementation of original SSIM paper. Default:"same"
- Return type:
- Returns:
The ssim index map with shape \((B, C, H, W)\).
Examples
>>> input1 = torch.rand(1, 4, 5, 5) >>> input2 = torch.rand(1, 4, 5, 5) >>> ssim_map = ssim(input1, input2, 5) # 1x4x5x5
- kornia.metrics.ssim3d(img1, img2, window_size, max_val=1.0, eps=1e-12, padding='same')[source]#
Compute the Structural Similarity (SSIM) index map between two images.
Measures the (SSIM) index between each element in the input x and target y.
The index can be described as:
\[\text{SSIM}(x, y) = \frac{(2\mu_x\mu_y+c_1)(2\sigma_{xy}+c_2)} {(\mu_x^2+\mu_y^2+c_1)(\sigma_x^2+\sigma_y^2+c_2)}\]- torch.where:
\(c_1=(k_1 L)^2\) and \(c_2=(k_2 L)^2\) are two variables to stabilize the division with weak denominator.
\(L\) is the dynamic range of the pixel-values (typically this is \(2^{\#\text{bits per pixel}}-1\)).
- Parameters:
img1 (
Tensor) – the first input image with shape \((B, C, D, H, W)\).img2 (
Tensor) – the second input image with shape \((B, C, D, H, W)\).window_size (
int) – the size of the gaussian kernel to smooth the images.max_val (
float, optional) – the dynamic range of the images. Default:1.0eps (
float, optional) – Small value for numerically stability when dividing. Default:1e-12padding (
str, optional) –'same'|'valid'. Whether to only use the “valid” convolution area to compute SSIM to match the MATLAB implementation of original SSIM paper. Default:"same"
- Return type:
- Returns:
The ssim index map with shape \((B, C, D, H, W)\).
Examples
>>> input1 = torch.rand(1, 4, 5, 5, 5) >>> input2 = torch.rand(1, 4, 5, 5, 5) >>> ssim_map = ssim3d(input1, input2, 5) # 1x4x5x5x5
- class kornia.metrics.SSIM(window_size, max_val=1.0, eps=1e-12, padding='same')[source]#
Create a module that computes the Structural Similarity (SSIM) index between two images.
Measures the (SSIM) index between each element in the input x and target y.
The index can be described as:
\[\text{SSIM}(x, y) = \frac{(2\mu_x\mu_y+c_1)(2\sigma_{xy}+c_2)} {(\mu_x^2+\mu_y^2+c_1)(\sigma_x^2+\sigma_y^2+c_2)}\]- where:
\(c_1=(k_1 L)^2\) and \(c_2=(k_2 L)^2\) are two variables to stabilize the division with weak denominator.
\(L\) is the dynamic range of the pixel-values (typically this is \(2^{\#\text{bits per pixel}}-1\)).
- Parameters:
window_size (
int) – the size of the gaussian kernel to smooth the images.max_val (
float, optional) – the dynamic range of the images. Default:1.0eps (
float, optional) – Small value for numerically stability when dividing. Default:1e-12padding (
str, optional) –'same'|'valid'. Whether to only use the “valid” convolution area to compute SSIM to match the MATLAB implementation of original SSIM paper. Default:"same"
- Shape:
Input: \((B, C, H, W)\).
Target \((B, C, H, W)\).
Output: \((B, C, H, W)\).
Examples
>>> input1 = torch.rand(1, 4, 5, 5) >>> input2 = torch.rand(1, 4, 5, 5) >>> ssim = SSIM(5) >>> ssim_map = ssim(input1, input2) # 1x4x5x5
- class kornia.metrics.SSIM3D(window_size, max_val=1.0, eps=1e-12, padding='same')[source]#
Create a module that computes the Structural Similarity (SSIM) index between two 3D images.
Measures the (SSIM) index between each element in the input x and target y.
The index can be described as:
\[\text{SSIM}(x, y) = \frac{(2\mu_x\mu_y+c_1)(2\sigma_{xy}+c_2)} {(\mu_x^2+\mu_y^2+c_1)(\sigma_x^2+\sigma_y^2+c_2)}\]- torch.where:
\(c_1=(k_1 L)^2\) and \(c_2=(k_2 L)^2\) are two variables to stabilize the division with weak denominator.
\(L\) is the dynamic range of the pixel-values (typically this is \(2^{\#\text{bits per pixel}}-1\)).
- Parameters:
window_size (
int) – the size of the gaussian kernel to smooth the images.max_val (
float, optional) – the dynamic range of the images. Default:1.0eps (
float, optional) – Small value for numerically stability when dividing. Default:1e-12padding (
str, optional) –'same'|'valid'. Whether to only use the “valid” convolution area to compute SSIM to match the MATLAB implementation of original SSIM paper. Default:"same"
- Shape:
Input: \((B, C, D, H, W)\).
Target \((B, C, D, H, W)\).
Output: \((B, C, D, H, W)\).
Examples
>>> input1 = torch.rand(1, 4, 5, 5, 5) >>> input2 = torch.rand(1, 4, 5, 5, 5) >>> ssim = SSIM3D(5) >>> ssim_map = ssim(input1, input2) # 1x4x5x5x5