cutcutcodec.core.signal.metric.psnr

cutcutcodec.core.signal.metric.psnr()

Compute the peak signal to noise ratio of 2 images in C language.

This function is nearly equivalent to:

import math
import numpy as np

def psnr(im1: np.ndarray, im2: np.ndarray, weights = None) -> float:
    if weights is None:
        weights = [1.0 for _ in range(im1.shape[2])]
    layers_mse = ((im1 - im2)**2).mean(axis=(0, 1)).tolist()
    tot = sum(weights)
    mse = sum(l*w/tot for w, l in zip(weights, layers_mse))
    return -10.0*math.log10(mse) if mse > 1e-10 else 100.0

Parameters

im1, im2np.ndarray

The 2 images to be compared, of shape (height, width, channels). Supported types are float32 and float64.

threadsint, optional

Defines the number of threads. The value -1 means that the function uses as many calculation threads as there are cores. The default value (0) allows the same behavior as (-1) if the function is called in the main thread, otherwise (1) to avoid nested threads. Any other positive value corresponds to the number of threads used.

Returns

psnrfloat

The global peak signal to noise ratio, as a ponderation of the mean square error of each channel.

Examples

>>> import numpy as np
>>> from cutcutcodec.core.signal.metric import psnr
>>> im1 = np.random.random((1080, 1920, 3))
>>> im2 = 0.8*im1 + 0.2*np.random.random((1080, 1920, 3))
>>> round(psnr(im1, im2))
22
>>>