def IntImage(f): """ ~ Calculates the integral image of f ========Input========= f : 2D input array ========Raises======== TypeError : If input array isn't 2D ========Output======== I : Integral image """ if (tl.ndim(f) != 2): raise TypeError('Input array must be 2D') n, m = f.shape I = tl.zeros((n, m), dtype='float') for j in range(m): S = 0 for i in range(n): S += f[i, j] if (j == 0): I[i, j] = S else: I[i, j] = I[i, j - 1] + S return I
def Power(f, dx=1., dy=None): """ ~ Calculates the power of f (essentially its quadrature) ========Input========= f : 2D input array dx : Horizontal sampling interval (Default value is unit of measurement) dy : Vertical sampling interval (Default value is square pixels) ========Raises======== TypeError : If input array isn't 2D ========Output======== P : Power """ if (tl.ndim(f) != 2): raise TypeError('Input array must be 2D') if (dy == None): dy = dx P = tl.summ(tl.abss(f)**2) * dx * dy return P
def Embed(f, g, x=0., y=0.): """ ~ Embeds g onto f centered at (x,y) ~ (x,y) coordinates are given in a centered cartesian frame ========Input========= f : 2D input array g : 2D array to be embedded x : x cartesian coordinate of the embedding center measured from input array's center y : y cartesian coordinate of the embedding center measured from input array's center ========Raises======== ValueError : If either f or g isn't a 2D array ValueError : If g is bigger than f ========Output======== h : 2D input array f with g embedded at (x,y) """ if ((tl.ndim(f), tl.ndim(g)) != (2, 2)): raise ValueError('Input and embedding arrays must be 2D') (m, n) = tl.shape(f) (mm, nn) = tl.shape(g) if (mm > m or nn > n): raise ValueError('Embedding array must be smaller than input array') h = f.copy() h[tl.int32(tl.floor((m - mm) * 0.5) + y):tl.int32(tl.floor((m + mm) * 0.5) + y), tl.int32(tl.floor((n - nn) * 0.5) + x):tl.int32(tl.floor((n + nn) * 0.5) + x)] = g return h
def Fresnel2S(f, z, wl, L1, L2): """ ~ Propagates input 2D array in free space using the two-step Fresnel propagator Voelz D. Computational Fourier Optics: A MATLAB Tutorial. 2011. ========Input========= f : Input complex amplitude profile (2D complex array) z : Propagation distance wl : Central wavelenght of the field L1 : Input sample plane side lenght L2 : Output sample plane side lenght ========Output======== h : Propagated complex amplitude profile (2D complex array) """ if (tl.ndim(f) != 2): raise TypeError("Input array must be a 2D square array") m, n = tl.shape(f) if (m != n): raise ValueError("Input array must be a 2D square array") k = 2 * tl.pi / wl # Source plane dx1 = L1 / m x1 = tl.arange(-L1 / 2., L1 / 2., dx1) X, Y = tl.meshgrid(x1, x1) F = f * tl.exp(1j * k / (2. * z * L1) * (L1 - L2) * (X**2 + Y**2)) F = tl.fft.fft2(tl.fft.fftshift(F)) # Dummy plane fx1 = tl.arange(-1. / (2 * dx1), 1. / (2 * dx1), 1. / L1) fx1 = tl.fft.fftshift(fx1) FX1, FY1 = tl.meshgrid(fx1, fx1) G = F * tl.exp(-1j * tl.pi * wl * z * L1 / L2 * (FX1**2 + FY1**2)) g = tl.fft.ifftshift(tl.fft.ifft2(G)) # Observation plane dx2 = L2 / m x2 = tl.arange(-L2 / 2., L2 / 2., dx2) X, Y = tl.meshgrid(x2, x2) g = (L2 / L1) * g * tl.exp(-1j * k / (2. * z * L2) * (L1 - L2) * (X**2 + Y**2)) g = g * (dx1 / dx2)**2 return g
def Trim(f, shape, x=0., y=0.): """ ~ Trims an array from f centered at (x,y) with shape shape ~ (x,y) coordinates are given in a centered cartesian frame ========Input========= f : 2D input array from which the trim is taken shape : Trimming shape x : x cartesian coordinate of the trimming center measured from input array's center y : y cartesian coordinate of the trimming center measured from input array's center ========Raises======== TypeError : If input array isn't 2D TypeError : If trimming shape isn't 2D ValueError : If trimming shape is bigger than input array's shape ========Output======== h : Trimmed 2D array """ if (tl.ndim(f) != 2): raise TypeError('Input array must be 2D') if (len(shape) != 2): raise TypeError('Trimming shape must be 2D') (m, n) = tl.shape(f) (mm, nn) = shape if (mm > m or nn > n): raise ValueError('Trimming array must be smaller than input array') h = f[tl.int32(tl.floor((m - mm) * 0.5) + y):tl.int32(tl.floor((m + mm) * 0.5) + y), tl.int32(tl.floor((n - nn) * 0.5) + x):tl.int32(tl.floor((n + nn) * 0.5) + x)] return h
def ExportImage(f, path, normalize=True): """ ~ Exports real 2D input array f as an 8-bit grayscale image. ========Input========= f : Real 2D input array path : Image save path normalize : Normalize input array before saving (default value for linear normalization to [0,1]) ========Raises======== TypeError : If f isn't a real 2D array """ if ((tl.isreal(f).all == False) or (tl.ndim(f) != 2)): raise TypeError('Input array must be a real 2D array') if (normalize): f = LinearNormal(f) f = tl.Image.fromarray(f * 255) f = f.convert('L') f.save(path)