Skip to content

grayscale_intensity

Computes the mean, std, & quantiles of grayscale intensity of objects in img.

Parameters:

Name Type Description Default
img ndarray

Image to compute properties from. Shape (H, W).

required
label ndarray

Label image. Shape (H, W).

None
quantiles Tuple[float, ...]

Quantiles to compute for each object.

(0.25, 0.5, 0.75)

Returns:

Name Type Description
means ndarray

Mean intensity of each object. Shape (N,).

std ndarray

Standard deviation of each object. Shape (N,).

quantile_vals ndarray

Quantile values for each object. Shape (N, len(quantiles)).

Examples:

>>> from histolytics.data import hgsc_cancer_he, hgsc_cancer_nuclei
>>> from histolytics.utils.raster import gdf2inst
>>> from histolytics.nuc_feats.intensity import grayscale_intensity
>>>
>>> he_image = hgsc_cancer_he()
>>> nuclei = hgsc_cancer_nuclei()
>>> neoplastic_nuclei = nuclei[nuclei["class_name"] == "neoplastic"]
>>> inst_mask = gdf2inst(
...     neoplastic_nuclei, width=he_image.shape[1], height=he_image.shape[0]
... )
>>> # Extract grayscale intensity features from the neoplastic nuclei
>>> means, stds, quantiles = grayscale_intensity(he_image, inst_mask)
>>> print(means.mean())
    0.21791865214466258
Source code in src/histolytics/nuc_feats/intensity.py
def grayscale_intensity(
    img: np.ndarray,
    label: np.ndarray = None,
    quantiles: Tuple[float, ...] = (0.25, 0.5, 0.75),
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
    """Computes the mean, std, & quantiles of grayscale intensity of objects in `img`.

    Parameters:
        img (np.ndarray):
            Image to compute properties from. Shape (H, W).
        label (np.ndarray):
            Label image. Shape (H, W).
        quantiles (Tuple[float, ...]):
            Quantiles to compute for each object.

    Returns:
        means (np.ndarray):
            Mean intensity of each object. Shape (N,).
        std (np.ndarray):
            Standard deviation of each object. Shape (N,).
        quantile_vals (np.ndarray):
            Quantile values for each object. Shape (N, len(quantiles)).

    Examples:
        >>> from histolytics.data import hgsc_cancer_he, hgsc_cancer_nuclei
        >>> from histolytics.utils.raster import gdf2inst
        >>> from histolytics.nuc_feats.intensity import grayscale_intensity
        >>>
        >>> he_image = hgsc_cancer_he()
        >>> nuclei = hgsc_cancer_nuclei()
        >>> neoplastic_nuclei = nuclei[nuclei["class_name"] == "neoplastic"]
        >>> inst_mask = gdf2inst(
        ...     neoplastic_nuclei, width=he_image.shape[1], height=he_image.shape[0]
        ... )
        >>> # Extract grayscale intensity features from the neoplastic nuclei
        >>> means, stds, quantiles = grayscale_intensity(he_image, inst_mask)
        >>> print(means.mean())
            0.21791865214466258
    """
    if label is not None and img.shape[:2] != label.shape:
        raise ValueError(
            f"Shape mismatch: img.shape[:2]={img.shape[:2]}, label.shape={label.shape}"
        )

    p2, p98 = np.percentile(img, (2, 98))
    img = rescale_intensity(img, in_range=(p2, p98))
    img = rgb2gray(img) * (label > 0)

    means = []
    std = []
    quantile_vals = []
    for i in np.unique(label)[1:]:
        inst = label == i
        _intensity = img * inst
        non_zero = _intensity.ravel()
        non_zero = non_zero[non_zero > 0]
        means.append(np.mean(non_zero))
        std.append(np.std(non_zero))
        quantile_vals.append(np.quantile(non_zero, quantiles))

    return np.array(means), np.array(std), np.array(quantile_vals)