def PSNR(f, g, max_val=None): """ ~ Calculates peak signal-to-noise ratio of an array and its noisy approximation ========input========= f : Input noise-free array g : Input noisy approximation array max_val : Maximum pixel value ========output======== psnr : PSNR """ f = tl.array(f) g = tl.array(g) if (tl.shape(f) != tl.shape(g)): raise TypeError('f and g must be arrays of equal shape') if (max_val == None): max_val = tl.maxx([f, g]) if ((f == g).all()): psnr = tl.inf else: mse = tl.mean(tl.abss(f - g)**2) psnr = 10 * tl.log10(max_val**2 / mse) return psnr
def ImportImage(path, normalize=True): """ ~ Imports image as a real 2D array ========Input========= path : Image load path normalize : Normalize output array (default value for linear normalization to [0,1]) ========Raises======== OSError: If path doesn't exist ========Output========= f : 2D array containing the image information """ if (tl.isfile(path) == False): raise OSError('File not found') f = tl.Image.open(path).convert('L') f = tl.array(f, dtype='float') if (normalize): f = LinearNormal(f) return f
def ErrDiffBin(f, T=0.5, b=[[1 / 16., 5 / 16., 3 / 16.], [7 / 16., 0., 0.], [0., 0., 0.]]): """ ~ Binarizes input 2D array using the Error-Diffusion algorithm. Barnard E. Optimal error diffusion for computer-generated holograms. J Opt Soc Am A 1988;5:1803-17. ========Input========= f : Real input 2D array T : Binarization threshold (default value for input array with [0,1] dynamic range) b : 3x3 2D real array with the diffusion coefficients (default given by paper; see paper for details) ========Raises======== TypeError : If b isn't a 3x3 2D real array ValueError : If b doesn't meet the convergence conditions (see paper for details) ========Output======== h[1:-1,1:-1] : Binarized input array """ b = tl.array(b) if (tl.shape(b) != (3, 3) or tl.isreal(b).all() == False): raise TypeError('b has to be a 3x3 2D array') if (tl.summ(abs(b)) > 1): raise ValueError( 'The diffusion coefficients don\'t meet the convergence conditions' ) n, m = f.shape g = tl.zeros((n + 2, m + 2)) h = tl.zeros((n + 2, m + 2)) s = tl.zeros((n + 2, m + 2)) g[1:-1, 1:-1] = f for i in range(1, n - 1): for j in range(1, m - 1): g[i, j] += tl.summ(b * s[i - 1:i + 2, j - 1:j + 2]) h[i, j] = (g[i, j] > T) * 1. s[i, j] = g[i, j] - h[i, j] return h[1:-1, 1:-1]
def CC(f, g): """ ~ Calculates correlation coefficient between two arrays ~ Works similar to corr2 MATLAB function ========input========= f : Input array 1 g : Input array 2 ========output======== cc : Correlation coefficient """ f = tl.array(f) g = tl.array(g) cr = tl.summ(abs((f - tl.mean(f)) * (g - tl.mean(g)))) cc = cr / tl.sqrt( (tl.summ(abs(f - tl.mean(f))**2)) * (tl.summ(abs(g - tl.mean(g))**2))) return cc
def Resize(f, shape): """ ~ Resizes f to shape ========Input========= f : Input 2D array shape : New shape ========Output======== g : Resized f 2D array """ f = LinearNormal(f) f = tl.Image.fromarray(f * 255) g = f.resize(shape) # i'm sorry about this g = tl.array(g, dtype=float) g = LinearNormal(g) return g