Skip to content

Background subtraction (optix.background)

Estimators

Background subtraction by grey-scale morphology.

Rolling ball (opening by a ball) and top-hat (opening by a flat disk or square), after Sternberg (1983). Building blocks (structuring elements, erosion, dilation, opening) are in optix.background.utils.

RollingBall

RollingBall(*, radius=None, output=None, shrink=None, smooth=None, faithful=True, dtype=None, device=None, layout=None)

Bases: _Background

Rolling-ball background subtraction (Sternberg 1983).

The background is the grey-scale opening of each image (or volume) by a ball: see rolling_ball for the method. There are no parameters to fit: fit only records the layout, and apply streams blocks with a halo of 2⌊ρ⌋ pixels (see rolling_ball_halo), so arrays larger than memory (zarr, memmap) give the same result as a whole-array computation. The spatial axes of the layout (2 or 3) set the dimension of the ball.

Note

Faithful mode is the paper's opening by a ball. shrink and smooth are extensions after ImageJ, off by default.

References

  1. Sternberg SR. Biomedical image processing. Computer 16(1):22–34, 1983. doi:10.1109/MC.1983.1654163
BibTeX
@article{sternberg1983biomedical,
  author  = {Sternberg, Stanley R.},
  title   = {Biomedical image processing},
  journal = {Computer},
  volume  = {16}, number = {1}, pages = {22--34},
  year    = {1983}, doi = {10.1109/MC.1983.1654163}
}

Configure the filter.

Parameters:

Name Type Description Default
radius float

Radius ρ > 0 of the ball, in pixels and intensity units (required; the paper leaves it to the user).

None
output (corrected, background)

What apply returns: data − background (default) or the background.

"corrected"
shrink int

Extension (not in the paper): shrink factor; see rolling_ball.

1
smooth bool

Extension (not in the paper): open the 3ⁿ mean of the data.

False
faithful bool

See Estimator.

True
dtype bool

See Estimator.

True
device bool

See Estimator.

True
layout bool

See Estimator.

True

TopHat

TopHat(*, radius=None, shape=None, output=None, faithful=True, dtype=None, device=None, layout=None)

Bases: _Background

Top-hat background subtraction with a flat structuring element.

The background is the grey-scale opening of each image (or volume) by a flat disk or square (Sternberg 1983): see top_hat. There are no parameters to fit; apply streams blocks with a halo of 2k pixels for a half-width k, which gives the whole-array result.

References

  1. Sternberg SR. Biomedical image processing. Computer 16(1):22–34, 1983. doi:10.1109/MC.1983.1654163
BibTeX
@article{sternberg1983biomedical,
  author  = {Sternberg, Stanley R.},
  title   = {Biomedical image processing},
  journal = {Computer},
  volume  = {16}, number = {1}, pages = {22--34},
  year    = {1983}, doi = {10.1109/MC.1983.1654163}
}

Configure the filter.

Parameters:

Name Type Description Default
radius int

Half-width k ≥ 0 of the element, in pixels (required).

None
shape (disk, square)

Flat element: ‖u‖ ≤ k or ‖u‖∞ ≤ k.

"disk"
output (corrected, background)

What apply returns: data − background (default) or the background.

"corrected"
faithful bool

See Estimator.

True
dtype bool

See Estimator.

True
device bool

See Estimator.

True
layout bool

See Estimator.

True

Transforms

Background subtraction by grey-scale morphology.

Rolling ball (opening by a ball) and top-hat (opening by a flat disk or square), after Sternberg (1983). Building blocks (structuring elements, erosion, dilation, opening) are in optix.background.utils.

rolling_ball

rolling_ball(data, radius, *, ndim=2, output='corrected', shrink=1, smooth=False)

Subtract the background estimated by a rolling ball.

Sternberg (1983) views a grey-scale image y as the umbra of its surface (the set of points under it) and estimates the background as the grey-scale opening of y by a ball of radius ρ:

B(u) = √(ρ² − ‖u‖²) − ρ,   ‖u‖ ≤ ρ          (the ball's umbra)
(y ⊖ B)(r) = min_u [y(r + u) − B(u)]         (erosion)
(z ⊕ B)(r) = max_u [z(r − u) + B(u)]         (dilation)
β = (y ⊖ B) ⊕ B,   result = y − β.

The opening β is the top surface swept by the ball rolled under the intensity landscape: the ball follows the slowly varying background but cannot enter peaks narrower than itself, which the subtraction keeps. Pixel and intensity units are the same (the "boxels" of the paper are cubes), so the radius must exceed the half-width of the largest feature to keep, and intensities may need rescaling.

The opening is exact (no decomposition or approximation of the ball). Offsets outside the image are left out of the minima and maxima (the paper does not specify the boundary), so the ball can overhang the edges. The opening never exceeds y, so the result is ≥ 0; the background is capped at data, which also removes the rounding of (y − B) + B.

Extensions

shrink and smooth are not from Sternberg (1983). They follow the practical options of ImageJ's background subtraction, written here from their published descriptions (they do not reproduce ImageJ's output bit for bit):

  • smooth=True opens the 3ⁿ mean of data (edges replicated) instead of data, which reduces the effect of noise on the background; the result is still data − background.
  • shrink=s takes the minimum of data over cells of sⁿ pixels, opens that coarse image with the ellipsoid of lateral semi-axis ρ/s and height ρ (the same ball, in coarse pixels), and interpolates the opening linearly from the cell centres back to the pixel grid. The cost drops by about s²ⁿ.

Both make the background an approximation of the opening; it is then capped at data.

Cost and halo

The cost is O(N·rⁿ) per pass for N pixels (one operation per offset of the ball). The result at a pixel depends on the data within 2⌊ρ⌋ pixels (erosion then dilation), the halo needed to process blocks independently; see RollingBall.

scikit-image

skimage.restoration.rolling_ball returns the erosion y ⊖ B (the height of the ball's apex), not the opening; the two differ by the final dilation.

References

  1. Sternberg SR. Biomedical image processing. Computer 16(1):22–34, 1983. doi:10.1109/MC.1983.1654163
BibTeX
@article{sternberg1983biomedical,
  author  = {Sternberg, Stanley R.},
  title   = {Biomedical image processing},
  journal = {Computer},
  volume  = {16}, number = {1}, pages = {22--34},
  year    = {1983}, doi = {10.1109/MC.1983.1654163}
}

Parameters:

Name Type Description Default
data Tensor

(..., *spatial); the last ndim dims are spatial (floating point; other dtypes are converted to the configured dtype).

required
radius float

Radius ρ > 0 of the ball, in pixels (and intensity units). The paper leaves it to the user: larger than the features to keep, smaller than the scale of the background variations.

required
ndim (2, 3, 1)

Number of spatial dims: 2 for images (a 3-D ball under a 2-D surface, as in the paper), 3 for volumes (a 4-D ball).

2
output (corrected, background)

Return y − β (default) or the background β.

"corrected"
shrink int

Extension: shrink factor s ≥ 1 (1 is the paper's opening).

1
smooth bool

Extension: open the 3ⁿ mean of the data.

False

Returns:

Type Description
Tensor

Of the shape of data.

top_hat

top_hat(data, radius, *, shape='disk', ndim=2, output='corrected')

White top-hat: subtract the opening by a flat structuring element.

With a flat element S (B = 0 on S), the erosion and dilation of Sternberg (1983) are a minimum and a maximum filter:

(y ⊖ S)(r) = min_{u ∈ S} y(r + u),   (z ⊕ S)(r) = max_{u ∈ S} z(r − u),
β = (y ⊖ S) ⊕ S,   result = y − β,

the top-hat of mathematical morphology. The paper defines the grey-scale opening for any element B and uses a ball for the background; a flat disk or square is the same equations with B = 0, not the paper's choice of element. It removes the background under features that S cannot fit into, whatever their height (the ball of rolling_ball also limits their slope). Offsets outside the image are left out of the minima and maxima. The square (cube) is computed as successive 1-D filters, the disk (ball) as 1-D filters along the last axis combined over the other axes; both are exact.

References

  1. Sternberg SR. Biomedical image processing. Computer 16(1):22–34, 1983. doi:10.1109/MC.1983.1654163
BibTeX
@article{sternberg1983biomedical,
  author  = {Sternberg, Stanley R.},
  title   = {Biomedical image processing},
  journal = {Computer},
  volume  = {16}, number = {1}, pages = {22--34},
  year    = {1983}, doi = {10.1109/MC.1983.1654163}
}

Parameters:

Name Type Description Default
data Tensor

(..., *spatial); the last ndim dims are spatial (floating point; other dtypes are converted to the configured dtype).

required
radius int

Half-width k ≥ 0 of the element, in pixels.

required
shape (disk, square)

"disk": ‖u‖ ≤ k (a ball in 3-D); "square": ‖u‖∞ ≤ k (side 2k + 1; a cube in 3-D).

"disk"
ndim (2, 3, 1)

Number of spatial dims.

2
output (corrected, background)

Return y − β (default) or the background β.

"corrected"

Returns:

Type Description
Tensor

Of the shape of data.

rolling_ball_halo

rolling_ball_halo(radius, *, shrink=1, smooth=False)

Halo, in pixels, that makes block-wise rolling ball exact.

2⌊ρ⌋ for the opening (erosion then dilation), plus 1 with smooth. With shrink=s, a multiple of s that covers the coarse opening, the shrinking cells and the interpolation: s·(2⌊ρ/s⌋ + 2 + [smooth]). Blocks must then start on multiples of s.

Parameters:

Name Type Description Default
radius float
required
shrink int
1
smooth bool
False

Returns:

Type Description
int

Utilities

No stability promise

optix.background.utils holds building blocks of the filters above; they may change without notice.

Building blocks of optix.background (no stability promise).

Structuring elements and grey-scale erosion, dilation and opening (Sternberg 1983).

ball

ball(radius, ndim=2, *, height=None, dtype=None, device=None)

Umbra of a ball (or ellipsoid), as a non-flat structuring element.

B(u) = h · √(1 − ‖u‖²/ρ²) − h   for ‖u‖ ≤ ρ,   −∞ elsewhere,

on the integer offsets u ∈ ℤⁿ, with ρ the radius and h the height (h = ρ for a ball: B(u) = √(ρ² − ‖u‖²) − ρ). The heights are shifted so that B(0) = 0; an opening does not depend on this shift.

Parameters:

Name Type Description Default
radius float

Radius ρ > 0 along the spatial axes, in pixels.

required
ndim int

Number of spatial dims n.

2
height float

Semi-axis h along the intensity axis (default: radius, a ball).

None
dtype optional

Of the result (default dtype: float64).

None
device optional

Of the result (default dtype: float64).

None

Returns:

Type Description
Tensor

(2k+1,)*ndim with k = ⌊r⌋.

footprint

footprint(radius, ndim=2, shape='disk', *, dtype=None, device=None)

Flat structuring element: 0 on its support, −∞ elsewhere.

Parameters:

Name Type Description Default
radius int

Half-width k ≥ 0, in pixels.

required
ndim int

Number of spatial dims n.

2
shape (disk, square)

"disk": the offsets with ‖u‖ ≤ k (a ball in 3-D); "square": the offsets with ‖u‖∞ ≤ k (a cube in 3-D).

"disk"
dtype optional

Of the result (default dtype: float64).

None
device optional

Of the result (default dtype: float64).

None

Returns:

Type Description
Tensor

(2k+1,)*ndim.

grey_erosion

grey_erosion(data, structure)

Grey-scale erosion, Sternberg (1983):

(y ⊖ B)(r) = min_u [y(r + u) − B(u)],

over the offsets u of the support of B, on the trailing structure.ndim dims of data. Offsets that fall outside the image are left out of the minimum (the image is +∞ outside its domain).

Parameters:

Name Type Description Default
data Tensor

(..., *spatial), floating point.

required
structure Tensor

Heights B, odd shape, origin at the centre, −∞ outside the support (see ball and footprint).

required

Returns:

Type Description
Tensor

Of the shape of data.

grey_dilation

grey_dilation(data, structure)

Grey-scale dilation, Sternberg (1983):

(z ⊕ B)(r) = max_u [z(r − u) + B(u)],

over the offsets u of the support of B, on the trailing structure.ndim dims of data. Offsets that fall outside the image are left out of the maximum (the image is −∞ outside its domain).

Parameters:

Name Type Description Default
data Tensor

(..., *spatial), floating point.

required
structure Tensor

Heights B, as in grey_erosion.

required

Returns:

Type Description
Tensor

Of the shape of data.

grey_opening

grey_opening(data, structure)

Grey-scale opening, β = (y ⊖ B) ⊕ B (Sternberg 1983).

The opening is the upper surface of the union of all translates of the umbra of B that fit under the umbra of y: it is the surface swept by B "rolled" under the image. Its influence radius is twice the half-size of B.

Parameters:

Name Type Description Default
data Tensor

(..., *spatial), floating point.

required
structure Tensor

Heights B, as in grey_erosion.

required

Returns:

Type Description
Tensor

Of the shape of data.