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())
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())
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() );
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) from ClearMap.ImageProcessing.CellSizeDetection import findCellSize, findCellIntensity cellSizes = findCellSize(dataShape, maxLabel=cells.shape[0]) cellIntensities = findCellIntensity(dataBGR, dataShape, maxLabel=cells.shape[0]) import matplotlib.pyplot as mpl mpl.figure() mpl.plot(cellSizes, cellIntensities, '.') mpl.xlabel('cell size [voxel]') mpl.ylabel('cell intensity [au]')
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); #points, intensities = thresholdPoints(points,cintensity, threshold = (5,20), row = (1,1)); points, cellSizesPost = thresholdPoints(points, cellSizesPre, threshold=pointsThresh, row=threshType) #pdb.set_trace() #io.writeData(os.path.join(homeDir, 'Results/OverlayWatershed.tif'), overlay_Img); overlay_Img = plt.fredOverlayPoints(cfos_fn,
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());
cells = findCenterOfMaxima(data, dataMax); print cells.shape plt.plotOverlayPoints(data, cells, z = (10,16)) from ClearMap.ImageProcessing.CellSizeDetection import detectCellShape dataShape = detectCellShape(dataDoG, cells, threshold = 15); plt.plotOverlayLabel(dataDoG / dataDoG.max(), dataShape, z = (10,16)) # find intensities / cell sizes from ClearMap.ImageProcessing.CellSizeDetection import findCellSize, findCellIntensity #size of cells cellSizes = findCellSize(dataShape, maxLabel = cells.shape[0]); #intensity of cells cellIntensities = findCellIntensity(dataBGR, dataShape, maxLabel = cells.shape[0]); import matplotlib.pyplot as mpl mpl.figure() mpl.plot(cellSizes, cellIntensities, '.') mpl.xlabel('cell sizes [voxel]') mpl.ylabel('cell intensities [au]')