R/background.R
model_background.RdEstimates the colour of the agar, removes the spatial illumination gradient (vignetting, oblique light) by flat-field correction, and flags every dish pixel that differs significantly from agar.
model_background(
plate,
flat_field = TRUE,
degree = 2L,
min_delta_e = 3,
texture_z = 4,
shadow_texture_z = 2.5,
smooth_mm = 0.08,
feature_smooth_mm = 0.25,
reference = NULL,
reference_lab = c(50, 0, 0)
)A mycohalo_plate with a detected dish.
Logical; apply the illumination correction.
Polynomial degree of the illumination surface (1 or 2).
Minimum CIE76 colour difference from agar for a pixel to be considered object.
Robust z-score of log local texture (relative to agar) above which a pixel is an object even if its colour matches the agar.
Pixels darker than agar, with agar-like
chromaticity and texture z below this value are cast shadows of raised
colonies and are excluded (set -Inf to disable).
Gaussian smoothing (mm) of colour used for object detection (measurements always use unsmoothed pixels).
Gaussian smoothing (mm) of colour used as classification feature; averages over hyphal ridges and valleys so a wrinkled colony is described by its mean colour.
Optional exposure / white-balance reference applied
after flat-field correction: a grey-card region c(x, y, r) in relative
image coordinates (see calibrate_color()), or "agar" to scale the
image so that the agar takes the colour reference_lab (only valid when
all plates use the same medium batch and the agar is not tinted).
Known CIELAB of the reference.
The plate with corrected rgb/lab and an element bg.
1. Agar colour by mode seeking. The agar is the most frequent colour inside the dish even on crowded confrontation plates, because agar pixels are concentrated in colour space while colonies, bacteria and halos are spread along gradients. The mode of the (smoothed) \(L^*a^*b^*\) distribution of low-texture pixels is found with a coarse 3-D histogram followed by mean-shift refinement (Comaniciu & Meer 2002). It works on dark and on light agar.
2. Flat-field correction. Illumination acts multiplicatively on linear light. For each linear RGB channel c a quadratic surface \(\log I_c(x, y) = \beta_0 + \beta_1 x + \beta_2 y + \beta_3 x^2 + \beta_4 xy + \beta_5 y^2\) is fitted to agar pixels with iterative 3-MAD trimming, and every pixel is multiplied by \(\exp(\hat\ell_c(x_0, y_0) - \hat\ell_c(x, y))\), i.e. normalised to the illumination at the dish centre. Because the correction is estimated from agar around the colonies and applied to the colonies, colony lightness becomes independent of where on the dish the colony sits.
3. Object detection. After correction the agar has a spatially
constant colour \(\mu\) with per-channel robust SD \(\sigma\) (MAD).
A pixel is object (non-agar) if its standardised distance
\(D^2 = \sum_c ((x_c - \mu_c)/\sigma_c)^2\) exceeds the 0.999 quantile
of \(\chi^2_3\) and its colour difference exceeds min_delta_e
(default 3, just above the ~2.3 just-noticeable difference), so that
neither sensor noise nor imperceptible tints are called objects.
In addition, a pixel is object when its local texture (SD of \(L^*\)
in a ~0.2 mm window, log scale) exceeds the agar texture by more than
texture_z robust standard deviations: strongly melanized, wrinkled
colonies can have almost the mean colour of a dark agar but are far
rougher. Cast shadows of raised colonies (darker than agar, agar-like
chromaticity, smooth) are removed from the objects: under directional
light they would otherwise be counted as melanized tissue on one side of
every colony.
sim <- simulate_plate(width = 300, height = 400, seed = 3, vignetting = 0.3)
p <- detect_plate(read_plate(sim$image))
p <- model_background(p)
p$bg$agar_lab
#> L a b
#> 22.2716888 -0.9644315 -3.8346218
p$bg$illumination_range_pct
#> [1] 28.25772