lucem_hist::stains
Brightfield stains: optical density (Beer–Lambert), stain vectors and colour deconvolution.
A pixel’s optical density per channel is -log10(I / I₀) with I₀ the background (white)
value. Each stain has a unit OD vector (how much it absorbs in red, green and blue); three
vectors form a matrix whose inverse separates a pixel’s OD into the amount of each stain.
Presets are Ruifrok & Johnston’s (2001); estimate_macenko fits two stains to a region of
the slide itself (Macenko et al., 2009), which follows the actual staining better.
StainVector
Section titled “StainVector”pub struct StainVectorFields
name: Stringod: [f64; 3]: Unit optical density in red, green, blue.
Methods
pub fn new(name: &str, od: [f64; 3]) -> SelfImplements Clone, Debug, Deserialize, PartialEq, Serialize.
StainVectors
Section titled “StainVectors”pub struct StainVectorsThree stains (the third may be the residual: what the first two do not explain) and the background RGB.
Fields
name: Stringstains: [StainVector; 3]background: [f64; 3]
Methods
pub fn two(name: &str, a: StainVector, b: StainVector, background: [f64; 3]) -> SelfTwo stains and the residual (their cross product).
pub fn h_dab() -> SelfHematoxylin and DAB (immunohistochemistry).
pub fn h_e() -> SelfHematoxylin and eosin.
pub fn h_e_dab() -> SelfHematoxylin, eosin and DAB.
Implements Clone, Debug, Deserialize, PartialEq, Serialize.
Functions
Section titled “Functions”pub fn od(value: f64, background: f64) -> f64The optical density of a value against its background (0 at the background, larger darker).
deconvolve
Section titled “deconvolve”pub fn deconvolve(rgb: &[u8], stains: &StainVectors) -> HistResult<[Vec<f32>; 3]>Separates interleaved RGB pixels (0–255) into the amount of each stain (optical density
units): three planes, in the order of stains.stains.
od_sum
Section titled “od_sum”pub fn od_sum(rgb: &[u8], background: [f64; 3]) -> Vec<f32>The summed optical density of each pixel (how much any stain is there: tissue detection, “OD sum” measurements).
estimate_background
Section titled “estimate_background”pub fn estimate_background(rgb: &[u8]) -> [f64; 3]The background (white) of a brightfield region: the mean of its brightest 1 % of pixels.
estimate_macenko
Section titled “estimate_macenko”pub fn estimate_macenko( rgb: &[u8], background: [f64; 3], names: (&str, &str), min_od: f64, alpha: f64,) -> HistResult<StainVectors>Two stains fitted to a region (Macenko): the optical densities of its stained pixels (OD
above min_od in every channel) span a plane; the stains are its extreme directions (the
alpha and 100 − alpha percentiles of their angles). The first stain is the one that
absorbs more red (hematoxylin before eosin or DAB). Fewer than 100 stained pixels: an error.