def apply(self, img): if not(self.initialized and (img.shape == self.image_dimension)): if name == 'LDDMM': fimg = np.rfftn(img) if len(self.dimension)==3: (x,y,z) = np.mgrid(0:img.shape[0], 0:img.shape[1], 0:img.shape[2]) u = (self.gamma + self.alpha *(x**2 +y**2+z**2))**(2*self.lddmm_order) elif len(self.dimension)==2: (x,y) = np.mgrid(0:img.shape[0], 0:img.shape[1]) u = (self.gamma + self.alpha *(x**2 +y**2))**(-2*self.lddmm_order) elif len(self.dimension)==1: x = np.mgrid(0:img.shape[0]) u = (self.gamma + self.alpha *(x**2))**(2*self.lddmm_order) else: print 'use lddmm kernel in dimension 1 , 2 or 3' return self.image_dimension = copy(img.shape) self.fft = np.rfftn(u) else: self.image_dimension = img.shape + self.dimension/2 + 1 fimg = np.rfftn(img, self.image_dimension) self.fft = np.ifftshift(np.rfftn(self.values, self.image_dimension)) print 'kernel is not implemented (yet...)' return u = np.multiply(self.fft, fimg) res = np.ifftn(u) return res
def __init__(dimension = None, name = 'gauss', sigma = 6.5, order = 3, lddmm_order = 1, alpha = 0.01, gamma = 1, w1 = 1.0, w2 = 1.0, dim = 3, center = [0,0,0]): self.name = name self.sigma = sigma self.order = order self.alpha = alpha self.gamma = gamma self.w1 = w1 self.w2 = w2 self.dim = dim self.center = center self.dimension = dimension self.image_dimension = None self.initialized = False if name == 'gauss' || name == 'laplacian': center = self.dimension/2 ; if len(self.dimension)==3: (x,y,z) = np.mgrid(0:self.dimension[0], 0:self.dimension[1], 0:self.dimension[2]) u = (x-self.center[0])**2 + (y-self.center[1])**2 + (z-self.center[2])**2 elif len(self.dimension)==2: (x,y) = np.mgrid(0:self.dimension[0], 0:self.dimension[1]) u = (x-self.center[0])**2 + (y-self.center[1])**2 elif len(self.dimension)==1: (x,y) = np.mgrid(0:self.dimension[0]) u = (x-self.center[0])**2 else: print 'use lddmm kernel in dimension 1 , 2 or 3' return if name == 'gauss': self.values = np.exp(-u/(2*sigma**2)) elif name=='laplacian': u = np.sqrt(u) / sigma if order == 0: pol = np.ones(u.shape) elif order == 1: pol = 1 + u elif order == 2: pol = 1 + u + u*u/3 elif order == 3: pol = 1 + u + 0.4*u*u + u**3/15 elif order == 4: pol = 1 + u + (3./7)*(u*u) + (u**3)/10.5 + (u**4)/105 else: print "Laplacian kernel defined up to order 4" return self.values = pol * np.exp(-u)
def Matrix2GCP(x_vec, y_vec, lon, lat): y_lim = y_vec[-1] + 1 x_lim = x_vec[-1] + 1 y = y_vec.max - y_vec xx, yy = np.mgrid(x_vec, y) x, y = xx.ravel(), yy.ravel() lon_n, lat_n = lon.ravel(), lat.ravel() GCP = np.empty([len(x), 4]) GCP[:, 0], GCP[:, 1] = x, y GCP[:, 2], GCP[:, 3] = lon_n, lat_n GCP = GCP.tolist() return GCP, x_lim, y_lim
def fq2_4circ(Q, pars): '''NOTE: This is slow. Compute the |Fourier transform|**2 of a quarter circle. This is an analytical calculation. It is assumed that not more than a 10 oscilations would be needed so circle is made to be 1/10th of image. 1000 x1000 pixels are used. The interpolation function's domain is in units of radians per pixel for a half circle at 100 pixels in radius. ''' global F4Cinterp if (F4Cinterp is None): print( "F4 interpolation function empty, creating it for the first time..." ) N = 1000 X, Y = np.mgrid(N, N) QCimg = (sqrt(X**2 + Y**2) < 100**2).astype(int) FQC = fftshift(fft2(Qimg)) X = (X - N / 2.) * 2 * np.pi / N Y = (Y - N / 2.) * 2 * np.pi / N F4Cinterp = RectBivariateSpline(X, Y, FQC) radius = pars['radius'] Q = radius / 100. * Q F4c = F4Cinterp(Q[0], Q[1])
# Triangulate based on X, Y with Delaunay 2D algorithm. # Save resulting triangulation. mesh = mlab.pipeline.delaunay2d(pts) # Remove the point representation from the plot pts.remove() # Draw a surface based on the triangulation surf = mlab.pipeline.surface(mesh) # Simple plot. mlab.xlabel("x") mlab.ylabel("y") mlab.zlabel("z") mlab.show() x, y, z = np.ogrid[-10:10:20j, -10:10:20j, -10:10:20j] a = np.sin(x*y*z)/(x*y*z) mlab.contour3d( a ) mlab.show() bx = np.mgrid([0.,1.,2.,7.]) by = np.array([5.,4.,3.,8.]) bz = np.array([7.,8.,9.,3.]) b = np.vstack(np.meshgrid(bx,by,bz)).reshape(3,-1).T #ax.plot3D(* x_axis_vec,c='r',marker=marker,markersize=10)
import numpy as np import scipy.integrate as int import scipy.interpolate as ntp import matplotlib.pyplot as plt x = np.linspace(-5, 5, 1) y = np.linspace(-5, 5, 1) xx, yy = np.mgrid() r = 3 h = 3 def func(x, y, r, h): return np.sqrt((x**2 + y**2) / (r / h))
def axis_x( self, n, nodal ): """ 1D array along axis n """ return np.mgrid(self.axis_slice(n,nodal))