예제 #1
0
import os
import ClearMap.Settings as settings
filename = os.path.join(settings.ClearMapPath,
                        'Test/Data/ImageAnalysis/cfos-substack.tif')
import ClearMap.Visualization.Plot as plt
import ClearMap.IO as io
data = io.readData(filename, z=(0, 26))
import ClearMap.ImageProcessing.BackgroundRemoval as bgr
dataBGR = bgr.removeBackground(data.astype('float'), size=(3, 3), verbose=True)
from ClearMap.ImageProcessing.Filter.DoGFilter import filterDoG
dataDoG = filterDoG(dataBGR, size=(8, 8, 4), verbose=True)
from ClearMap.ImageProcessing.MaximaDetection import findExtendedMaxima
dataMax = findExtendedMaxima(dataDoG, hMax=None, verbose=True, threshold=10)
from ClearMap.ImageProcessing.MaximaDetection import findCenterOfMaxima
cells = findCenterOfMaxima(data, dataMax)
from ClearMap.ImageProcessing.CellSizeDetection import detectCellShape
dataShape = detectCellShape(dataDoG, cells, threshold=15)
plt.plotOverlayLabel(dataDoG / dataDoG.max(), dataShape, z=(10, 16))
예제 #2
0
def detectSpots(img,
                detectSpotsParameter=None,
                correctIlluminationParameter=None,
                removeBackgroundParameter=None,
                filterDoGParameter=None,
                findExtendedMaximaParameter=None,
                detectCellShapeParameter=None,
                verbose=False,
                out=sys.stdout,
                **parameter):
    """Detect Cells in 3d grayscale image using DoG filtering and maxima detection
    
    Effectively this function performs the following steps:
        * illumination correction via :func:`~ClearMap.ImageProcessing.IlluminationCorrection.correctIllumination`
        * background removal via :func:`~ClearMap.ImageProcessing.BackgroundRemoval.removeBackground`
        * difference of Gaussians (DoG) filter via :func:`~ClearMap.ImageProcessing.Filter.filterDoG`
        * maxima detection via :func:`~ClearMap.ImageProcessing.MaximaDetection.findExtendedMaxima`
        * cell shape detection via :func:`~ClearMap.ImageProcessing.CellSizeDetection.detectCellShape`
        * cell intensity and size measurements via: :func:`~ClearMap.ImageProcessing.CellSizeDetection.findCellIntensity`,
          :func:`~ClearMap.ImageProcessing.CellSizeDetection.findCellSize`. 
    
    Note: 
        Processing steps are done in place to save memory.
        
    Arguments:
        img (array): image data
        detectSpotParameter: image processing parameter as described in the individual sub-routines
        verbose (bool): print progress information
        out (object): object to print progress information to
        
    Returns:
        tuple: tuple of arrays (cell coordinates, raw intensity, fully filtered intensty, illumination and background corrected intensity [, cell size])
    """

    timer = Timer()

    # correct illumination
    correctIlluminationParameter = getParameter(
        detectSpotsParameter, "correctIlluminationParameter",
        correctIlluminationParameter)
    img1 = img.copy()
    img1 = correctIllumination(
        img1,
        correctIlluminationParameter=correctIlluminationParameter,
        verbose=verbose,
        out=out,
        **parameter)

    # background subtraction in each slice
    #img2 = img.copy();
    removeBackgroundParameter = getParameter(detectSpotsParameter,
                                             "removeBackgroundParameter",
                                             removeBackgroundParameter)
    img2 = removeBackground(
        img1,
        removeBackgroundParameter=removeBackgroundParameter,
        verbose=verbose,
        out=out,
        **parameter)

    #DoG filter
    filterDoGParameter = getParameter(detectSpotsParameter,
                                      "filterDoGParameter", filterDoGParameter)
    dogSize = getParameter(filterDoGParameter, "size", None)
    #img3 = img2.copy();
    img3 = filterDoG(img2,
                     filterDoGParameter=filterDoGParameter,
                     verbose=verbose,
                     out=out,
                     **parameter)

    # extended maxima
    findExtendedMaximaParameter = getParameter(detectSpotsParameter,
                                               "findExtendedMaximaParameter",
                                               findExtendedMaximaParameter)
    hMax = getParameter(findExtendedMaximaParameter, "hMax", None)
    imgmax = findExtendedMaxima(
        img3,
        findExtendedMaximaParameter=findExtendedMaximaParameter,
        verbose=verbose,
        out=out,
        **parameter)

    #center of maxima
    if not hMax is None:
        centers = findCenterOfMaxima(img,
                                     imgmax,
                                     verbose=verbose,
                                     out=out,
                                     **parameter)
    else:
        centers = findPixelCoordinates(imgmax,
                                       verbose=verbose,
                                       out=out,
                                       **parameter)

    #cell size detection
    detectCellShapeParameter = getParameter(detectSpotsParameter,
                                            "detectCellShapeParameter",
                                            detectCellShapeParameter)
    cellShapeThreshold = getParameter(detectCellShapeParameter, "threshold",
                                      None)
    if not cellShapeThreshold is None:

        # cell shape via watershed
        imgshape = detectCellShape(
            img2,
            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="Spot 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())

    else:
        #intensity of cells
        cintensity = findIntensity(img,
                                   centers,
                                   verbose=verbose,
                                   out=out,
                                   **parameter)

        #intensity of cells in background image
        cintensity2 = findIntensity(img2,
                                    centers,
                                    verbose=verbose,
                                    out=out,
                                    **parameter)

        #intensity of cells in dog filtered image
        if dogSize is None:
            cintensity3 = cintensity2
        else:
            cintensity3 = findIntensity(img3,
                                        centers,
                                        verbose=verbose,
                                        out=out,
                                        **parameter)

        if verbose:
            out.write(timer.elapsedTime(head="Spot Detection") + "\n")

        return (centers, numpy.vstack(
            (cintensity, cintensity3, cintensity2)).transpose())
예제 #3
0
def detectSpots(img,
                detectSpotsParameter=None,
                correctIlluminationParameter=None,
                removeBackgroundParameter=None,
                filterDoGParameter=None,
                findExtendedMaximaParameter=None,
                detectCellShapeParameter=None,
                verbose=False,
                out=sys.stdout,
                **parameter):
    """Detect Cells in 3d grayscale image using DoG filtering and maxima detection
    
    Effectively this function performs the following steps:
        * illumination correction via :func:`~ClearMap.ImageProcessing.IlluminationCorrection.correctIllumination`
        * background removal via :func:`~ClearMap.ImageProcessing.BackgroundRemoval.removeBackground`
        * difference of Gaussians (DoG) filter via :func:`~ClearMap.ImageProcessing.Filter.filterDoG`
        * maxima detection via :func:`~ClearMap.ImageProcessing.MaximaDetection.findExtendedMaxima`
        * cell shape detection via :func:`~ClearMap.ImageProcessing.CellSizeDetection.detectCellShape`
        * cell intensity and size measurements via: :func:`~ClearMap.ImageProcessing.CellSizeDetection.findCellIntensity`,
          :func:`~ClearMap.ImageProcessing.CellSizeDetection.findCellSize`. 
    detectCells
    Note: 
        Processing steps are done in place to save memory.
        
    Arguments:
        img (array): image data
        detectSpotParameter: image processing parameter as described in the individual sub-routines
        verbose (bool): print progress information
        out (object): object to print progress information to
        
    Returns:
        tuple: tuple of arrays (cell coordinates, raw intensity, fully filtered intensty, illumination and background corrected intensity [, cell size])
    """

    timer = Timer()

    # normalize data -> to check
    #img = img.astype('float');
    #dmax = 0.075 * 65535;
    #ids = img > dmax;
    #img[ids] = dmax;
    #img /= dmax;
    #out.write(timer.elapsedTime(head = 'Normalization'));
    #img = dataset[600:1000,1600:1800,800:830];
    #img = dataset[600:1000,:,800:830];

    # correct illumination
    correctIlluminationParameter = getParameter(
        detectSpotsParameter, "correctIlluminationParameter",
        correctIlluminationParameter)
    img1 = img.copy()
    img1 = correctIllumination(
        img1,
        correctIlluminationParameter=correctIlluminationParameter,
        verbose=verbose,
        out=out,
        **parameter)

    # background subtraction in each slice
    #img2 = img.copy();
    removeBackgroundParameter = getParameter(detectSpotsParameter,
                                             "removeBackgroundParameter",
                                             removeBackgroundParameter)
    img2 = removeBackground(
        img1,
        removeBackgroundParameter=removeBackgroundParameter,
        verbose=verbose,
        out=out,
        **parameter)

    # mask
    #timer.reset();
    #if mask == None: #explicit mask
    #    mask = img > 0.01;
    #    mask = binary_opening(mask, self.structureELement('Disk', (3,3,3)));
    #img[img < 0.01] = 0; # masking in place  # extended maxima
    #out.write(timer.elapsedTime(head = 'Mask'));

    #DoG filter
    filterDoGParameter = getParameter(detectSpotsParameter,
                                      "filterDoGParameter", filterDoGParameter)
    dogSize = getParameter(filterDoGParameter, "size", None)
    #img3 = img2.copy();
    img3 = filterDoG(img2,
                     filterDoGParameter=filterDoGParameter,
                     verbose=verbose,
                     out=out,
                     **parameter)

    # normalize
    #    imax = img.max();
    #    if imax == 0:
    #        imax = 1;
    #    img /= imax;

    # extended maxima
    findExtendedMaximaParameter = getParameter(detectSpotsParameter,
                                               "findExtendedMaximaParameter",
                                               findExtendedMaximaParameter)
    hMax = getParameter(findExtendedMaximaParameter, "hMax", None)
    imgmax = findExtendedMaxima(
        img3,
        findExtendedMaximaParameter=findExtendedMaximaParameter,
        verbose=verbose,
        out=out,
        **parameter)

    #center of maxima
    if not hMax is None:
        centers = findCenterOfMaxima(img,
                                     imgmax,
                                     verbose=verbose,
                                     out=out,
                                     **parameter)
    else:
        centers = findPixelCoordinates(imgmax,
                                       verbose=verbose,
                                       out=out,
                                       **parameter)

    #cell size detection
    detectCellShapeParameter = getParameter(detectSpotsParameter,
                                            "detectCellShapeParameter",
                                            detectCellShapeParameter)
    cellShapeThreshold = getParameter(detectCellShapeParameter, "threshold",
                                      None)
    if not cellShapeThreshold is None:

        # cell shape via watershed
        imgshape = detectCellShape(
            img2,
            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='Spot 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())

    else:
        #intensity of cells
        cintensity = findIntensity(img,
                                   centers,
                                   verbose=verbose,
                                   out=out,
                                   **parameter)

        #intensity of cells in background image
        cintensity2 = findIntensity(img2,
                                    centers,
                                    verbose=verbose,
                                    out=out,
                                    **parameter)

        #intensity of cells in dog filtered image
        if dogSize is None:
            cintensity3 = cintensity2
        else:
            cintensity3 = findIntensity(img3,
                                        centers,
                                        verbose=verbose,
                                        out=out,
                                        **parameter)

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

        return (centers, numpy.vstack(
            (cintensity, cintensity3, cintensity2)).transpose())
예제 #4
0
    img = io.readData(cfos_fn)
    img = img[..., numpy.newaxis]
    img = img.astype('int16')
    # converting data to smaller integer types can be more memory efficient!

    imgB = bgr.removeBackground(img, size=backgroundSize, verbose=True)
    io.writeData(os.path.join(resultDir, fName + '_BackgroundRemoval.tif'),
                 imgB)

    ##image filter
    imgD = filterDoG(img, size=DoGSize, verbose=True)
    io.writeData(os.path.join(resultDir, fName + '_FilterDoG.tif'), imgD)

    #Detect Maxima
    imgMaxima = findExtendedMaxima(img,
                                   hMax=None,
                                   verbose=True,
                                   threshold=maximaThresh)
    points = findCenterOfMaxima(img, imgMaxima, verbose=True)
    points = points.astype('int16')

    #threshold intensities
    dataShape = detectCellShape(imgD,
                                points,
                                threshold=cellShapeThresh,
                                verbose=True)
    cellSizesPre = findCellSize(dataShape, maxlabel=points.shape[0])

    io.writeData(os.path.join(resultDir, fName + '_CellShapes.tif'),
                 255 * dataShape.astype('int32') / dataShape.max())
    #cintensity = findIntensity(imgD, points);
import os
import ClearMap.Settings as settings

filename = os.path.join(settings.ClearMapPath, "Test/Data/ImageAnalysis/cfos-substack.tif")
import ClearMap.Visualization.Plot as plt
import ClearMap.IO as io

data = io.readData(filename, z=(0, 26))
import ClearMap.ImageProcessing.BackgroundRemoval as bgr

dataBGR = bgr.removeBackground(data.astype("float"), size=(3, 3), verbose=True)
from ClearMap.ImageProcessing.Filter.DoGFilter import filterDoG

dataDoG = filterDoG(dataBGR, size=(8, 8, 4), verbose=True)
from ClearMap.ImageProcessing.MaximaDetection import findExtendedMaxima

dataMax = findExtendedMaxima(dataDoG, hMax=None, verbose=True, threshold=10)
from ClearMap.ImageProcessing.MaximaDetection import findCenterOfMaxima

cells = findCenterOfMaxima(data, dataMax)
plt.plotOverlayPoints(data, cells, z=(10, 16))
예제 #6
0
def detectSpots(img, detectSpotsParameter = None, correctIlluminationParameter = None, removeBackgroundParameter = None,
                filterDoGParameter = None, findExtendedMaximaParameter = None, detectCellShapeParameter = None,
                verbose = False, out = sys.stdout, **parameter):
    """Detect Cells in 3d grayscale image using DoG filtering and maxima detection
    
    Effectively this function performs the following steps:
        * illumination correction via :func:`~ClearMap.ImageProcessing.IlluminationCorrection.correctIllumination`
        * background removal via :func:`~ClearMap.ImageProcessing.BackgroundRemoval.removeBackground`
        * difference of Gaussians (DoG) filter via :func:`~ClearMap.ImageProcessing.Filter.filterDoG`
        * maxima detection via :func:`~ClearMap.ImageProcessing.MaximaDetection.findExtendedMaxima`
        * cell shape detection via :func:`~ClearMap.ImageProcessing.CellSizeDetection.detectCellShape`
        * cell intensity and size measurements via: :func:`~ClearMap.ImageProcessing.CellSizeDetection.findCellIntensity`,
          :func:`~ClearMap.ImageProcessing.CellSizeDetection.findCellSize`. 
    
    Note: 
        Processing steps are done in place to save memory.
        
    Arguments:
        img (array): image data
        detectSpotParameter: image processing parameter as described in the individual sub-routines
        verbose (bool): print progress information
        out (object): object to print progress information to
        
    Returns:
        tuple: tuple of arrays (cell coordinates, raw intensity, fully filtered intensty, illumination and background corrected intensity [, cell size])
    """

    timer = Timer();
    
    # normalize data -> to check
    #img = img.astype('float');
    #dmax = 0.075 * 65535;
    #ids = img > dmax;
    #img[ids] = dmax;
    #img /= dmax; 
    #out.write(timer.elapsedTime(head = 'Normalization'));
    #img = dataset[600:1000,1600:1800,800:830];
    #img = dataset[600:1000,:,800:830];
    
    # correct illumination
    correctIlluminationParameter = getParameter(detectSpotsParameter, "correctIlluminationParameter", correctIlluminationParameter);
    img1 = img.copy();
    img1 = correctIllumination(img1, correctIlluminationParameter = correctIlluminationParameter, verbose = verbose, out = out, **parameter)   

    # background subtraction in each slice
    #img2 = img.copy();
    removeBackgroundParameter = getParameter(detectSpotsParameter, "removeBackgroundParameter", removeBackgroundParameter);
    img2 = removeBackground(img1, removeBackgroundParameter = removeBackgroundParameter, verbose = verbose, out = out, **parameter)   
    
    # mask
    #timer.reset();
    #if mask == None: #explicit mask
    #    mask = img > 0.01;
    #    mask = binary_opening(mask, self.structureELement('Disk', (3,3,3)));
    #img[img < 0.01] = 0; # masking in place  # extended maxima
    #out.write(timer.elapsedTime(head = 'Mask'));    
    
    #DoG filter
    filterDoGParameter = getParameter(detectSpotsParameter, "filterDoGParameter", filterDoGParameter);
    dogSize = getParameter(filterDoGParameter, "size", None);
    #img3 = img2.copy();    
    img3 = filterDoG(img2, filterDoGParameter = filterDoGParameter, verbose = verbose, out = out, **parameter);
    
    # normalize    
    #    imax = img.max();
    #    if imax == 0:
    #        imax = 1;
    #    img /= imax;
    
    # extended maxima
    findExtendedMaximaParameter = getParameter(detectSpotsParameter, "findExtendedMaximaParameter", findExtendedMaximaParameter);
    hMax = getParameter(findExtendedMaximaParameter, "hMax", None);
    imgmax = findExtendedMaxima(img3, findExtendedMaximaParameter = findExtendedMaximaParameter, verbose = verbose, out = out, **parameter);
    
    #center of maxima
    if not hMax is None:
        centers = findCenterOfMaxima(img, imgmax, verbose = verbose, out = out, **parameter);
    else:
        centers = findPixelCoordinates(imgmax, verbose = verbose, out = out, **parameter);
    
    #cell size detection
    detectCellShapeParameter = getParameter(detectSpotsParameter, "detectCellShapeParameter", detectCellShapeParameter);
    cellShapeThreshold = getParameter(detectCellShapeParameter, "threshold", None);
    if not cellShapeThreshold is None:
        
        # cell shape via watershed
        imgshape = detectCellShape(img2, 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 = 'Spot 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());        
        
    
    else:
        #intensity of cells
        cintensity = findIntensity(img, centers, verbose = verbose, out = out, **parameter);

        #intensity of cells in background image
        cintensity2 = findIntensity(img2, centers, verbose = verbose, out = out, **parameter);
    
        #intensity of cells in dog filtered image
        if dogSize is None:
            cintensity3 = cintensity2;
        else:
            cintensity3 = findIntensity(img3, centers, verbose = verbose, out = out, **parameter);

        if verbose:
            out.write(timer.elapsedTime(head = 'Spot Detection') + '\n');
    
        return ( centers, numpy.vstack((cintensity, cintensity3, cintensity2)).transpose());