diff --git a/skimage/measure/_simple_metrics.py b/skimage/measure/simple_metrics.py similarity index 69% rename from skimage/measure/_simple_metrics.py rename to skimage/measure/simple_metrics.py index 2848b42c..883b4dbd 100644 --- a/skimage/measure/_simple_metrics.py +++ b/skimage/measure/simple_metrics.py @@ -7,7 +7,7 @@ __all__ = ['mse', 'nrmse', 'psnr'] def mse(X, Y): - """ compute mean-squared error between two images. + """Compute the mean-squared error between two images. Parameters ---------- @@ -27,42 +27,38 @@ def mse(X, Y): return np.square(X - Y).mean() -def nrmse(im_true, im, norm_type='Euclidean'): - """ compute the normalized root mean-squared error between two images. +def nrmse(im_true, im_test, norm_type='Euclidean'): + """Compute the normalized root mean-squared error between two images. Parameters ---------- im_true : ndarray Ground-truth image. - im : ndarray + im_test : ndarray Test image. norm_type : {'Euclidean', 'min-max', 'mean'} Controls the normalization method to use in the denominator of the - NRMSE. + NRMSE. There is no standard method of normalization across the + literature [1]_. The methods available here are as follows: + + - 'Euclidean' : normalize by the Euclidean norm of ``im_true``. + - 'min-max' : normalize by the intensity range of ``im_true``. + - 'mean' : normalize by the mean of ``im_true``. Returns ------- nrmse : float The NRMSE metric. - Notes - ----- - There is no standard method of normalization across the literature [1]_. - The methods available here are as follows: - - - 'Euclidean' : normalize by the Euclidean norm of ``im_true``. - - 'min-max' : normalize by the intensity range of ``im_true``. - - 'mean' : normalize by the mean of ``im_true``. - References ---------- .. [1] https://en.wikipedia.org/wiki/Root-mean-square_deviation """ - if not im_true.dtype == im.dtype: + if not im_true.dtype == im_test.dtype: raise ValueError('Input images must have the same dtype.') - if not im_true.shape == im.shape: + if not im_true.shape == im_test.shape: raise ValueError('Input images must have the same dimensions.') norm_type = norm_type.lower() @@ -72,17 +68,19 @@ def nrmse(im_true, im, norm_type='Euclidean'): denom = im_true.max() - im_true.min() elif norm_type == 'mean': denom = im_true.mean() - return np.sqrt(mse(im_true, im)) / denom + else: + raise ValueError("Unsupported norm_type") + return np.sqrt(mse(im_true, im_test)) / denom -def psnr(im_true, im, dynamic_range=None): +def psnr(im_true, im_test, dynamic_range=None): """ Compute the peak signal to noise ratio (PSNR) for an image. Parameters ---------- im_true : ndarray Ground-truth image. - im : ndarray + im_test : ndarray Test image. dynamic_range : int The dynamic range of the input image (distance between minimum and @@ -99,10 +97,10 @@ def psnr(im_true, im, dynamic_range=None): .. [1] https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio """ - if not im_true.dtype == im.dtype: + if not im_true.dtype == im_test.dtype: raise ValueError('Input images must have the same dtype.') - if not im_true.shape == im.shape: + if not im_true.shape == im_test.shape: raise ValueError('Input images must have the same dimensions.') if dynamic_range is None: @@ -110,7 +108,7 @@ def psnr(im_true, im, dynamic_range=None): dynamic_range = dmax - dmin im_true = im_true.astype(np.float64) - im = im.astype(np.float64) + im_test = im_test.astype(np.float64) - err = mse(im_true, im) + err = mse(im_true, im_test) return 10 * np.log10((dynamic_range ** 2) / err) diff --git a/skimage/measure/tests/test_simple_metrics.py b/skimage/measure/tests/test_simple_metrics.py index cab25ab7..43122e01 100644 --- a/skimage/measure/tests/test_simple_metrics.py +++ b/skimage/measure/tests/test_simple_metrics.py @@ -1,5 +1,6 @@ import numpy as np -from numpy.testing import assert_equal, assert_raises, assert_almost_equal +from numpy.testing import (run_module_suite, assert_equal, assert_raises, + assert_almost_equal) from skimage.measure import psnr, nrmse import skimage.data @@ -41,3 +42,7 @@ def test_NRMSE(): assert_raises(ValueError, nrmse, x.astype(np.uint8), y) assert_raises(ValueError, nrmse, x[:-1], y) + + +if __name__ == "__main__": + run_module_suite()