Example #1
0
def _test():
    """Tests"""
    import ClearMap.Tests.Files as tsf
    import ClearMap.Visualization.Plot3d as p3d

    import ClearMap.ImageProcessing.LightsheetCorrection as ls
    from importlib import reload
    reload(ls)

    s = tsf.source('vasculature_lightsheet_raw')
    #p3d.plot(s)

    import ClearMap.ImageProcessing.Experts.Vasculature as vasc
    clipped, mask, high, low = vasc.clip(s[:, :, 80:120],
                                         clip_range=(400, 60000))

    corrected = ls.correct_lightsheet(clipped,
                                      mask=mask,
                                      percentile=0.25,
                                      lightsheet=dict(selem=(150, 1, 1)),
                                      background=dict(selem=(200, 200, 1),
                                                      spacing=(25, 25, 1),
                                                      step=(2, 2, 1),
                                                      interpolate=1),
                                      lightsheet_vs_background=2)

    p3d.plot([clipped, corrected])
Example #2
0
def equalize(source, percentile = (0.5, 0.95), max_value = 1.5, selem = (200,200,5), spacing = (50,50,5), interpolate = 1, mask = None):
  equalized = ls.local_percentile(source, percentile=percentile, mask=mask, dtype=float, selem=selem, spacing=spacing, interpolate=interpolate);
  normalize = 1/np.maximum(equalized[...,0], 1);
  maxima = equalized[...,1];
  ids = maxima * normalize > max_value;
  normalize[ids] = max_value / maxima[ids];
  equalized = np.array(source, dtype = float) * normalize;                          
  return equalized;
Example #3
0
def _test():
    """Tests."""
    import numpy as np
    import ClearMap.Visualization.Plot3d as p3d

    import ClearMap.ImageProcessing.LocalStatistics as ls
    from importlib import reload
    reload(ls)

    source = np.random.rand(100, 200, 150) + np.arange(100)[:, None, None]
    p = ls.local_percentile(source,
                            percentile=0.5,
                            selem=(30, 30, 30),
                            interpolate=1)
    p3d.plot([source, p])
Example #4
0
def threshold_adaptive(source, function = threshold_isodata, selem = (100,100,3), spacing = (25,25,3), interpolate = 1, mask = None, step = None):
  source = io.as_source(source)[:];
  threshold = ls.apply_local_function(source, function=function, mask=mask, dtype=float, selem=selem, spacing=spacing, interpolate=interpolate, step = step);
  return threshold
Example #5
0
def correct_lightsheet(source,
                       percentile=0.25,
                       max_bin=2**12,
                       mask=None,
                       lightsheet=dict(selem=(150, 1, 1)),
                       background=dict(selem=(200, 200, 1),
                                       spacing=(25, 25, 1),
                                       interpolate=1,
                                       dtype=float,
                                       step=(2, 2, 1)),
                       lightsheet_vs_background=2,
                       return_lightsheet=False,
                       return_background=False,
                       verbose=True):
    """Removes lightsheet artifacts.
  
  Arguments
  ---------
  source : array
    The source to correct.
  percentile : float in [0,1]
    Ther percentile to base the lightsheet correction on.
  max_bin : int 
    The maximal bin to use. Max_bin needs to be >= the maximal value in the 
    source.
  mask : array or None
    Optional mask.
  lightsheet : dict
    Parameter to pass to the percentile routine for the lightsheet artifact
    estimate. See :func:`ImageProcessing.Filter.Rank.percentile`.
  background : dict
    Parameter to pass to the percentile rouitne for the background estimation.
  lightsheet_vs_background : float
    The background is multiplied by this weight before comparing to the
    lightsheet artifact estimate.
  return_lightsheet : bool
    If True, return the lightsheeet artifact estimate.
  return_background : bool
    If True, return the background estimate.
  verbose : bool
    If True, print progress information.
  
  Returns
  -------
  corrected : array
    Lightsheet artifact corrected image.
  lightsheet : array
    The lightsheet artifact estimate.
  background : array
    The background estimate.
  
  Note
  ----
  The routine implements a fast but efftice way to remove lightsheet artifacts.
  Effectively the percentile in an eleoganted structural element along the 
  lightsheet direction centered around each pixel is calculated and then
  compared to the percentile in a symmetrical box like structural element 
  at the same pixel. The former is an estimate of the lightsheet artifact 
  the latter of the backgrond. The background is multiplied by the factor 
  lightsheet_vs_background and then the minimum of both results is subtracted
  from the source.
  Adding an overall background estimate helps to not accidentally remove
  vessesl like structures along the light-sheet direction.
  """
    if verbose:
        timer = tmr.Timer()

    #lightsheet artifact estimate
    l = rnk.per.percentile(source,
                           percentile=percentile,
                           max_bin=max_bin,
                           mask=mask,
                           **lightsheet)
    if verbose:
        timer.print_elapsed_time(
            'LightsheetCorrection: lightsheet artifact done')

    #background estimate
    b = ls.local_percentile(source,
                            percentile=percentile,
                            mask=mask,
                            **background)
    if verbose:
        timer.print_elapsed_time('LightsheetCorrection: background done')

    #combined estimate
    lb = np.minimum(l, lightsheet_vs_background * b)

    #corrected image
    c = source - np.minimum(source, lb)

    if verbose:
        timer.print_elapsed_time('LightsheetCorrection: done')

    result = (c, )
    if return_lightsheet:
        result += (l, )
    if return_background:
        result += (b, )
    if len(result) == 1:
        result = result[0]
    return result