Esempio n. 1
0
def filterDoG(img, filterDoGParameter = None,  size = None, sigma = None, sigma2 = None, save = None, verbose = None,
              subStack = None, out = sys.stdout, **parameter):
    """Difference of Gaussians (DoG) filter step
    
    Arguments:
        img (array): image data
        filterDoGParameter (dict):
            ========= ==================== ================================================================
            Name      Type                 Descritption
            ========= ==================== ================================================================
            *size*    (tuple or None)      size for the DoG filter 
                                           if None, do not correct for any background
            *sigma*   (tuple or None)      std of outer Guassian, if None autmatically determined from size
            *sigma2*  (tuple or None)      std of inner Guassian, if None autmatically determined from size
            *save*    (str or None)        file name to save result of this operation
                                           if None dont save to file 
            *verbose* (bool or int)        print progress information                            
            ========= ==================== ================================================================
        subStack (dict or None): sub-stack information 
        out (object): object to write progress info to
        
    Returns:
        array: DoG filtered image
    """
    
    timer = Timer();  
    
    dogSize  = getParameter(filterDoGParameter, "size",  size);
    dogSigma = getParameter(filterDoGParameter, "sigma", sigma);
    dogSigma2= getParameter(filterDoGParameter, "sigma2",sigma2);
    dogSave  = getParameter(filterDoGParameter, "save",  save);
    verbose  = getParameter(filterDoGParameter, "verbose",  verbose);
    
    if verbose:
        writeParameter(out = out, head = 'DoG:', size = dogSize, sigma = dogSigma, sigma2 = dogSigma2, save = dogSave);
    #DoG filter
    img = img.astype('float32'); # always convert to float for downstream processing
        
    if not dogSize is None:
        fdog = filterKernel(ftype = 'DoG', size = dogSize, sigma = dogSigma, sigma2 = dogSigma2);
        fdog = fdog.astype('float32');
        #img = correlate(img, fdog);
        #img = scipy.signal.correlate(img, fdog);
        img = correlate(img, fdog);
        #img = convolve(img, fdog, mode = 'same');
        img[img < 0] = 0;
    
    if verbose > 1:
        plotTiling(img);
    
    if not dogSave is None:
        writeSubStack(dogSave, img, subStack = subStack);
    
    if verbose:
        out.write(timer.elapsedTime(head = 'DoG') + '\n');
    
    return img
def greyReconstruction(img, mask, greyReconstructionParameter = None, method = None, size = 3, save = None, verbose = False,
                       subStack = None, out = sys.stdout, **parameter):
    """Calculates the grey reconstruction of the image 
    
    Reconstruction is done z-slice by z-slice.
    
    Arguments:
        img (array): image data
        removeBackGroundParameter (dict):
            ========= ==================== ===========================================================
            Name      Type                 Descritption
            ========= ==================== ===========================================================
            *method*  (tuple or None)      'dilation' or 'erosion', if None return original image
            *size*    (int or tuple)       size of structuring element
            *save*    (str or None)        file name to save result of this operation
                                           if None dont save to file 
            *verbose* (bool or int)        print / plot information about this step 
            ========= ==================== ===========================================================
        subStack (dict or None): sub-stack information 
        verbose (bool): print progress info 
        out (object): object to write progress info to
        
    Returns:
        array: grey reconstructed image
    """
    
    method = getParameter(greyReconstructionParameter, "method", method);
    size   = getParameter(greyReconstructionParameter, "size", size);
    save   = getParameter(greyReconstructionParameter, "save", save);    
    verbose= getParameter(greyReconstructionParameter, "verbose", verbose);   
    
    if verbose:
        writeParameter(out = out, head = 'Grey reconstruction:', method = method, size = size, save = save);
    
    if method is None:
        return img;
    
    timer = Timer();
    
    # background subtraction in each slice
    se = structureElement('Disk', size).astype('uint8');
    for z in range(img.shape[2]):
         #img[:,:,z] = img[:,:,z] - grey_opening(img[:,:,z], structure = structureElement('Disk', (30,30)));
         #img[:,:,z] = img[:,:,z] - morph.grey_opening(img[:,:,z], structure = self.structureELement('Disk', (150,150)));
         img[:,:,z] = img[:,:,z] - reconstruct(img[:,:,z], method = method, selem = se)
    
    if not save is None:
        writeSubStack(save, img, subStack = subStack)

    if verbose > 1:
        plotTiling(img);

    if verbose:
        out.write(timer.elapsedTime(head = 'Grey reconstruction:') + '\n');
    
    return img 
def removeBackground(img, removeBackgroundParameter = None, size = None, save = None, verbose = False,
                     subStack = None, out = sys.stdout, **parameter):
    """Remove background via subtracting a morphological opening from the original image 
    
    Background removal is done z-slice by z-slice.
    
    Arguments:
        img (array): image data
        removeBackGroundParameter (dict):
            ========= ==================== ===========================================================
            Name      Type                 Descritption
            ========= ==================== ===========================================================
            *size*    (tuple or None)      size for the structure element of the morphological opening
                                           if None, do not correct for any background
            *save*    (str or None)        file name to save result of this operation
                                           if None dont save to file
            *verbose* (bool or int)        print / plot information about this step                                 
            ========= ==================== ===========================================================
        subStack (dict or None): sub-stack information 
        verbose (bool): print progress info 
        out (object): object to write progress info to
        
    Returns:
        array: background corrected image
    """
    
    size = getParameter(removeBackgroundParameter, "size", size);
    save = getParameter(removeBackgroundParameter, "save", save);    
    verbose = getParameter(removeBackgroundParameter, "verbose", verbose);   
    
    if verbose:
        writeParameter(out = out, head = 'Background Removal:', size = size, save = save);    
    
    if size is None:    
        return img;
        
    img = io.readData(img);
    
    timer = Timer();
    # background subtraction in each slice
    se = structureElement('Disk', size).astype('uint8');
    for z in range(img.shape[2]):
         #img[:,:,z] = img[:,:,z] - grey_opening(img[:,:,z], structure = structureElement('Disk', (30,30)));
         #img[:,:,z] = img[:,:,z] - morph.grey_opening(img[:,:,z], structure = self.structureELement('Disk', (150,150)));
         img[:,:,z] = img[:,:,z] - cv2.morphologyEx(img[:,:,z], cv2.MORPH_OPEN, se)
    
    if not save is None:
        writeSubStack(save, img, subStack = subStack)

    if verbose > 1:
        plotTiling(10*img);

    if verbose:
        out.write(timer.elapsedTime(head = 'Background') + '\n');
    
    return img
def classifyPixel(img, classifyPixelParameter = None, subStack = None, verbose = False, out = sys.stdout, **parameter):
    """Detect Cells Using a trained classifier in Ilastik
    
    Arguments:from ClearMap.ImageProcessing.CellSizeDetection import detectCellShape, findCellSize, findCellIntensity
        img (array): image data
        classifyPixelParameter (dict):
            ============ ==================== ===========================================================
            Name         Type                 Descritption
            ============ ==================== ===========================================================
            *classifier* (str or  None)       Ilastik project file with trained pixel classifier
            *save*       (str or None)        save the classification propabilities to a file
            *verbose*    (bool or int)        print / plot information about this step 
            ============ ==================== ===========================================================
        subStack (dict or None): sub-stack information 
        verbose (bool): print progress info 
        out (object): object to write progress info to
    
    Returns:
        array: probabilities for each pixel to belong to a class in the classifier, shape is (img.shape, number of classes)
    """

    ilastik.checkInitialized();
    
    classifier = getParameter(classifyPixelParameter, "classifier", None);  
    save       = getParameter(classifyPixelParameter, "save", None);   
    verbose    = getParameter(classifyPixelParameter, "verbose", verbose);
     
    if verbose:
        writeParameter(out = out, head = 'Ilastik classification:', classifier = classifier, save = save);        
    
    
    timer = Timer(); 
        
    #remove background
    #img2 = removeBackground(img, verbose = verbose, out = out, **parameter);
      
    #classify image
    if classifier is None:        
        return img;
    
    imgclass = ilastik.classifyPixel(classifier, img);
    
    if not save is None:
        for i in range(imgclass.shape[4]):
            fn = save[:-4] + '_class_' + str(i) + save[-4:];
            writeSubStack(fn, imgclass[:,:,:,i], subStack = subStack)
      
    if verbose > 1:
        for i in range(imgclass.shape[4]):
            plotTiling(imgclass[:,:,:,i]);
    
    if verbose:
        out.write(timer.elapsedTime(head = 'Ilastik classification') + '\n');    
    
    return imgclass;
def classifyPixel(img, classifyPixelParameter = None, subStack = None, verbose = False, out = sys.stdout, **parameter):
    """Detect Cells Using a trained classifier in Ilastik
    
    Arguments:from ClearMap.ImageProcessing.CellSizeDetection import detectCellShape, findCellSize, findCellIntensity
        img (array): image data
        classifyPixelParameter (dict):
            ============ ==================== ===========================================================
            Name         Type                 Descritption
            ============ ==================== ===========================================================
            *classifier* (str or  None)       Ilastik project file with trained pixel classifier
            *save*       (str or None)        save the classification propabilities to a file
            *verbose*    (bool or int)        print / plot information about this step 
            ============ ==================== ===========================================================
        subStack (dict or None): sub-stack information 
        verbose (bool): print progress info 
        out (object): object to write progress info to
    
    Returns:
        array: probabilities for each pixel to belong to a class in the classifier, shape is (img.shape, number of classes)
    """

    ilastik.checkInitialized();
    
    classifier = getParameter(classifyPixelParameter, "classifier", None);  
    save       = getParameter(classifyPixelParameter, "save", None);   
    verbose    = getParameter(classifyPixelParameter, "verbose", verbose);
     
    if verbose:
        writeParameter(out = out, head = 'Ilastik classification:', classifier = classifier, save = save);        
    
    
    timer = Timer(); 
        
    #remove background
    #img2 = removeBackground(img, verbose = verbose, out = out, **parameter);
      
    #classify image
    if classifier is None:        
        return img;
    
    imgclass = ilastik.classifyPixel(classifier, img);
    
    if not save is None:
        for i in range(imgclass.shape[4]):
            fn = save[:-4] + '_class_' + str(i) + save[-4:];
            writeSubStack(fn, imgclass[:,:,:,i], subStack = subStack)
      
    if verbose > 1:
        for i in range(imgclass.shape[4]):
            plotTiling(imgclass[:,:,:,i]);
    
    if verbose:
        out.write(timer.elapsedTime(head = 'Ilastik classification') + '\n');    
    
    return imgclass;
def classifyCells(img, classifyCellsParameter = None, classifier = None, classindex = 0, save = None, verbose = False,
                  detectCellShapeParameter = None,
                  subStack = None, out = sys.stdout, **parameter):
    """Detect Cells Using a trained classifier in Ilastik
    
    The routine assumes that the first class is identifying the cells.
        
    Arguments:    
        img (array): image data
        classifyPixelParameter (dict):
            ============ ==================== ===========================================================
            Name         Type                 Descritption
            ============ ==================== ===========================================================
            *classifier* (str or  None)       Ilastik project file with trained pixel classifier
            *classindex* (int)                class index considered to be cells
            *save*       (str or None)        save the detected cell pixel to a file
            *verbose*    (bool or int)        print / plot information about this step 
            ============ ==================== ===========================================================
        subStack (dict or None): sub-stack information 
        verbose (bool): print progress info 
        out (object): object to write progress info to
    
    Returns:
        tuple: centers of the cells, intensity measurments
        
    Note:    
        The routine could be potentially refined to make use of background 
        detection in ilastik
    """
    
    classifier = getParameter(classifyCellsParameter, "classifier", classifier);
    classindex = getParameter(classifyCellsParameter, "classindex", classindex);
    save       = getParameter(classifyCellsParameter, "save", save);   
    verbose    = getParameter(classifyCellsParameter, "verbose", verbose);
     
    if verbose:
        writeParameter(out = out, head = 'Ilastik cell detection:', classifier = classifier, classindex = classindex, save = save);        

    timer = Timer(); 

    ilastik.isInitialized();
    
    #remove background
    #img = removeBackground(img, verbose = verbose, out = out, **parameter);
      
    #classify image / assume class 1 are the cells !  
    timer = Timer();  
    
    imgmax = ilastik.classifyPixel(classifier, img);
    #print imgmax.shape
    #max probability gives final class, last axis is class axis
    imgmax = numpy.argmax(imgmax, axis = -1);
    
    if save:
        writeSubStack(save, numpy.asarray(imgmax, dtype = 'float32'), subStack = subStack)    

    # class 0 is used as cells 
    imgmax = imgmax == classindex; # class 1 is used as cells 
    imgshape, nlab = sm.label(imgmax);
    
    if verbose > 1:
        plotTiling(imgmax);
        
    #center of maxima
    centers = findCenterOfMaxima(img, imgmax, imgshape, verbose = verbose, out = out, **parameter);
    
    #intensity of cells
    #cintensity = findIntensity(img, centers, verbose = verbose, out = out, **parameter);

    #intensity of cells in filtered image
    #cintensity2 = findIntensity(img, centers, verbose = verbose, out = out, **parameter);
    
    #if verbose:
    #    out.write(timer.elapsedTime(head = 'Ilastik cell detection') + '\n');    
    
    #return ( centers, numpy.vstack((cintensity, cintensity2)).transpose() );   
    #return ( centers, cintensity ); 
    
    
    #cell size detection
    #detectCellShapeParameter = getParameter(classifyCellsParameter, "detectCellShapeParameter", detectCellShapeParameter);
    #cellShapeThreshold = getParameter(detectCellShapeParameter, "threshold", None);
    
    #if not cellShapeThreshold is None:
        
    # cell shape via watershed
    #imgshape = detectCellShape(img, centers, detectCellShapeParameter = detectCellShapeParameter, verbose = verbose, out = out, **parameter);
    
    #size of cells        
    csize = findCellSize(imgshape, maxLabel = centers.shape[0], out = out, **parameter);
    
    #intensity of cells
    cintensity = findCellIntensity(img, imgshape,  maxLabel = centers.shape[0], verbose = verbose, out = out, **parameter);

    #intensity of cells in background image
    #cintensity2 = findCellIntensity(img2, imgshape,  maxLabel = centers.shape[0], verbose = verbose, out = out, **parameter);

    #intensity of cells in dog filtered image
    #if dogSize is None:
    #    cintensity3 = cintensity2;
    #else:
    #    cintensity3 = findCellIntensity(img3, imgshape,  maxLabel = centers.shape[0], verbose = verbose, out = out, **parameter);
    
    if verbose:
        out.write(timer.elapsedTime(head = 'Ilastik Cell Detection') + '\n');
    
    #remove cell;s of size 0
    idz = csize > 0;
                   
    #return ( centers[idz], numpy.vstack((cintensity[idz], cintensity3[idz], cintensity2[idz], csize[idz])).transpose());        
    return ( centers[idz], numpy.vstack((cintensity[idz], csize[idz])).transpose() ); 
def classifyCells(img, classifyCellsParameter = None, classifier = None, classindex = 0, save = None, verbose = False,
                  detectCellShapeParameter = None,
                  subStack = None, out = sys.stdout, **parameter):
    """Detect Cells Using a trained classifier in Ilastik
    
    The routine assumes that the first class is identifying the cells.
        
    Arguments:    
        img (array): image data
        classifyPixelParameter (dict):
            ============ ==================== ===========================================================
            Name         Type                 Descritption
            ============ ==================== ===========================================================
            *classifier* (str or  None)       Ilastik project file with trained pixel classifier
            *classindex* (int)                class index considered to be cells
            *save*       (str or None)        save the detected cell pixel to a file
            *verbose*    (bool or int)        print / plot information about this step 
            ============ ==================== ===========================================================
        subStack (dict or None): sub-stack information 
        verbose (bool): print progress info 
        out (object): object to write progress info to
    
    Returns:
        tuple: centers of the cells, intensity measurments
        
    Note:    
        The routine could be potentially refined to make use of background 
        detection in ilastik
    """
    
    classifier = getParameter(classifyCellsParameter, "classifier", classifier);
    classindex = getParameter(classifyCellsParameter, "classindex", classindex);
    save       = getParameter(classifyCellsParameter, "save", save);   
    verbose    = getParameter(classifyCellsParameter, "verbose", verbose);
     
    if verbose:
        writeParameter(out = out, head = 'Ilastik cell detection:', classifier = classifier, classindex = classindex, save = save);        

    timer = Timer(); 

    ilastik.isInitialized();
    
    #remove background
    #img = removeBackground(img, verbose = verbose, out = out, **parameter);
      
    #classify image / assume class 1 are the cells !  
    timer = Timer();  
    
    imgmax = ilastik.classifyPixel(classifier, img);
    #print imgmax.shape
    #max probability gives final class, last axis is class axis
    imgmax = numpy.argmax(imgmax, axis = -1);
    
    if save:
        writeSubStack(save, numpy.asarray(imgmax, dtype = 'float32'), subStack = subStack)    

    # class 0 is used as cells 
    imgmax = imgmax == classindex; # class 1 is used as cells 
    imgshape, nlab = sm.label(imgmax);
    
    if verbose > 1:
        plotTiling(imgmax);
        
    #center of maxima
    centers = findCenterOfMaxima(img, imgmax, imgshape, verbose = verbose, out = out, **parameter);
    
    #intensity of cells
    #cintensity = findIntensity(img, centers, verbose = verbose, out = out, **parameter);

    #intensity of cells in filtered image
    #cintensity2 = findIntensity(img, centers, verbose = verbose, out = out, **parameter);
    
    #if verbose:
    #    out.write(timer.elapsedTime(head = 'Ilastik cell detection') + '\n');    
    
    #return ( centers, numpy.vstack((cintensity, cintensity2)).transpose() );   
    #return ( centers, cintensity ); 
    
    
    #cell size detection
    #detectCellShapeParameter = getParameter(classifyCellsParameter, "detectCellShapeParameter", detectCellShapeParameter);
    #cellShapeThreshold = getParameter(detectCellShapeParameter, "threshold", None);
    
    #if not cellShapeThreshold is None:
        
    # cell shape via watershed
    #imgshape = detectCellShape(img, centers, detectCellShapeParameter = detectCellShapeParameter, verbose = verbose, out = out, **parameter);
    
    #size of cells        
    csize = findCellSize(imgshape, maxLabel = centers.shape[0], out = out, **parameter);
    
    #intensity of cells
    cintensity = findCellIntensity(img, imgshape,  maxLabel = centers.shape[0], verbose = verbose, out = out, **parameter);

    #intensity of cells in background image
    #cintensity2 = findCellIntensity(img2, imgshape,  maxLabel = centers.shape[0], verbose = verbose, out = out, **parameter);

    #intensity of cells in dog filtered image
    #if dogSize is None:
    #    cintensity3 = cintensity2;
    #else:
    #    cintensity3 = findCellIntensity(img3, imgshape,  maxLabel = centers.shape[0], verbose = verbose, out = out, **parameter);
    
    if verbose:
        out.write(timer.elapsedTime(head = 'Ilastik Cell Detection') + '\n');
    
    #remove cell;s of size 0
    idz = csize > 0;
                   
    #return ( centers[idz], numpy.vstack((cintensity[idz], cintensity3[idz], cintensity2[idz], csize[idz])).transpose());        
    return ( centers[idz], numpy.vstack((cintensity[idz], csize[idz])).transpose() ); 
def correctIllumination(img, correctIlluminationParameter = None, flatfield = None, background = None, scaling = None, save = None, verbose = False, 
                        subStack = None, out = sys.stdout, **parameter):
    """Correct illumination variations
    
     The intensity image :math:`I(x)` given a flat field :math:`F(x)` and 
     a background :math:`B(x)` the image is corrected to :math:`C(x)` as:
     
     .. math:
         C(x) = \\frac{I(x) - B(x)}{F(x) - B(x)}
         
     If the background is not given :math:`B(x) = 0`. 
     
     The correction is done slice by slice assuming the data was collected with 
     a light sheet microscope.
     
     The image is finally optionally scaled.
  
    Arguments:
        img (array): image data
        findCenterOfMaximaParameter (dict):
            ============ ==================== ===========================================================
            Name         Type                 Descritption
            ============ ==================== ===========================================================
            *flatfield*  (str, None or array) flat field intensities, if None d onot correct image for
                                              illumination, if True the 
            *background* (str, None or array) background image as file name or array
                                              if None background is assumed to be zero
            *scaling*    (str or None)        scale the corrected result by this factor
                                              if 'max'/'mean' scale to keep max/mean invariant
            *save*       (str or None)        save the corrected image to file
            *verbose*    (bool or int)        print / plot information about this step 
            ============ ==================== ===========================================================
        subStack (dict or None): sub-stack information 
        verbose (bool): print progress info 
        out (object): object to write progress info to
    
    Returns:
        array: illumination corrected image
        
        
    References: 
        Fundamentals of Light Microscopy and Electronic Imaging, p 421        
        
    See Also:
        :const:`DefaultFlatFieldLineFile`
    """  
    
    flatfield  = getParameter(correctIlluminationParameter, "flatfield",  flatfield);
    background = getParameter(correctIlluminationParameter, "background", background);
    scaling    = getParameter(correctIlluminationParameter, "scaling",    scaling);
    save       = getParameter(correctIlluminationParameter, "save",       save);
    verbose    = getParameter(correctIlluminationParameter, "verbose",    verbose);

    if verbose:    
        if flatfield is None or isinstance(flatfield, str) or flatfield is True:
            fld = flatfield;
        else:
            fld = "image of size %s" % str(flatfield.shape);
    
        if background is None or isinstance(background, str):
            bkg = background;
        else:
            bkg = "image of size %s" % str(background.shape);
        
        writeParameter(out = out, head = 'Illumination correction:', flatfield = fld, background = bkg, scaling = scaling, save = save);  
    
    
    print subStack;
 
    if not subStack is None:
        x = subStack["x"];
        y = subStack["y"];
    else:
        x = all;
        y = all;
    
    #print "sizes", x, y, img.shape
 
    #read data  
 
    timer = Timer(); 
 
    if flatfield is None:
        return img;
        
    elif flatfield is True:
        # default flatfield correction
    
        if subStack is None:    
            flatfield = flatfieldFromLine(DefaultFlatFieldLineFile, img.shape[0]);
        else:
            dataSize = io.dataSize(subStack["source"]);
            flatfield = flatfieldFromLine(DefaultFlatFieldLineFile, dataSize[0]);
            
    elif isinstance(flatfield, str):
        # point or image file
        if io.isPointFile(flatfield):
            if subStack is None:    
                flatfield = flatfieldFromLine(flatfield, img.shape[0]);
            else:
               dataSize = io.dataSize(subStack["source"]);
               flatfield = flatfieldFromLine(flatfield, dataSize[0]);
        else:
            flatfield = io.readData(flatfield);
    
    ffmean = flatfield.mean();    
    ffmax = flatfield.max();

    #correct for subset
    flatfield = io.readData(flatfield, x = x, y = y);   
    
    background = io.readData(background, x = x, y = y);
    
    if flatfield.shape != img[:,:,0].shape:
        raise RuntimeError("correctIllumination: flatfield does not match image size: %s vs %s" % (flatfield.shape,  img[:,:,0].shape));
    
    #convert to float for scaling
    dtype = img.dtype;
    img = img.astype('float32');
    flatfield = flatfield.astype('float32');
    
    # illumination correction in each slice
    if background is None:
        for z in range(img.shape[2]):
            img[:,:,z] = img[:,:,z] / flatfield;
    else:
        if background.shape != flatfield.shape:
            raise RuntimeError("correctIllumination: background does not match image size: %s vs %s" % (background.shape,  img[:,:,0].shape));        
        background = background.astype('float32');

        flatfield = (flatfield - background);
        for z in range(img.shape[2]):
            img[:,:,z] = (img[:,:,z] - background) / flatfield;
    
        
    # rescale
    if scaling is True:
        scaling = "mean";
    
    if isinstance(scaling, str):
        if scaling.lower() == "mean":
            # scale back by average flat field correction:
            sf = ffmean;
        elif scaling.lower() == "max":
            sf = ffmax;
        else:
            raise RuntimeError('Scaling not "Max" or "Mean" but %s' % scaling);
    else:
        sf = scaling;
      
    if verbose:
         writeParameter(out = out, head = 'Illumination correction:',  scaling = sf);
    
        
    
    if not sf is None:
        img = img * sf;
        img = img.astype(dtype);
    
    
    #write result for inspection
    if not save is None:
        writeSubStack(save, img, subStack = subStack);    
    
    #plot result for inspection
    if verbose > 1:
        plotTiling(img);
    
    if verbose:
        out.write(timer.elapsedTime(head = 'Illumination correction') + '\n');    
    
    return img 
Esempio n. 9
0
def filterLinear(img,
                 filterLinearParameter=None,
                 ftype=None,
                 size=None,
                 sigma=None,
                 sigma2=None,
                 save=None,
                 subStack=None,
                 verbose=False,
                 out=sys.stdout,
                 **parameter):
    """Applies a linear filter to the image
    
    Arguments:
        img (array): image data
        filterLinearParameter (dict):
            ========= ==================== ================================================================
            Name      Type                 Descritption
            ========= ==================== ================================================================
            *ftype*   (str or None)        the type of the filter, see :ref:`FilterTypes`
                                           if None do ot perform any fitlering
            *size*    (tuple or None)      size for the filter 
                                           if None, do not perform filtering
            *sigma*   (tuple or None)      std of outer Guassian, if None autmatically determined from size
            *sigma2*  (tuple or None)      std of inner Guassian, if None autmatically determined from size
            *save*    (str or None)        file name to save result of this operation
                                           if None dont save to file 
            *verbose* (bool or int)        print progress information       
            ========= ==================== ================================================================
        subStack (dict or None): sub-stack information 
        verbose (bool): print progress info 
        out (object): object to write progress info to
        
    Returns:
        array: filtered image
        
    Note:
        Converts image to float32 type if filter is active!
    """

    timer = Timer()

    ftype = getParameter(filterLinearParameter, "ftype", ftype)
    size = getParameter(filterLinearParameter, "size", size)
    sigma = getParameter(filterLinearParameter, "sigma", sigma)
    sigma2 = getParameter(filterLinearParameter, "sigma2", sigma2)
    save = getParameter(filterLinearParameter, "save", save)
    verbose = getParameter(filterLinearParameter, "verbose", verbose)

    if verbose:
        writeParameter(out=out,
                       head='Linear Filter:',
                       ftype=ftype,
                       size=size,
                       sigma=sigma,
                       sigma2=sigma2,
                       save=save)

    if ftype is None:
        return img

    #DoG filter
    img = img.astype('float32')
    # always convert to float for downstream processing

    if not size is None:
        fil = filterKernel(ftype=ftype, size=size, sigma=sigma, sigma2=sigma2)
        fil = fil.astype('float32')
        #img = correlate(img, fdog);
        #img = scipy.signal.correlate(img, fdog);
        img = correlate(img, fil)
        #img = convolve(img, fdog, mode = 'same');
        img[img < 0] = 0

    if verbose > 1:
        plotTiling(img)

    if not save is None:
        writeSubStack(save, img, subStack=subStack)

    if verbose:
        out.write(timer.elapsedTime(head='Linear Filter') + '\n')

    return img
Esempio n. 10
0
def greyReconstruction(img,
                       mask,
                       greyReconstructionParameter=None,
                       method=None,
                       size=3,
                       save=None,
                       verbose=False,
                       subStack=None,
                       out=sys.stdout,
                       **parameter):
    """Calculates the grey reconstruction of the image 
    
    Reconstruction is done z-slice by z-slice.
    
    Arguments:
        img (array): image data
        removeBackGroundParameter (dict):
            ========= ==================== ===========================================================
            Name      Type                 Descritption
            ========= ==================== ===========================================================
            *method*  (tuple or None)      'dilation' or 'erosion', if None return original image
            *size*    (int or tuple)       size of structuring element
            *save*    (str or None)        file name to save result of this operation
                                           if None dont save to file 
            *verbose* (bool or int)        print / plot information about this step 
            ========= ==================== ===========================================================
        subStack (dict or None): sub-stack information 
        verbose (bool): print progress info 
        out (object): object to write progress info to
        
    Returns:
        array: grey reconstructed image
    """

    method = getParameter(greyReconstructionParameter, "method", method)
    size = getParameter(greyReconstructionParameter, "size", size)
    save = getParameter(greyReconstructionParameter, "save", save)
    verbose = getParameter(greyReconstructionParameter, "verbose", verbose)

    if verbose:
        writeParameter(out=out,
                       head='Grey reconstruction:',
                       method=method,
                       size=size,
                       save=save)

    if method is None:
        return img

    timer = Timer()

    # background subtraction in each slice
    se = structureElement('Disk', size).astype('uint8')
    for z in range(img.shape[2]):
        #img[:,:,z] = img[:,:,z] - grey_opening(img[:,:,z], structure = structureElement('Disk', (30,30)));
        #img[:,:,z] = img[:,:,z] - morph.grey_opening(img[:,:,z], structure = self.structureELement('Disk', (150,150)));
        img[:, :, z] = img[:, :, z] - reconstruct(
            img[:, :, z], method=method, selem=se)

    if not save is None:
        writeSubStack(save, img, subStack=subStack)

    if verbose > 1:
        plotTiling(img)

    if verbose:
        out.write(timer.elapsedTime(head='Grey reconstruction:') + '\n')

    return img
def correctIllumination(img,
                        correctIlluminationParameter=None,
                        flatfield=None,
                        background=None,
                        scaling=None,
                        save=None,
                        verbose=False,
                        subStack=None,
                        out=sys.stdout,
                        **parameter):
    """Correct illumination variations
    
     The intensity image :math:`I(x)` given a flat field :math:`F(x)` and 
     a background :math:`B(x)` the image is corrected to :math:`C(x)` as:
     
     .. math:
         C(x) = \\frac{I(x) - B(x)}{F(x) - B(x)}
         
     If the background is not given :math:`B(x) = 0`. 
     
     The correction is done slice by slice assuming the data was collected with 
     a light sheet microscope.
     
     The image is finally optionally scaled.
  
    Arguments:
        img (array): image data
        findCenterOfMaximaParameter (dict):
            ============ ==================== ===========================================================
            Name         Type                 Descritption
            ============ ==================== ===========================================================
            *flatfield*  (str, None or array) flat field intensities, if None d onot correct image for
                                              illumination, if True the 
            *background* (str, None or array) background image as file name or array
                                              if None background is assumed to be zero
            *scaling*    (str or None)        scale the corrected result by this factor
                                              if 'max'/'mean' scale to keep max/mean invariant
            *save*       (str or None)        save the corrected image to file
            *verbose*    (bool or int)        print / plot information about this step 
            ============ ==================== ===========================================================
        subStack (dict or None): sub-stack information 
        verbose (bool): print progress info 
        out (object): object to write progress info to
    
    Returns:
        array: illumination corrected image
        
        
    References: 
        Fundamentals of Light Microscopy and Electronic Imaging, p 421        
        
    See Also:
        :const:`DefaultFlatFieldLineFile`
    """

    flatfield = getParameter(correctIlluminationParameter, "flatfield",
                             flatfield)
    background = getParameter(correctIlluminationParameter, "background",
                              background)
    scaling = getParameter(correctIlluminationParameter, "scaling", scaling)
    save = getParameter(correctIlluminationParameter, "save", save)
    verbose = getParameter(correctIlluminationParameter, "verbose", verbose)

    if verbose:
        if flatfield is None or isinstance(flatfield,
                                           str) or flatfield is True:
            fld = flatfield
        else:
            fld = "image of size %s" % str(flatfield.shape)

        if background is None or isinstance(background, str):
            bkg = background
        else:
            bkg = "image of size %s" % str(background.shape)

        writeParameter(out=out,
                       head='Illumination correction:',
                       flatfield=fld,
                       background=bkg,
                       scaling=scaling,
                       save=save)

    print subStack

    if not subStack is None:
        x = subStack["x"]
        y = subStack["y"]
    else:
        x = all
        y = all

    #print "sizes", x, y, img.shape

    #read data

    timer = Timer()

    if flatfield is None:
        return img

    elif flatfield is True:
        # default flatfield correction

        if subStack is None:
            flatfield = flatfieldFromLine(DefaultFlatFieldLineFile,
                                          img.shape[0])
        else:
            dataSize = io.dataSize(subStack["source"])
            flatfield = flatfieldFromLine(DefaultFlatFieldLineFile,
                                          dataSize[0])

    elif isinstance(flatfield, str):
        # point or image file
        if io.isPointFile(flatfield):
            if subStack is None:
                flatfield = flatfieldFromLine(flatfield, img.shape[0])
            else:
                dataSize = io.dataSize(subStack["source"])
                flatfield = flatfieldFromLine(flatfield, dataSize[0])
        else:
            flatfield = io.readData(flatfield)

    ffmean = flatfield.mean()
    ffmax = flatfield.max()

    #correct for subset
    flatfield = io.readData(flatfield, x=x, y=y)

    background = io.readData(background, x=x, y=y)

    if flatfield.shape != img[:, :, 0].shape:
        raise RuntimeError(
            "correctIllumination: flatfield does not match image size: %s vs %s"
            % (flatfield.shape, img[:, :, 0].shape))

    #convert to float for scaling
    dtype = img.dtype
    img = img.astype('float32')
    flatfield = flatfield.astype('float32')

    # illumination correction in each slice
    if background is None:
        for z in range(img.shape[2]):
            img[:, :, z] = img[:, :, z] / flatfield
    else:
        if background.shape != flatfield.shape:
            raise RuntimeError(
                "correctIllumination: background does not match image size: %s vs %s"
                % (background.shape, img[:, :, 0].shape))
        background = background.astype('float32')

        flatfield = (flatfield - background)
        for z in range(img.shape[2]):
            img[:, :, z] = (img[:, :, z] - background) / flatfield

    # rescale
    if scaling is True:
        scaling = "mean"

    if isinstance(scaling, str):
        if scaling.lower() == "mean":
            # scale back by average flat field correction:
            sf = ffmean
        elif scaling.lower() == "max":
            sf = ffmax
        else:
            raise RuntimeError('Scaling not "Max" or "Mean" but %s' % scaling)
    else:
        sf = scaling

    if verbose:
        writeParameter(out=out, head='Illumination correction:', scaling=sf)

    if not sf is None:
        img = img * sf
        img = img.astype(dtype)

    #write result for inspection
    if not save is None:
        writeSubStack(save, img, subStack=subStack)

    #plot result for inspection
    if verbose > 1:
        plotTiling(img)

    if verbose:
        out.write(timer.elapsedTime(head='Illumination correction') + '\n')

    return img
Esempio n. 12
0
def filterDoG(img,
              filterDoGParameter=None,
              size=None,
              sigma=None,
              sigma2=None,
              save=None,
              verbose=None,
              subStack=None,
              out=sys.stdout,
              **parameter):
    """Difference of Gaussians (DoG) filter step
    
    Arguments:
        img (array): image data
        filterDoGParameter (dict):
            ========= ==================== ================================================================
            Name      Type                 Descritption
            ========= ==================== ================================================================
            *size*    (tuple or None)      size for the DoG filter 
                                           if None, do not correct for any background
            *sigma*   (tuple or None)      std of outer Guassian, if None autmatically determined from size
            *sigma2*  (tuple or None)      std of inner Guassian, if None autmatically determined from size
            *save*    (str or None)        file name to save result of this operation
                                           if None dont save to file 
            *verbose* (bool or int)        print progress information                            
            ========= ==================== ================================================================
        subStack (dict or None): sub-stack information 
        out (object): object to write progress info to
        
    Returns:
        array: DoG filtered image
    """

    timer = Timer()

    dogSize = getParameter(filterDoGParameter, "size", size)
    dogSigma = getParameter(filterDoGParameter, "sigma", sigma)
    dogSigma2 = getParameter(filterDoGParameter, "sigma2", sigma2)
    dogSave = getParameter(filterDoGParameter, "save", save)
    verbose = getParameter(filterDoGParameter, "verbose", verbose)

    if verbose:
        writeParameter(out=out,
                       head='DoG:',
                       size=dogSize,
                       sigma=dogSigma,
                       sigma2=dogSigma2,
                       save=dogSave)
    #DoG filter
    img = img.astype('float32')
    # always convert to float for downstream processing

    if not dogSize is None:
        fdog = filterKernel(ftype='DoG',
                            size=dogSize,
                            sigma=dogSigma,
                            sigma2=dogSigma2)
        fdog = fdog.astype('float32')
        #img = correlate(img, fdog);
        #img = scipy.signal.correlate(img, fdog);
        img = correlate(img, fdog)
        #img = convolve(img, fdog, mode = 'same');
        img[img < 0] = 0

    if verbose > 1:
        plotTiling(img)

    if not dogSave is None:
        writeSubStack(dogSave, img, subStack=subStack)

    if verbose:
        out.write(timer.elapsedTime(head='DoG') + '\n')

    return img
Esempio n. 13
0
def filterLinear(img, filterLinearParameter = None, ftype = None, size = None, sigma = None, sigma2 = None, save = None, 
                 subStack = None, verbose = False, out = sys.stdout, **parameter):
    """Applies a linear filter to the image
    
    Arguments:
        img (array): image data
        filterLinearParameter (dict):
            ========= ==================== ================================================================
            Name      Type                 Descritption
            ========= ==================== ================================================================
            *ftype*   (str or None)        the type of the filter, see :ref:`FilterTypes`
                                           if None do ot perform any fitlering
            *size*    (tuple or None)      size for the filter 
                                           if None, do not perform filtering
            *sigma*   (tuple or None)      std of outer Guassian, if None autmatically determined from size
            *sigma2*  (tuple or None)      std of inner Guassian, if None autmatically determined from size
            *save*    (str or None)        file name to save result of this operation
                                           if None dont save to file 
            *verbose* (bool or int)        print progress information       
            ========= ==================== ================================================================
        subStack (dict or None): sub-stack information 
        verbose (bool): print progress info 
        out (object): object to write progress info to
        
    Returns:
        array: filtered image
        
    Note:
        Converts image to float32 type if filter is active!
    """
    
    timer = Timer();  
    
    ftype   = getParameter(filterLinearParameter, "ftype",  ftype);
    size    = getParameter(filterLinearParameter, "size",   size);
    sigma   = getParameter(filterLinearParameter, "sigma",  sigma);
    sigma2  = getParameter(filterLinearParameter, "sigma2", sigma2);
    save    = getParameter(filterLinearParameter, "save",   save);
    verbose = getParameter(filterLinearParameter, "verbose",verbose);

    if verbose:
        writeParameter(out = out, head = 'Linear Filter:', ftype = ftype, size = size, sigma = sigma, sigma2 = sigma2, save = save);

    if ftype is None:
        return img;
    
    #DoG filter
    img = img.astype('float32'); # always convert to float for downstream processing
        
    if not size is None:
        fil = filterKernel(ftype = ftype, size = size, sigma = sigma, sigma2 = sigma2);
        fil = fil.astype('float32');
        #img = correlate(img, fdog);
        #img = scipy.signal.correlate(img, fdog);
        img = correlate(img, fil);
        #img = convolve(img, fdog, mode = 'same');
        img[img < 0] = 0;
    
    if verbose > 1:
        plotTiling(img);
    
    if not save is None:
        writeSubStack(save, img, subStack = subStack);
    
    if verbose:
        out.write(timer.elapsedTime(head = 'Linear Filter') + '\n');
    
    return img