Rolling ball
At a glance
- Estimates: the background of an image or volume, and the image minus it
- Noise model: none
- Streaming: halo
- Needs many frames: no
- Reference: Sternberg, Computer, 1983, doi:10.1109/MC.1983.1654163
Overview
RollingBall estimates a slowly varying background and subtracts it, with the
background normalisation of
Sternberg (1983): the image is read
as a landscape, and the background is the surface traced by the top of a ball
rolled underneath it. The ball follows the smooth background but cannot enter
peaks narrower than itself, so the peaks are kept in the difference. TopHat
does the same with a flat disk or square instead of a ball.
Use it when the specimen is made of features smaller than the scale of the background, such as spots on an uneven or drifting background, and no stack of frames is available to fit a shading model. The two sizes are the only parameters: the radius must exceed the half-width of the largest feature to keep, and stay below the scale of the background variations. When many frames share one illumination pattern, BaSiC or CIDRE estimate it from the data instead.
There is nothing to fit: fit only records the layout, and apply returns the
corrected frames, or the background with output="background". The result is
an image of the shape of the input, not a result type of
optix.estimates. See
optix.background for the API.
Image formation (optics)
The model is the additive background of Image formation and noise, with no assumption on the camera or the illumination beyond their smoothness:
The image \(\mathbf{y} \in \mathbb{R}^{\lvert\Omega\rvert}\), one frame of \(\lvert\Omega\rvert\) pixels (each frame of a stack is treated alone), is the sum of the features \(\mathbf{x} \ge 0\) and a background \(\boldsymbol{\beta}\) that is smooth with respect to the ball while the features are not (Sternberg 1983, p. 29). The paper does not write this sum as an equation; it describes the gel image as spots on a background that "is not uniform over a gel and varies between gels". The ball is built in units where one pixel and one grey level have the same length, so the radius below is both a distance in the image and a height in intensity.
Probabilistic model
The method is a morphological operator, not a statistical model. Its objective is the opening of the image by a structuring element, which Sternberg describes through the umbra of the image: the solid under the surface \((\mathbf{r}, \mathbf{y}(\mathbf{r}))\), a pile of cubic "boxels" at every pixel. The opening is the union of all translations of the element that fit entirely inside the umbra, and its upper surface is the background.
with \(C = A \ominus B_k\) and \(D = (A \ominus B) \oplus B = A_B\) (p. 30); the background image is \(D\) and the result is \(A - D\) (Fig. 9).
| optix | Sternberg 1983 | Meaning | Shape |
|---|---|---|---|
| \(\mathbf{y}\), \(y(\mathbf{r})\) | \(A\), \(A(x, y)\) | recorded image; its value at position \(\mathbf{r}\) | \(\lvert\Omega\rvert\), one per frame |
| \(\mathbf{r}\) | \((x, y)\) | pixel position | \(p\) integers (one per spatial axis) |
| \(\mathbf{u}\) | \((-i, -j)\) in the erosion, \((i, j)\) in the dilation | offset within the element | \(p\) integers |
| \(B(\mathbf{u})\) | \(B_k(i, j)\) | height (grey level) of the element at offset \(\mathbf{u}\) | one per offset, one element for the whole stack |
| \(\mathbf{y} \ominus B\) | \(C = A \ominus B_k\) | eroded image: height of the apex of the ball | \(\lvert\Omega\rvert\), one per frame |
| \(\boldsymbol{\beta}\) | \(D = A_B\), the "background image" | background, the opening of the image | \(\lvert\Omega\rvert\), one per frame |
| \(\hat{\mathbf{x}}\) | \(A - D\) | background-normalised image | \(\lvert\Omega\rvert\), one per frame |
| \(\rho\) | half the "sphere diameter" (p. 29) | radius of the ball, in pixels and grey levels | scalar, one for the whole stack |
| \(B\) | \(B_1 \oplus \cdots \oplus B_K\) | structuring element, composite of the sequence \(B_1, \ldots, B_K\) | offsets within radius \(\rho\), one for the whole stack |
The paper builds its element as a sequence of small elements \(B_1, \ldots, B_K\), applied one after the other, and relies on the identities
so that the sequence acts as one erosion, then one dilation, by the composite element \(B\) (p. 27 and 30). Here \(B\) is given directly, and the sequence is not used. The substitution \(\mathbf{u} \to -\mathbf{u}\) turns the paper's erosion into the form above, which adds the offset of the element to the image. Both are a minimum (or maximum) over the same set of offsets only because the support of the ball is symmetric (\(\mathbf{u}\) and \(-\mathbf{u}\) are both in it, with the same height), so that reflecting the window changes nothing.
The ball. With radius \(\rho\), the umbra of the ball is the element
on the integer offsets \(\mathbf{u}\), and \(-\infty\) elsewhere (those offsets are left out). The shift by \(-\rho\) makes \(B(\mathbf{0}) = 0\); an opening does not depend on it. For an image (\(p = 2\) spatial axes) the ball has three dimensions, the two of the image plane and the grey level; for a volume (\(p = 3\)) it has four.
The flat element. For a flat element \(S\) (\(B = 0\) on \(S\)), the same formulas reduce to a minimum then a maximum filter over \(S\), which is the top-hat of mathematical morphology:
What the paper says about flat elements
The paper illustrates opening with flat elements on binary images (Figs. 6 and 7) and defines the grey-scale version with any element \(B_k\), so the flat case is the same equations with \(B = 0\). It does not name the top-hat, and it uses a ball for the background.
Inference / derivation
There is no estimation: the background follows from the image by the two filters above. The method has no statistical counterpart (no likelihood or prior is optimised); the opening is the largest function below \(\mathbf{y}\) that is a union of translates of \(B\), a deterministic envelope rather than an estimator. The reasoning of the paper (Fig. 10) is geometric:
- The erosion \(\mathbf{y} \ominus B\) is the height of the apex of the ball (the top of its umbra, where \(B = 0\)) as it is pushed up against the underside of the surface at every position.
- The dilation by the same element sweeps the ball over those apex positions and takes the top of the ball. The result is the surface touched by the ball's top while it rolls under the image.
- The opening is anti-extensive: \(\boldsymbol{\beta} \le \mathbf{y}\), so \(\hat{\mathbf{x}} \ge 0\). Peaks narrower than the ball are cut off by the opening, and survive in \(\hat{\mathbf{x}}\); slow variations are followed by the ball and removed.
The radius is a trade-off that the paper leaves to the user: the ball must be "considerably wider than any of the peaks", and "small enough to follow the smooth contours of the changing background".
The result at a pixel depends only on the image within \(2\lfloor\rho\rfloor\)
pixels, since the erosion and the dilation each reach \(\lfloor\rho\rfloor\) (one more
with smooth=True). This
is the halo that makes block-wise processing exact (see
Memory and streaming).
Algorithm
Input: an image or volume \(\mathbf{y} \in \mathbb{R}^{\lvert\Omega\rvert}\)
with \(p \le 3\) spatial axes; the radius \(\rho > 0\) of the ball, or the
half-width of a flat disk or square for TopHat; output="corrected". \
Output: \(\hat{\mathbf{x}} = \mathbf{y} - \boldsymbol{\beta}\), or
\(\boldsymbol{\beta}\) with output="background".
- Build the element \(B(\mathbf{u})\) on the integer offsets \(\lVert \mathbf{u} \rVert \le \rho\) (or \(B = 0\) on the disk or square).
- Erode: \(c(\mathbf{r}) = \min_{\mathbf{u}} \bigl[\, y(\mathbf{r} + \mathbf{u}) - B(\mathbf{u}) \,\bigr]\).
- Dilate: \(\beta(\mathbf{r}) = \max_{\mathbf{u}} \bigl[\, c(\mathbf{r} - \mathbf{u}) + B(\mathbf{u}) \,\bigr]\).
- Cap: \(\boldsymbol{\beta} \leftarrow \min(\boldsymbol{\beta}, \mathbf{y})\), pixel-wise.
- Return \(\mathbf{y} - \boldsymbol{\beta}\) (or \(\boldsymbol{\beta}\)).
Offsets that fall outside the image are left out of the minimum and the maximum. Step 4 changes nothing in exact arithmetic, as the opening never exceeds \(\mathbf{y}\); it removes the rounding error of \((\mathbf{y} - B) + B\) in floating point, so that the result is never negative.
Faithful mode and extensions
faithful=True (the default) is the opening of the paper. The radius and the
output are free parameters of the method, so any value of them is faithful.
TopHat has no extension: it is always faithful.
Choices that the code makes where the paper is silent:
- Edges. The paper does not say what happens at the border. Out-of-image offsets are ignored (equivalent to \(+\infty\) for the erosion and \(-\infty\) for the dilation), so the ball can overhang the edge.
- Exact ball. The paper builds its ball as the dilation of one point by 26 small elements of a \(3 \times 3 \times 3\) window (Fig. 11), a hardware approximation. Here \(B\) is sampled from the analytic formula above and the opening is exact for it; the 26-element construction is not reproduced.
- Units. A pixel and a grey level are the same length (the boxels are cubes), so \(\rho\) is in pixels and in intensity units. Rescale the intensities if the ball should be flatter or taller.
- Dimension. The number of spatial axes of the layout (up to three) sets the dimension of the ball; the paper treats only images.
Extensions, off by default, make the estimate faster or less sensitive to noise and are not in the paper. They follow the public descriptions of the background subtraction of ImageJ and do not reproduce its output bit for bit:
| Keyword | Changes | Faithful value | Recommended value |
|---|---|---|---|
shrink |
\(s \ge 1\): take the minimum over cells of \(s^p\) pixels, open that coarse image with the ellipsoid of lateral radius \(\rho / s\) and height \(\rho\), then interpolate the background linearly from the cell centres back to the pixel grid. The cost drops by about \(s^{2p}\) | 1 | 1 |
smooth |
open the \(3^p\) mean of the image (edges replicated) instead of the image; the result is still the image minus the background | False |
False |
With either, the background approximates the opening and is capped at the image. Sternberg's streak removal, by openings with a horizontal and a vertical bar, is not implemented.
scikit-image
skimage.restoration.rolling_ball returns the erosion \(\mathbf{y} \ominus B\),
the height of the apex of the ball, not the opening. It matches
optix.background.utils.grey_erosion to \(10^{-12}\), and the background here is
the dilation of its output.
Memory and streaming
RollingBall and TopHat are tagged halo. apply reads blocks of the spatial
axes with an overlap of \(2\lfloor\rho\rfloor\) pixels, filters each block and
stores its core, so zarr and memmap inputs that exceed memory give the same
result as a whole-array computation, and several frames or channels are
independent. The halo is available as
rolling_ball_halo. In-place output is
refused, since a block reads pixels that other blocks have written.
With shrink=s, blocks start on multiples of \(s\) so that the cells lie on the
global grid, and the halo grows to \(s\,(2\lfloor\rho/s\rfloor + 2 + [\text{smooth}])\), a
multiple of \(s\) that covers the coarse opening, the cells and the interpolation
(\([\text{smooth}]\) is 1 with smooth=True and 0 otherwise).
The cost of the exact ball is \(O(\lvert\Omega\rvert\, \rho^p)\) for \(\lvert\Omega\rvert\) pixels, one operation per
offset of the ball. On one CPU thread, a \(1024^2\) image takes about 0.4 s with
\(\rho = 10\) and 11 s with \(\rho = 50\); shrink=4 brings the latter to 0.03 s. A
\(128^3\) volume takes about 12 s with \(\rho = 10\). The flat disk and square are
computed with running minima and maxima over one axis at a time, and are much
cheaper.
Usage
The example puts the beads of a phantom on a smooth background.
>>> import torch
>>> from optix.background import RollingBall, rolling_ball
>>> from optix.models import phantoms
>>> # 1. A smooth background (the truth) under ten blurred beads.
>>> background = phantoms.background((128, 128), level=20.0, seed=0)
>>> spots, _ = phantoms.beads((128, 128), count=10, sigma=2.0, seed=1)
>>> data = background + spots
>>> # 2. Subtract the background, or 3. return it.
>>> corrected = rolling_ball(data, radius=15)
>>> estimate = rolling_ball(data, radius=15, output="background")
>>> bool((corrected >= 0).all())
True
>>> # 4. Compare to the truth: mean error about 0.07 (level 20).
>>> round(float((estimate - background).abs().mean()), 2)
0.07
For a stack, the estimator filters every frame and returns the background with
output="background":
>>> stack = data[None] + torch.zeros(4, 1, 1) # (frames, H, W)
>>> ball = RollingBall(radius=15, output="background").fit(stack)
>>> ball.is_faithful
True
>>> bool(torch.equal(ball.apply(stack)[0], estimate))
True
Common variations:
from optix.background import TopHat
# A flat square element, and the fast approximation of the ball.
corrected = TopHat(radius=15, shape="square").fit(stack).apply(stack)
fast = RollingBall(radius=15, shrink=4).fit(stack).apply(stack) # not faithful
# A volume: a 4-D ball over the last three axes.
volume = rolling_ball(torch.rand(32, 64, 64), radius=5, ndim=3)
For a large zarr array, pass it to apply with out= set to a zarr array; the
filter reads and writes in blocks. See the
API reference for every option.
Validation: the morphology is bit-identical to a brute-force transcription of the
formulas above in 2-D and 3-D, in float32 and float64; spots on a smooth
background in synthetic scenes are recovered; block-wise and whole-image results
are identical (to \(10^{-10}\) with shrink), and zarr input gives the same
result as in-memory input.
References
References
- 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},
}