LocalStatistics#

Module provides functions to calculate local data and statistics of an image and apply a function to those. It is useful for local and adaptive image processing of large 3d images.

Note

The module provides ways to speed up the local statistics by only sampling on a sub-grid of the image and resample the result to the full image shape.

apply_local_function(source, function, selem=(50, 50), spacing=None, step=None, interpolate=2, mask=None, fshape=None, dtype=None, return_centers=False)[source]#

Calculate local histograms on a sub-grid, apply a scalar valued function and resample to original image shape.

Arguments

sourcearray

The source to process.

functionfunction

Function to apply to the linear array of the local source data. If the function does not return a scalar, fshape has to be given.

selemtuple or array or None

The structural element to use to extract the local image data. If tuple, use a rectangular region of this shape. If array, the array is assumed to be bool and acts as a local mask around the center point.

spacingtuple or None

The spacing between sample points. If None, use shape of structural_element.

steptuple of int or None

If tuple, subsample the local region by these step. Note that the structural_element is applied after this subsampling.

interpolateint or None

If int, resample the result back to the original source shape using this order of interpolation. If None, return the results on the sub-grid.

maskarray or None

Optional mask to use.

fshapetuple or None

If tuple, this is the shape of the function output. If None assumed to be (1,).

dtypedtype or None

Optional data type for the result.

return_centersbool

If True, additionaly return the centers of the sampling.

Returns

localarray

The reuslt of applying the function to the local samples.

cetnersarray

Optional cttners of the sampling.

local_histogram(source, max_bin=4096, selem=(50, 50), spacing=None, step=None, interpolate=None, mask=None, dtype=None, return_centers=False)[source]#

Calculate local histograms on a sub-grid.

Arguments

sourcearray

The source to process.

selemtuple or array or None

The structural element to use to extract the local image data. If tuple, use a rectangular region of this shape. If array, the array is assumed to be bool and acts as a local mask around the center point.

spacingtuple or None

The spacing between sample points. If None, use shape of structural_element.

steptuple of int or None

If tuple, subsample the local region by these step. Note that the structural_element is applied after this subsampling.

interpolateint or None

If int, resample the result back to the original source shape using this order of interpolation. If None, return the results on the sub-grid.

maskarray or None

Optional mask to use.

max_binint

Maximal bin value to account for.

return_centersbool

If True, additionaly return the centers of the sampling.

Returns

histogramsarray

The local histograms.

cetnersarray

Optional centers of the sampling.

Note

For speed, this function works only for uint sources as the histogram is calculated directly via the source values. The source values should be smaller than max_bin.

local_percentile(source, percentile, selem=(50, 50), spacing=None, step=None, interpolate=1, mask=None, dtype=None, return_centers=False)[source]#

Calculate local percentile.

Arguments

sourcearray

The source to process.

percentilefloat or array

The percentile(s) to estimate locally.

selemtuple or array or None

The structural element to use to extract the local image data. If tuple, use a rectangular region of this shape. If array, the array is assumed to be bool and acts as a local mask around the center point.

spacingtuple or None

The spacing between sample points. If None, use shape of structural_element.

steptuple or int or None

If tuple, subsample the local region by these steps. Note that the structural_element is applied after this subsampling.

interpolateint or None

If int, resample the result back to the original source shape using this order of interpolation. If None, return the results on the sub-grid.

maskarray or None

Optional mask to use.

return_centersbool

If True, additionally return the centers of the sampling.

Returns

percentilesarray

The local percentiles.

centersarray

Optional centers of the sampling.