def filterKernel2D(ftype='Gaussian', size=(5, 5), sigma=None, sigma2=None, radius=None): """Creates a 2d filter kernel of a special type Arguments: ftype (str): filter type, see :ref:`FilterTypes` size (array or tuple): size of the filter kernel sigma (tuple or float): std for the first gaussian (if present) radius (tuple or float): radius of the kernel (if applicable) sigma2 (tuple or float): std of a second gaussian (if present) Returns: array: structure element """ ftype = ftype.lower() o = structureElementOffsets(size) mo = o.min(axis=1) size = numpy.array(size) if ftype == 'mean': # unifrom return numpy.ones(size) / size.prod() elif ftype == 'gaussian': if sigma == None: sigma = size / 2. / math.sqrt(2 * math.log(2)) sigma = numpy.array(sigma) if len(sigma) < 3: sigma = numpy.array((sigma[0], sigma[0])) else: sigma = sigma[0:2] g = numpy.mgrid[-o[0, 0]:o[0, 1], -o[1, 0]:o[1, 1]] add = ((size + 1) % 2) / 2. x = g[0, :, :, :] + add[0] y = g[1, :, :, :] + add[1] ker = numpy.exp(-(x * x / 2. / (sigma[0] * sigma[0]) + y * y / 2. / (sigma[1] * sigma[1]))) return ker / ker.sum() elif ftype == 'sphere': if radius == None: radius = mo radius = numpy.array(radius) if len(radius) < 3: radius = numpy.array((radius[0], radius[0])) else: radius = radius[0:2] g = numpy.mgrid[-o[0, 0]:o[0, 1], -o[1, 0]:o[1, 1]] add = ((size + 1) % 2) / 2. x = g[0, :, :, :] + add[0] y = g[1, :, :, :] + add[1] ker = 1 - (x * x / 2. / (radius[0] * radius[0]) + y * y / 2. / (radius[1] * radius[1])) ker[ker < 0] = 0. return ker / ker.sum() elif ftype == 'disk': if radius == None: radius = mo radius = numpy.array(radius) if len(radius) < 3: radius = numpy.array((radius[0], radius[0])) else: radius = radius[0:2] g = numpy.mgrid[-o[0, 0]:o[0, 1], -o[1, 0]:o[1, 1]] add = ((size + 1) % 2) / 2. x = g[0, :, :, :] + add[0] y = g[1, :, :, :] + add[1] ker = 1 - (x * x / 2. / (radius[0] * radius[0]) + y * y / 2. / (radius[1] * radius[1])) ker[ker < 0] = 0. ker[ker > 0] = 1.0 return ker / ker.sum() elif ftype == 'log': # laplacian of gaussians if sigma == None: sigma = size / 4. / math.sqrt(2 * math.log(2)) sigma = numpy.array(sigma) if len(sigma) < 3: sigma = numpy.array((sigma[0], sigma[0])) else: sigma = sigma[0:2] g = numpy.mgrid[-o[0, 0]:o[0, 1], -o[1, 0]:o[1, 1]] add = ((size + 1) % 2) / 2. x = g[0, :, :, :] + add[0] y = g[1, :, :, :] + add[1] ker = numpy.exp(-(x * x / 2. / (radius[0] * radius[0]) + y * y / 2. / (radius[1] * radius[1]))) ker /= ker.sum() arg = x * x / math.pow(sigma[0], 4) + y * y / math.pow(sigma[1], 4) - ( 1 / (sigma[0] * sigma[0]) + 1 / (sigma[1] * sigma[1])) ker = ker * arg return ker - ker.sum() / len(ker) elif ftype == 'dog': if sigma2 == None: sigma2 = size / 2. / math.sqrt(2 * math.log(2)) sigma2 = numpy.array(sigma2) if len(sigma2) < 3: sigma2 = numpy.array((sigma2[0], sigma2[0])) else: sigma2 = sigma2[0:2] if sigma == None: sigma = sigma2 / 1.5 sigma = numpy.array(sigma) if len(sigma) < 3: sigma = numpy.array((sigma[0], sigma[0])) else: sigma = sigma[0:2] g = numpy.mgrid[-o[0, 0]:o[0, 1], -o[1, 0]:o[1, 1]] add = ((size + 1) % 2) / 2. x = g[0, :, :, :] + add[0] y = g[1, :, :, :] + add[1] ker = numpy.exp(-(x * x / 2. / (sigma[0] * sigma[0]) + y * y / 2. / (sigma[1] * sigma[1]))) ker /= ker.sum() sub = numpy.exp(-(x * x / 2. / (sigma2[0] * sigma2[0]) + y * y / 2. / (sigma2[1] * sigma2[1]))) return ker - sub / sub.sum() else: raise Exception('filter type ' + ftype + ' not implemented!')
def findIntensity(img, centers, findIntensityParameter = None, method = None, size = (3,3,3), verbose = False, out = sys.stdout, **parameter): """Find instensity value around centers in the image Arguments: img (array): image data findIntensityParameter (dict): =========== =================== =========================================================== Name Type Descritption =========== =================== =========================================================== *method* (str, func, None) method to use to determine intensity (e.g. "Max" or "Mean") if None take intensities at the given pixels *size* (tuple) size of the box on which to perform the *method* *verbose* (bool or int) print / plot information about this step =========== =================== =========================================================== verbose (bool): print progress info out (object): object to write progress info to Returns: array: measured intensities """ method = getParameter(findIntensityParameter, "method", "Max"); size = getParameter(findIntensityParameter, "size", (3,3,3)); verbose = getParameter(findIntensityParameter, "verbose", verbose); if verbose: writeParameter(out = out, head = 'Cell Intensities:', method = method, size = size); timer = Timer(); if centers.shape[0] == 0: return numpy.zeros(0); if method is None: return numpy.array([img[centers[i,0], centers[i,1], centers[i,2]] for i in range(centers.shape[0])]); isize = img.shape; #print isize offs = structureElementOffsets(size); if isinstance(method, str): method = eval('numpy.' + method.lower()); intensities = numpy.zeros(centers.shape[0], dtype = img.dtype); for c in range(centers.shape[0]): xmin = int(-offs[0,0] + centers[c,0]); if xmin < 0: xmin = 0; xmax = int(offs[0,1] + centers[c,0]); if xmax > isize[0]: xmax = isize[0]; ymin = int(-offs[1,0] + centers[c,1]); if ymin < 0: ymin = 0; ymax = int(offs[1,1] + centers[c,1]); if ymax > isize[1]: ymax = isize[1]; zmin = int(-offs[2,0] + centers[c,2]); if zmin < 0: zmin = 0; zmax = int(offs[1,1] + centers[c,2]); if zmax > isize[2]: zmax = isize[2]; #print xmin, xmax, ymin, ymax, zmin, zmax data = img[xmin:xmax, ymin:ymax, zmin:zmax]; intensities[c] = method(data); if verbose: out.write(timer.elapsedTime(head = 'Cell Intensities')); return intensities;
def filterKernel2D(ftype = 'Gaussian', size = (5,5), sigma = None, sigma2 = None, radius = None): """Creates a 2d filter kernel of a special type Arguments: ftype (str): filter type, see :ref:`FilterTypes` size (array or tuple): size of the filter kernel sigma (tuple or float): std for the first gaussian (if present) radius (tuple or float): radius of the kernel (if applicable) sigma2 (tuple or float): std of a second gaussian (if present) Returns: array: structure element """ ftype = ftype.lower(); o = structureElementOffsets(size); mo = o.min(axis=1); size = numpy.array(size); if ftype == 'mean': # unifrom return numpy.ones(size)/ size.prod(); elif ftype == 'gaussian': if sigma == None: sigma = size / 2. / math.sqrt(2 * math.log(2)); sigma = numpy.array(sigma); if len(sigma) < 3: sigma = numpy.array((sigma[0], sigma[0])); else: sigma = sigma[0:2]; g = numpy.mgrid[-o[0,0]:o[0,1], -o[1,0]:o[1,1]]; add = ((size + 1) % 2) / 2.; x = g[0,:,:,:] + add[0]; y = g[1,:,:,:] + add[1]; ker = numpy.exp(-(x * x / 2. / (sigma[0] * sigma[0]) + y * y / 2. / (sigma[1] * sigma[1]))); return ker/ker.sum(); elif ftype == 'sphere': if radius == None: radius = mo; radius = numpy.array(radius); if len(radius) < 3: radius = numpy.array((radius[0], radius[0])); else: radius = radius[0:2]; g = numpy.mgrid[-o[0,0]:o[0,1], -o[1,0]:o[1,1]]; add = ((size + 1) % 2) / 2.; x = g[0,:,:,:] + add[0]; y = g[1,:,:,:] + add[1]; ker = 1 - (x * x / 2. / (radius[0] * radius[0]) + y * y / 2. / (radius[1] * radius[1])); ker[ker < 0] = 0.; return ker / ker.sum(); elif ftype == 'disk': if radius == None: radius = mo; radius = numpy.array(radius); if len(radius) < 3: radius = numpy.array((radius[0], radius[0])); else: radius = radius[0:2]; g = numpy.mgrid[-o[0,0]:o[0,1], -o[1,0]:o[1,1]]; add = ((size + 1) % 2) / 2.; x = g[0,:,:,:] + add[0]; y = g[1,:,:,:] + add[1]; ker = 1 - (x * x / 2. / (radius[0] * radius[0]) + y * y / 2. / (radius[1] * radius[1])); ker[ker < 0] = 0.; ker[ker > 0] = 1.0; return ker / ker.sum(); elif ftype == 'log': # laplacian of gaussians if sigma == None: sigma = size / 4. / math.sqrt(2 * math.log(2)); sigma = numpy.array(sigma); if len(sigma) < 3: sigma = numpy.array((sigma[0], sigma[0])); else: sigma = sigma[0:2]; g = numpy.mgrid[-o[0,0]:o[0,1], -o[1,0]:o[1,1]]; add = ((size + 1) % 2) / 2.; x = g[0,:,:,:] + add[0]; y = g[1,:,:,:] + add[1]; ker = numpy.exp(-(x * x / 2. / (radius[0] * radius[0]) + y * y / 2. / (radius[1] * radius[1]))); ker /= ker.sum(); arg = x * x / math.pow(sigma[0], 4) + y * y/ math.pow(sigma[1],4) - (1/(sigma[0] * sigma[0]) + 1/(sigma[1] * sigma[1])); ker = ker * arg; return ker - ker.sum()/len(ker); elif ftype == 'dog': if sigma2 == None: sigma2 = size / 2. / math.sqrt(2 * math.log(2)); sigma2 = numpy.array(sigma2); if len(sigma2) < 3: sigma2 = numpy.array((sigma2[0], sigma2[0])); else: sigma2 = sigma2[0:2]; if sigma == None: sigma = sigma2 / 1.5; sigma = numpy.array(sigma); if len(sigma) < 3: sigma = numpy.array((sigma[0], sigma[0])); else: sigma = sigma[0:2]; g = numpy.mgrid[-o[0,0]:o[0,1], -o[1,0]:o[1,1]]; add = ((size + 1) % 2) / 2.; x = g[0,:,:,:] + add[0]; y = g[1,:,:,:] + add[1]; ker = numpy.exp(-(x * x / 2. / (sigma[0] * sigma[0]) + y * y / 2. / (sigma[1] * sigma[1]))); ker /= ker.sum(); sub = numpy.exp(-(x * x / 2. / (sigma2[0] * sigma2[0]) + y * y / 2. / (sigma2[1] * sigma2[1]))); return ker - sub / sub.sum(); else: raise StandardError('filter type ' + ftype + ' not implemented!');
def findIntensity(img, centers, findIntensityParameter = None, method = None, size = (3,3,3), verbose = False, out = sys.stdout, **parameter): """Find instensity value around centers in the image Arguments: img (array): image data findIntensityParameter (dict): =========== =================== =========================================================== Name Type Descritption =========== =================== =========================================================== *method* (str, func, None) method to use to determine intensity (e.g. "Max" or "Mean") if None take intensities at the given pixels *size* (tuple) size of the box on which to perform the *method* *verbose* (bool or int) print / plot information about this step =========== =================== =========================================================== verbose (bool): print progress info out (object): object to write progress info to Returns: array: measured intensities """ method = getParameter(findIntensityParameter, "method", "Max"); size = getParameter(findIntensityParameter, "size", (3,3,3)); verbose = getParameter(findIntensityParameter, "verbose", verbose); if verbose: writeParameter(out = out, head = 'Cell Intensities:', method = method, size = size); timer = Timer(); if centers.shape[0] == 0: return numpy.zeros(0); if method is None: return numpy.array([img[centers[i,0], centers[i,1], centers[i,2]] for i in range(centers.shape[0])]); isize = img.shape; #print isize offs = structureElementOffsets(size); if isinstance(method, basestring): method = eval('numpy.' + method.lower()); intensities = numpy.zeros(centers.shape[0], dtype = img.dtype); for c in range(centers.shape[0]): xmin = int(-offs[0,0] + centers[c,0]); if xmin < 0: xmin = 0; xmax = int(offs[0,1] + centers[c,0]); if xmax > isize[0]: xmax = isize[0]; ymin = int(-offs[1,0] + centers[c,1]); if ymin < 0: ymin = 0; ymax = int(offs[1,1] + centers[c,1]); if ymax > isize[1]: ymax = isize[1]; zmin = int(-offs[2,0] + centers[c,2]); if zmin < 0: zmin = 0; zmax = int(offs[1,1] + centers[c,2]); if zmax > isize[2]: zmax = isize[2]; #print xmin, xmax, ymin, ymax, zmin, zmax data = img[xmin:xmax, ymin:ymax, zmin:zmax]; intensities[c] = method(data); if verbose: out.write(timer.elapsedTime(head = 'Cell Intensities')); return intensities;
def filterKernel3D(ftype = 'Gaussian', size = (5,5,5), sigma = None, sigma2 = None, radius = None): """Creates a 3d filter kernel of a special type Arguments: ftype (str): filter type, see :ref:`FilterTypes` size (array or tuple): size of the filter kernel sigma (tuple or float): std for the first gaussian (if present) radius (tuple or float): radius of the kernel (if applicable) sigma2 (tuple or float): std of a second gaussian (if present) Returns: array: structure element """ ftype = ftype.lower(); o = structureElementOffsets(size); mo = o.min(axis=1); size = numpy.array(size); if ftype == 'mean': # differnce of gaussians return numpy.ones(size)/ size.prod(); elif ftype == 'gaussian': if sigma == None: sigma = size / 2. / math.sqrt(2 * math.log(2)); sigma = numpy.array(sigma); if len(sigma) < 3: sigma = numpy.array((sigma[0], sigma[0], sigma[0])); else: sigma = sigma[0:3]; g = numpy.mgrid[-o[0,0]:o[0,1], -o[1,0]:o[1,1], -o[2,0]:o[2,1]]; add = ((size + 1) % 2) / 2.; x = g[0,:,:,:] + add[0]; y = g[1,:,:,:] + add[1]; z = g[2,:,:,:] + add[2]; ker = numpy.exp(-(x * x / 2. / (sigma[0] * sigma[0]) + y * y / 2. / (sigma[1] * sigma[1]) + z * z / 2. / (sigma[2] * sigma[2]))); return ker/ker.sum(); elif ftype == 'sphere': if radius == None: radius = mo; radius = numpy.array(radius); if len(radius) < 3: radius = numpy.array((radius[0], radius[0], radius[0])); else: radius = radius[0:3]; g = numpy.mgrid[-o[0,0]:o[0,1], -o[1,0]:o[1,1], -o[2,0]:o[2,1]]; add = ((size + 1) % 2) / 2.; x = g[0,:,:,:] + add[0]; y = g[1,:,:,:] + add[1]; z = g[2,:,:,:] + add[2]; ker = 1 - (x * x / 2. / (radius[0] * radius[0]) + y * y / 2. / (radius[1] * radius[1]) + z * z / 2. / (radius[2] * radius[2])); ker[ker < 0] = 0.; return ker / ker.sum(); elif ftype == 'disk': if radius == None: radius = mo; radius = numpy.array(radius); if len(radius) < 3: radius = numpy.array((radius[0], radius[0], radius[0])); else: radius = radius[0:3]; g = numpy.mgrid[-o[0,0]:o[0,1], -o[1,0]:o[1,1], -o[2,0]:o[2,1]]; add = ((size + 1) % 2) / 2.; x = g[0,:,:,:] + add[0]; y = g[1,:,:,:] + add[1]; z = g[2,:,:,:] + add[2]; ker = 1 - (x * x / 2. / (radius[0] * radius[0]) + y * y / 2. / (radius[1] * radius[1]) + z * z / 2. / (radius[2] * radius[2])); ker[ker < 0] = 0.; ker[ker > 0] = 1.0; return ker / ker.sum(); elif ftype == 'log': # laplacian of gaussians if sigma == None: sigma = size / 4. / math.sqrt(2 * math.log(2)); sigma = numpy.array(sigma); if len(sigma) < 3: sigma = numpy.array((sigma[0], sigma[0], sigma[0])); else: sigma = sigma[0:3]; g = numpy.mgrid[-o[0,0]:o[0,1], -o[1,0]:o[1,1], -o[2,0]:o[2,1]]; add = ((size + 1) % 2) / 2.; x = g[0,:,:,:] + add[0]; y = g[1,:,:,:] + add[1]; z = g[2,:,:,:] + add[2]; ker = numpy.exp(-(x * x / 2. / (radius[0] * radius[0]) + y * y / 2. / (radius[1] * radius[1]) + z * z / 2. / (radius[2] * radius[2]))); ker /= ker.sum(); arg = x * x / math.pow(sigma[0], 4) + y * y/ math.pow(sigma[1],4) + z * z / math.pow(sigma[2],4) - (1/(sigma[0] * sigma[0]) + 1/(sigma[1] * sigma[1]) + 1 / (sigma[2] * sigma[2])); ker = ker * arg; return ker - ker.sum()/len(ker); elif ftype == 'dog': if sigma2 == None: sigma2 = size / 2. / math.sqrt(2 * math.log(2)); sigma2 = numpy.array(sigma2); if len(sigma2) < 3: sigma2 = numpy.array((sigma2[0], sigma2[0], sigma2[0])); else: sigma2 = sigma2[0:3]; if sigma == None: sigma = sigma2 / 1.5; sigma = numpy.array(sigma); if len(sigma) < 3: sigma = numpy.array((sigma[0], sigma[0], sigma[0])); else: sigma = sigma[0:3]; g = numpy.mgrid[-o[0,0]:o[0,1], -o[1,0]:o[1,1], -o[2,0]:o[2,1]]; add = ((size + 1) % 2) / 2.; x = g[0,:,:,:] + add[0]; y = g[1,:,:,:] + add[1]; z = g[2,:,:,:] + add[2]; ker = numpy.exp(-(x * x / 2. / (sigma[0] * sigma[0]) + y * y / 2. / (sigma[1] * sigma[1]) + z * z / 2. / (sigma[2] * sigma[2]))); ker /= ker.sum(); sub = numpy.exp(-(x * x / 2. / (sigma2[0] * sigma2[0]) + y * y / 2. / (sigma2[1] * sigma2[1]) + z * z / 2. / (sigma2[2] * sigma2[2]))); return ker - sub / sub.sum(); else: raise StandardError('filter type ' + ftype + ' not implemented!');