kaltu
kaltu

Reputation: 310

skimage.measure produce strangely high mean square error

Consider following code

import numpy as np
from skimage import measure

def mse(x, y):
    return np.mean(np.square(x - y))

def psnr(x, y):
    return 10 * np.log10(255 ** 2 / mse(x, y))

x = (np.random.rand(512, 512) * 255).astype(np.uint8)
y = (np.random.rand(512, 512) * 255).astype(np.uint8)
print(type(x))
print('MSE (np)\t', mse(x, y))
print('MSE (sk)\t', measure.compare_mse(x, y))
print('PSNR(np)\t', psnr(x, y))
print('PSNR(sk)\t', measure.compare_psnr(x, y))
print('PSNR(dr)\t', measure.compare_psnr(x, y, data_range=255))

It produce (may vary due to random):

MSE (np)         105.4649887084961
MSE (sk)         10802.859519958496
PSNR(np)         27.899720503741783
PSNR(sk)         7.7954163229186815
PSNR(dr)         7.7954163229186815

which is very puzzling. The mean-squre error is extrodanry high compare to vanilla numpy implementation.

The x and y in the code is to mimic an ordinary image with 8-bit integer data depth. Dig into the github of skimage:

def _as_floats(im1, im2):
    """Promote im1, im2 to nearest appropriate floating point precision."""
    float_type = np.result_type(im1.dtype, im2.dtype, np.float32)
    im1 = np.asarray(im1, dtype=float_type)
    im2 = np.asarray(im2, dtype=float_type)
    return im1, im2


def compare_mse(im1, im2):
    """Compute the mean-squared error between two images.
    Parameters
    ----------
    im1, im2 : ndarray
        Image.  Any dimensionality.
    Returns
    -------
    mse : float
        The mean-squared error (MSE) metric.
    """
    _assert_compatible(im1, im2)
    im1, im2 = _as_floats(im1, im2)
return np.mean(np.square(im1 - im2), dtype=np.float64)

It cast the image to float32 and re-cast to float64 again then to compute MSE. Dose this approach contribute to the skyrocketing high MSE value showed above?

Upvotes: 1

Views: 785

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114841

Your MSE function is the one that is miscalculating the value. The calculation np.square(x - y) is done with the data types of the inputs x and y, which is np.uint8 in this case. If any of the squared differences exceed 255, they will "wrap around", e.g.

In [37]: a = np.array([2, 3, 225, 0], dtype=np.uint8)

In [38]: b = np.array([3, 2, 0, 65], dtype=np.uint8)

You can already see problems in the subtraction:

In [39]: a - b
Out[39]: array([255,   1, 225, 191], dtype=uint8)

Now square those, and more problems are seen:

In [40]: np.square(a - b)
Out[40]: array([  1,   1, 193, 129], dtype=uint8)

If you convert the inputs to floating point before calling your function, it agrees with the skimage function:

In [41]: mse(x.astype(float), y.astype(float))
Out[41]: 10836.0170211792

In [42]: measure.compare_mse(x, y)
Out[42]: 10836.0170211792

Upvotes: 3

Related Questions