Skip to content

segment_images

Example to show how to configure and use the segmentation module of the cuisto package.

For fiber-like objects, binarize and skeletonize the image, then use skan to extract branches coordinates. For polygon-like objects, binarize the image and detect objects and extract contours coordinates. For points, treat that as polygons then extract the centroids instead of contours. Finally, export the coordinates as collections in geojson files, importable in QuPath. Supports any number of channel of interest within the same image. One output file per iamge, per channel will be created.

This script uses cuisto.segmentation. It is designed to work on probability maps generated from a pixel classifier in QuPath, but might work on raw images.

Usage : fill-in the Parameters section of the script and run it. Explanation is given as a docstring below each parameter.

Masks can be used to exclude objects detected too close to the edges, you can disable that by setting the EDGE_DIST parameter to 0 (then you can just ignore the masks-related parameters).

A "geojson" folder will be created in the parent directory of IMAGES_DIR. To exclude objects near the edges of an ROI, specify the path to masks stored as images with the same names as probabilities images (without their suffix).

CHANNELS_PARAMS = [{'name': 'cy5', 'target_channel': 0, 'proba_threshold': 0.85, 'qp_class': 'Fibers: Cy5', 'qp_color': [164, 250, 120]}, {'name': 'dsred', 'target_channel': 1, 'proba_threshold': 0.65, 'qp_class': 'Fibers: DsRed', 'qp_color': [224, 153, 18]}, {'name': 'egfp', 'target_channel': 2, 'proba_threshold': 0.85, 'qp_class': 'Fibers: EGFP', 'qp_color': [135, 11, 191]}] module-attribute #

This should be a list of dictionary (one per channel) with keys :

  • name: str, used as suffix for output geojson files, not used if only one channel
  • target_channel: int, index of the segmented channel of the image, 0-based
  • proba_threshold: float < 1, probability cut-off for that channel
  • qp_class: str, name of QuPath classification
  • qp_color: list of RGB values, associated color

EDGE_DIST = 0 module-attribute #

Distance to brain edge to ignore, in µm. 0 to disable.

FILTERS = {'length_low': 1.5, 'area_low': 10, 'area_high': 1000, 'ecc_low': 0.0, 'ecc_high': 0.9, 'dist_thresh': 30} module-attribute #

Dictionary with keys :

  • length_low: minimal length in microns - for lines
  • area_low: minimal area in µm² - for polygons and points
  • area_high: maximal area in µm² - for polygons and points
  • ecc_low: minimal eccentricity - for polygons and points (0 = circle)
  • ecc_high: maximal eccentricity - for polygons and points (1 = line)
  • dist_thresh: maximal inter-point distance in µm - for points

IMAGES_DIR = '/path/to/images' module-attribute #

Full path to the images to segment.

IMG_SUFFIX = '_Probabilities.tiff' module-attribute #

Images suffix, including extension. Masks must be the same name without the suffix.

MASKS_DIR = 'path/to/corresponding/masks' module-attribute #

Full path to the masks, to exclude objects near the brain edges (set to None or empty string to disable this feature).

MASKS_EXT = 'tiff' module-attribute #

Masks files extension.

MAX_PIX_VALUE = 255 module-attribute #

Maximum pixel possible value to adjust proba_threshold.

ORIGINAL_PIXELSIZE = 0.45 module-attribute #

Original images pixel size in microns. This is in case the pixel classifier uses a lower resolution, yielding smaller probability maps, so output objects coordinates need to be rescaled to the full size images. The pixel size is written in the "Image" tab in QuPath.

QUPATH_TYPE = 'detection' module-attribute #

QuPath object type: 'annotation' or 'detection'

SEGTYPE = 'cells' module-attribute #

Type of segmentation, must match one the hardcoded keywords to associate it to 'fibers', 'points' or polygon'. See cuisto.segmentation doc.