Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import numpy as np
from csbdeep.utils import normalize
def norm_minmse(gt, x, normalize_gt=True):
"""
normalizes and affinely scales an image pair such that the MSE is minimized
Parameters
----------
gt: ndarray
the ground truth image
x: ndarray
the image that will be affinely scaled
normalize_gt: bool
set to True of gt image should be normalized (default)
Returns
-------
gt_scaled, x_scaled
"""
if normalize_gt:
gt = normalize(gt, 0.1, 99.9, clip=False).astype(np.float32, copy = False)
x = x.astype(np.float32, copy=False) - np.mean(x)
gt = gt.astype(np.float32, copy=False) - np.mean(gt)
scale = np.cov(x.flatten(), gt.flatten())[0, 1] / np.var(x.flatten())
return gt, scale * x
if __name__ == '__main__':
from scipy.misc import ascent
from skimage.measure import compare_psnr, compare_mse, compare_ssim
# ground truth image
y = ascent().astype(np.float32)
# input image to compare to
x1 = y + 30*np.random.normal(0,1,y.shape)
# a scaled and shifted version of x1
x2 = 2*x1+100
# calulate mse, psnr, and ssim of the normalized/scaled images
mse1 = compare_mse(*norm_minmse(y, x1))
mse2 = compare_mse(*norm_minmse(y, x2))
# should be the same
print("MSE1 = %.6f\nMSE2 = %.6f"%(mse1, mse2))
psnr1 = compare_psnr(*norm_minmse(y, x1), data_range = 1.)
psnr2 = compare_psnr(*norm_minmse(y, x2), data_range = 1.)
# should be the same
print("PSNR1 = %.6f\nPSNR2 = %.6f"%(psnr1,psnr2))
ssim1 = compare_ssim(*norm_minmse(y, x1), data_range = 1.)
ssim2 = compare_ssim(*norm_minmse(y, x2), data_range = 1.)
# should be the same
print("SSIM1 = %.6f\nSSIM2 = %.6f"%(ssim1,ssim2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment