histo_kit.tissue_seg.postprocessing

Functions

cluster_regions(data[, max_iters, tol])

Cluster regions areas using a single-threaded KMeans algorithm with deterministic initialization.

remove_black_pen(img, thr_low, thr_back, radius)

Generate a binary mask to remove black pen markings from an RGB image.

remove_gray_stains(img[, mask])

Remove gray stains from an RGB image based on low chroma component.

remove_small_objects(mask[, mode])

Remove small objects from a binary mask based on area.

histo_kit.tissue_seg.postprocessing.cluster_regions(data, max_iters=100, tol=0.0001)[source]

Cluster regions areas using a single-threaded KMeans algorithm with deterministic initialization.

Parameters:
  • data (array-like, shape (n_samples,)) – 1D data vector to be clustered.

  • max_iters (int, optional) – Maximum number of iterations (default is 100).

  • tol (float, optional) – Tolerance for convergence (default is 1e-4).

Returns:

  • labels (ndarray, shape (n_samples,)) – Cluster labels assigned to each sample.

  • centers (ndarray, shape (n_clusters,)) – Coordinates of cluster centers.

histo_kit.tissue_seg.postprocessing.remove_black_pen(img, thr_low, thr_back, radius)[source]

Generate a binary mask to remove black pen markings from an RGB image.

The function identifies dark pen marks on tissue region by thresholding the image in the LAB color space and refining the result using morphological operations. The output is a boolean mask that can be used to remove or inpaint pen marks.

Parameters:
  • img (ndarray) – Input RGB image as a NumPy array of shape (M, N, 3).

  • thr_low (float) – Lower threshold applied to the Value (V) channel in LABspace to detect dark regions corresponding to pen marks.

  • thr_back (dict) – Dictionary containing background thresholds for each color channel. Calculated during background detection. Expected keys are "R", "G", and "B" with float values, e.g. {"R": 0.6, "G": 0.6, "B": 0.6}.

  • radius (int) – Radius of the disk-shaped structuring element used in morphological opening and closing operations.

Returns:

mask – Binary mask where True indicates detected pen regions.

Return type:

ndarray of bool, shape (M, N)

Notes

  • The function uses a disk-shaped structuring element obtained from get_strel_disk() for morphological operations.

  • This function does not remove pen marks of other colors.

Examples

>>> thr_back = {"R": 0.6, "G": 0.6, "B": 0.6}
>>> mask = remove_black_pen(image, thr_low=10, thr_back=thr_back, radius=3)
>>> image_no_pen = image.copy()
>>> image_no_pen[mask] = 0
histo_kit.tissue_seg.postprocessing.remove_gray_stains(img, mask=None)[source]

Remove gray stains from an RGB image based on low chroma component.

Parameters:
  • img (ndarray, shape (H, W, 3)) – Input RGB image as a NumPy array.

  • mask (ndarray of bool, shape (H, W), optional) – Precalculated mask from previous steps. If None, all pixels are considered for processing. Default is None.

Returns:

mask_out – Mask indicating pixels where gray stains have been removed.

Return type:

ndarray of bool, shape (H, W)

Notes

The function converts the image to LAB color space and computes the chroma component as sqrt(a^2 + b^2). Pixels with chroma greater than 2 are considered non-gray. If a mask is provided, the output is the intersection of the mask and the chroma threshold.

histo_kit.tissue_seg.postprocessing.remove_small_objects(mask, mode='otsu')[source]

Remove small objects from a binary mask based on area.

Objects smaller than a calculated threshold (roughly 5% of tissue area) are removed.

Parameters:
  • mask (ndarray of bool, shape (H, W)) – Binary mask containing objects to be filtered.

  • mode (string "otsu" or "kmeans") – Choose “otsu” to calculate the threshold based on two-step Otsu thresholding using histogram created from object sizes. Choose “kmeans” to calculate the threshold using kmeans method with the deterministic initialization. Centroids are set as minimum and maximum value in the areas vector.

Returns:

mask_out – Mask with small objects removed.

Return type:

ndarray of bool, shape (H, W)