Skip to content
Snippets Groups Projects
Commit 8dd47386 authored by Nuno Pimpão Santos Martins's avatar Nuno Pimpão Santos Martins
Browse files

add best psnr from n2v source

parent ffa55b59
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,7 @@ import warnings ...@@ -6,6 +6,7 @@ import warnings
import pywt import pywt
from skimage.filters import gaussian from skimage.filters import gaussian
from skimage.transform import rescale from skimage.transform import rescale
from csbdeep.utils.utils import normalize_minmse
# warnings.simplefilter('ignore', category=NumbaDeprecationWarning) # warnings.simplefilter('ignore', category=NumbaDeprecationWarning)
# warnings.simplefilter('ignore', category=NumbaPendingDeprecationWarning) # warnings.simplefilter('ignore', category=NumbaPendingDeprecationWarning)
...@@ -280,12 +281,45 @@ def PSNR(gt, pred, range_=4095.0): ...@@ -280,12 +281,45 @@ def PSNR(gt, pred, range_=4095.0):
psnr_indices = np.zeros((gt.shape[0]), dtype='float32') psnr_indices = np.zeros((gt.shape[0]), dtype='float32')
for z in range(gt.shape[0]): for z in range(gt.shape[0]):
mse = np.mean((gt[z] - pred[z])**2) # mse = np.mean((gt[z] - pred[z])**2)
psnr = 20 * np.log10((range_)/np.sqrt(mse)) # psnr = 20 * np.log10((range_)/np.sqrt(mse))
psnr_indices[z] = psnr psnr_indices[z] = best_PSNR(gt[z], pred[z], range_)
return psnr_indices return psnr_indices
def PSNR_single(gt, img, range):
"""
Compute Peak Signal-to-Noise Ratio.
Parameters:
gt: np.array
The ground truth target image.
img: np.array
The image of interest.
range: float
Intensity range e.g. gt.max() - gt.min() used for the PSNR
computation.
"""
mse = np.mean(np.square(gt - img))
return 20 * np.log10(range) - 10 * np.log10(mse)
def best_PSNR(gt, img, range):
"""
Compute best Peak Signal-to-Noise Ratio by normalizing img such that
MSE is minimized to the gt image.
Parameters:
gt: np.array
The ground truth target image.
img: np.array
The image of interest.
range: float
Intensity range e.g. gt.max() - gt.min() used for the PSNR
computation.
"""
img_n = normalize_minmse(img, gt)
return PSNR_single(gt, img_n, range=range)
def MSE(gt, pred): def MSE(gt, pred):
""" """
TODO add description TODO add description
...@@ -310,4 +344,4 @@ def MAE(gt, pred): ...@@ -310,4 +344,4 @@ def MAE(gt, pred):
for z in range(gt.shape[0]): for z in range(gt.shape[0]):
mae_values[z] = np.mean((gt[z] - pred[z])) mae_values[z] = np.mean((gt[z] - pred[z]))
return mae_values return mae_values
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment