def mask_gaussianND(arr, zyx, v, sigma=2., ret=None, rot=0, clipZero=True): ''' subtract elliptical gaussian at y,x with peakVal v if ret, return arr, else, arr itself is edited ''' from . import imgGeo zyx = N.asarray(zyx) ndim = arr.ndim shape = N.array(arr.shape) try: if len(sigma) != ndim: raise ValueError('len(sigma) must be the same as len(shape)') else: sigma = N.asarray(sigma) except TypeError:#(TypeError, ValueError): sigma = N.asarray([sigma]*ndim) # prepare small window slc = imgGeo.nearbyRegion(shape, N.floor(zyx), sigma * 10) inds, LD = imgFit.rotateIndicesND(slc, dtype=N.float32, rot=rot) param = (0, v,) + tuple(zyx) + tuple(sigma) sidx = 2 + ndim g = imgFit.yGaussianND(N.asarray(param), inds, sidx).astype(arr.dtype.type) roi = arr[slc] if clipZero: g = N.where(g > roi, roi, g) if ret: e = N.zeros_like(arr) e[slc] = g # this may be faster than copy() return arr - e else: arr[slc] -= g
def fitGaussianND(img, zyx, sigma=0.5, window=5, mean_max=None, rot=0): """ img: can be any dimensions zyx: point of object in the img (same dimension as img) sigma: scaler or [sigmaz, sigmay, sigmax] window: window size for fitting (scaler) mean_max: tuple of (mean, max), if already known rot: counter-clockwise, xy-plane (window should be even) returns [[mean, peakVal, [z, y,] x, [sigmaz, sigmay,] sigmax], check] check is 2 if single answer was found, while 5 if not """ slices = imgGeo.nearbyRegion(img.shape, N.floor(zyx), window) inds, LD = rotateIndicesND(slices, dtype=N.float64, rot=rot) # inds, LD = indicesFromSlice(slices, dtype=N.float64) #slices, inds, LD = _sliceIndLD(window, zyx, img.shape) return _fitGaussianND(img[slices], inds, zyx, sigma, mean_max)
def fitGaussian2D(img, y, x, sigma=[2., 2.], window=5, mean_max=None): """ y, x: point of object in the img sigma: scaler or [sigmay, sigmax] window: window size for fitting mean_max: tuple of (mean, max), if already known returns [[mean, peakVal, y, x, sigma], check] check is 2 if single answer was found, while 5 if not """ #yi, xi, LD = _indLD(window, y, x) sl = imgGeo.nearbyRegion(img.shape, N.floor((y, x)), window) inds, LD = indicesFromSlice(sl, dtype=N.float64) # sl, inds, LD = _sliceIndLD(window, (y,x), img.shape) yi, xi = inds #return _fitGaussian2D(img[yi, xi], yi, xi, y, x, sigma, mean_max) return _fitGaussian2D(img[sl], yi, xi, y, x, sigma, mean_max)
def fitGaussian2D(img, y, x, sigma=[2.,2.], window=5, mean_max=None): """ y, x: point of object in the img sigma: scaler or [sigmay, sigmax] window: window size for fitting mean_max: tuple of (mean, max), if already known returns [[mean, peakVal, y, x, sigma], check] check is 2 if single answer was found, while 5 if not """ #yi, xi, LD = _indLD(window, y, x) sl = imgGeo.nearbyRegion(img.shape, N.floor((y,x)), window) inds, LD = indicesFromSlice(sl, dtype=N.float64) # sl, inds, LD = _sliceIndLD(window, (y,x), img.shape) yi, xi = inds #return _fitGaussian2D(img[yi, xi], yi, xi, y, x, sigma, mean_max) return _fitGaussian2D(img[sl], yi, xi, y, x, sigma, mean_max)
def keepShape(a, shape, difmod=None): canvas = N.zeros(shape, a.dtype.type) if difmod is None: dif = (shape - N.array(a.shape, N.float32)) / 2. mod = N.ceil(N.mod(dif, 1)) else: dif, mod = difmod dif = N.where(dif > 0, N.ceil(dif), N.floor(dif)) # smaller aoff = N.where(dif < 0, 0, dif) aslc = [slice(dp, shape[i] - dp + mod[i]) for i, dp in enumerate(aoff)] # larger coff = N.where(dif > 0, 0, -dif) cslc = [slice(dp, a.shape[i] - dp + mod[i]) for i, dp in enumerate(coff)] canvas[aslc] = a[cslc] if difmod is None: return canvas, mod else: return canvas
def mask_gaussianND(arr, zyx, v, sigma=2., ret=None, rot=0, clipZero=True): ''' subtract elliptical gaussian at y,x with peakVal v if ret, return arr, else, arr itself is edited ''' from . import imgGeo zyx = N.asarray(zyx) ndim = arr.ndim shape = N.array(arr.shape) try: if len(sigma) != ndim: raise ValueError('len(sigma) must be the same as len(shape)') else: sigma = N.asarray(sigma) except TypeError: #(TypeError, ValueError): sigma = N.asarray([sigma] * ndim) # prepare small window slc = imgGeo.nearbyRegion(shape, N.floor(zyx), sigma * 10) inds, LD = imgFit.rotateIndicesND(slc, dtype=N.float32, rot=rot) param = ( 0, v, ) + tuple(zyx) + tuple(sigma) sidx = 2 + ndim g = imgFit.yGaussianND(N.asarray(param), inds, sidx).astype(arr.dtype.type) roi = arr[slc] if clipZero: g = N.where(g > roi, roi, g) if ret: e = N.zeros_like(arr) e[slc] = g # this may be faster than copy() return arr - e else: arr[slc] -= g
def keepShape(a, shape, difmod=None): canvas = N.zeros(shape, a.dtype.type) if difmod is None: dif = (shape - N.array(a.shape, N.float32)) / 2. mod = N.ceil(N.mod(dif, 1)) else: dif, mod = difmod dif = N.where(dif > 0, N.ceil(dif), N.floor(dif)) # smaller aoff = N.where(dif < 0, 0, dif) aslc = [slice(dp, shape[i]-dp+mod[i]) for i, dp in enumerate(aoff)] # larger coff = N.where(dif > 0, 0, -dif) cslc = [slice(dp, a.shape[i]-dp+mod[i]) for i, dp in enumerate(coff)] canvas[aslc] = a[cslc] if difmod is None: return canvas, mod else: return canvas
def getShift(shift, ZYX, erosionZYX=0): """ shift: zyxrmm return [zmin,zmax,ymin,ymax,xmin,xmax] """ # erosion try: if len(erosionZYX) == 3: erosionZ = erosionZYX[0] erosionYX = erosionZYX[1:] elif len(erosionZYX) == 2: erosionZ = 0 erosionYX = erosionZYX elif len(erosionZYX) == 1: erosionZ = 0 erosionYX = erosionZYX[0] except TypeError: # scalar erosionZ = erosionZYX erosionYX = erosionZYX # magnification magZYX = N.ones((3, ), N.float32) magZYX[3 - len(shift[4:]):] = shift[4:] if len(shift[4:]) == 1: magZYX[1] = shift[4] # rotation r = shift[3] # target shape ZYX = N.asarray(ZYX, N.float32) ZYXm = ZYX * magZYX # Z z = N.where(shift[0] < 0, N.floor(shift[0]), N.ceil(shift[0])) ztop = ZYXm[0] + z nz = N.ceil(N.where(ztop > ZYX[0], ZYX[0], ztop)) z += erosionZ nz -= erosionZ if z < 0: z = 0 if nz < 0: nz = z + 1 zyx0 = N.ceil((ZYX - ZYXm) / 2.) #print zyx0 #if zyx0[0] > 0: # z -= zyx0[0] # nz += zyx0[0] zs = N.array([z, nz]) # YX #try: # if len(erosionYX) != 2: # raise ValueError, 'erosion is only applied to lateral dimension' #except TypeError: # erosionYX = (erosionYX, erosionYX) yxShift = N.where(shift[1:3] < 0, N.floor(shift[1:3]), N.ceil(shift[1:3])) # rotate the magnified center xyzm = N.ceil(ZYXm[::-1]) / 2. xyr = imgGeo.RotateXY(xyzm[:-1], r) xyr -= xyzm[:-1] yx = xyr[::-1] leftYX = N.ceil(N.abs(yx)) rightYX = -N.ceil(N.abs(yx)) # then translate leftYXShift = (leftYX + yxShift) + zyx0[1:] leftYXShift = N.where(leftYXShift < 0, 0, leftYXShift) rightYXShift = (rightYX + yxShift) - zyx0[1:] YXmax = N.where(ZYXm[1:] > ZYX[1:], ZYXm[1:], ZYX[1:]) rightYXShift = N.where(rightYXShift > 0, YXmax, rightYXShift + YXmax) # deal with - idx rightYXShift = N.where(rightYXShift > ZYX[1:], ZYX[1:], rightYXShift) leftYXShift += erosionYX rightYXShift -= erosionYX # (z0,z1,y0,y1,x0,x1) tempZYX = N.array( (zs[0], zs[1], int(N.ceil(leftYXShift[0])), int(rightYXShift[0]), int(N.ceil(leftYXShift[1])), int(rightYXShift[1]))) return tempZYX