histo_kit.tissue_seg.bg_segmentation

Functions

plot_rgb_hist(R, G, B, thr)

Plot histograms for each RGB channel with threshold indicators.

segment_tissue(slide_file, args, paths_dict)

wsi_tissue_seg(region[, fill_holes, ...])

Segment tissue regions in a whole-slide image (WSI) region using GaMRed or Otsu algorithms.

histo_kit.tissue_seg.bg_segmentation.plot_rgb_hist(R, G, B, thr)[source]

Plot histograms for each RGB channel with threshold indicators.

This function creates a stacked plot of histograms for the Red, Green, and Blue channels of an image and overlays vertical lines indicating threshold values for each channel.

Parameters:
  • R (ndarray of shape (256,)) – Histogram of pixel counts for the Red channel.

  • G (ndarray of shape (256,)) – Histogram of pixel counts for the Green channel.

  • B (ndarray of shape (256,)) – Histogram of pixel counts for the Blue channel.

  • thr (dict) – Dictionary of threshold values for each color channel: - "R" : threshold for Red channel - "G" : threshold for Green channel - "B" : threshold for Blue channel

Returns:

  • fig (matplotlib.figure.Figure) – Figure object containing the plotted histograms.

  • axs (ndarray of matplotlib.axes.Axes) – Array of axes objects corresponding to each color channel subplot.

Notes

  • Histograms are plotted on a logarithmic y-scale to better visualize differences in pixel counts.

  • Vertical dashed lines represent the thresholds specified in thr.

Examples

>>> fig, axs = plot_rgb_hist(R, G, B, thr)
>>> plt.show()
histo_kit.tissue_seg.bg_segmentation.segment_tissue(slide_file, args, paths_dict)[source]
histo_kit.tissue_seg.bg_segmentation.wsi_tissue_seg(region, fill_holes=False, open_disk_r=2, close_disk_r=2, rem_small_obj=True)[source]

Segment tissue regions in a whole-slide image (WSI) region using GaMRed or Otsu algorithms.

This function performs tissue detection on a WSI region by first computing per-channel thresholds (GaMRed algorithm with fallback to Otsu if thresholds are too low). It removes black pen markings, eliminates background and gray stains, optionally fills holes, and applies morphological operations to clean the mask.

Parameters:
  • region (array_like) – WSI image region (RGB) for tissue detection.

  • fill_holes (bool, optional) – If True, fill holes in the tissue mask using binary filling. Default is False.

  • open_disk_r (int, optional) – Radius of the disk-shaped structuring element used for morphological opening. Default is 2.

  • close_disk_r (int, optional) – Radius of the disk-shaped structuring element used for morphological closing. Default is 2.

  • rem_small_obj (bool, optional) – If True, remove small objects around the tissue region. Default is True.

Returns:

result – Dictionary containing segmentation results and intermediate data: - "mask" : ndarray, tissue mask (1 = tissue, 0 = background) - "mask_pen" : ndarray, mask for detected black pen regions - "R", "G", "B" : ndarray, histograms of Red, Green, and Blue channels - "thr" : dict, threshold values for each color channel

Return type:

dict

Notes

  • Thresholds are computed using get_thr_image().

  • Black pen regions are removed using remove_pen().

  • Gray stains with low chroma are removed via remove_gray_stains().

  • Morphological opening and closing help refine the mask and remove noise.

  • Small objects in the mask are removed to keep only significant tissue regions.

  • Input images should be RGB NumPy arrays with pixel values in range [0, 255].

Examples

>>> result = wsi_tissue_seg(region, fill_holes=True, open_disk_r=3, close_disk_r=3)
>>> tissue_mask = result["mask"]
>>> print("Red channel histogram:", result["R"])
>>> print("Thresholds:", result["thr"])

References

Method is described in [1].