Documentation 0.1.2 Help

preprocessing.extract_segments

Functions

extract_segments_from_labels

Extracts segments from a label classification.

extract_segments_from_bounds

Extracts segments from Rect bounds.

remove_small_segments_from_labels

Removes small segments from a label classification.

Extract with Boundaries

A boundary is a Rect object that represents the minimum bounding box of a segment, with x0, y0, x1, y1 coordinates.

It simply slices the original image to the bounding box. The origin is the top left corner of the image.

+-----------------+ +-----------+ | Original | | Segmented | | Image | | Image | +-----+-----+-----+ +-----+-----+ | 1 | 2 | 3 | | 2 | 3 | +-----+-----+-----+ +-----+-----+ | 4 | 5 | 6 | -----------> | 5 | 6 | +-----+-----+-----+ 1, 2, 0, 2 +-----+-----+ | 7 | 8 | 9 | x0 y0 x1 y1 | 8 | 9 | +-----+-----+-----+ +-----+-----+
+-----------------+ +-----------------+ | Original | | Segmented | | Image | | Image | +-----+-----+-----+ +-----+-----+-----+ | 1 | 2 | 3 | | 0 | 2 | 3 | +-----+-----+-----+ +-----+-----+-----+ | 4 | 5 | 6 | -----------> | 0 | 5 | 6 | +-----+-----+-----+ 1, 2, 0, 2 +-----+-----+-----+ | 7 | 8 | 9 | x0 y0 x1 y1 | 0 | 8 | 9 | +-----+-----+-----+ +-----+-----+-----+

Extract with Labels

A label classification is a np.ndarray where each pixel is mapped to a segment. The segments are mapped to a unique integer. In our project, the 0th label is the background.

For example, a label classification of 3 segments will look like this:

+-----------------+ +-----------------+ | Label | | Original | | Classification | | Image | +-----+-----+-----+ +-----+-----+-----+ | 1 | 2 | 0 | | 1 | 2 | 3 | +-----+-----+-----+ +-----+-----+-----+ | 1 | 2 | 2 | | 4 | 5 | 6 | +-----+-----+-----+ +-----+-----+-----+ | 1 | 1 | 0 | | 7 | 8 | 9 | +-----+-----+-----+ +-----+-----+-----+

The extraction will take the minimum bounding box of each segment and return a list of segments.

For example, the label 1 and 2 extracted images will be

+-----------+ +-----------+ | Extracted | | Extracted | | Segment 1 | | Segment 2 | +-----+-----+ +-----+-----+ | 1 | 0 | | 2 | 0 | +-----+-----+ +-----+-----+ | 4 | 0 | | 5 | 6 | +-----+-----+ +-----+-----+ | 7 | 8 | +-----+-----+
+-----------------+ +-----------------+ | Extracted | | Extracted | | Segment 1 | | Segment 2 | +-----+-----+-----+ +-----+-----+-----+ | 1 | 0 | 0 | | 0 | 2 | 0 | +-----+-----+-----+ +-----+-----+-----+ | 4 | 0 | 0 | | 0 | 5 | 6 | +-----+-----+-----+ +-----+-----+-----+ | 7 | 8 | 0 | | 0 | 0 | 0 | +-----+-----+-----+ +-----+-----+-----+
  • If cropped is False, the segments are padded with 0s to the original image size. While this can ensure shape consistency, it can consume more memory for large images.

  • If cropped is True, the segments are cropped to the minimum bounding box. This can save memory, but the shape of the segments will be inconsistent.

Usage

Extract from Bounds and Labels

Extract segments from bounds and labels.

import numpy as np from frdc.load.preset import FRDCDatasetPreset from frdc.preprocess.extract_segments import extract_segments_from_bounds ds = FRDCDatasetPreset.chestnut_20201218() ar, order = ds._get_ar_bands() bounds, labels = ds._get_legacy_bounds_and_labels() segments: list[np.ndarray] = extract_segments_from_bounds(ar, bounds)

Extract from Auto-Segmentation

Extract segments from a label classification.

from skimage.morphology import remove_small_objects, remove_small_holes import numpy as np from frdc.load.preset import FRDCDatasetPreset from frdc.preprocess.morphology import ( threshold_binary_mask, binary_watershed ) from frdc.preprocess.scale import scale_0_1_per_band from frdc.preprocess.extract_segments import ( extract_segments_from_labels, remove_small_segments_from_labels ) ds = FRDCDatasetPreset.chestnut_20201218() ar, order = ds._get_ar_bands() ar = scale_0_1_per_band(ar) ar_mask = threshold_binary_mask(ar, -1, 90 / 256) ar_mask = remove_small_objects(ar_mask, min_size=100, connectivity=2) ar_mask = remove_small_holes(ar_mask, area_threshold=100, connectivity=2) ar_labels = binary_watershed(ar_mask) ar_labels = remove_small_segments_from_labels(ar_labels, min_height=10, min_width=10) segments: list[np.ndarray] = extract_segments_from_labels(ar, ar_labels)

API

extract_segments_from_labels(ar, ar_labels, cropped)

Extracts segments from a label classification.


ar_labels is a label classification as a np.ndarray

extract_segments_from_bounds(ar, bounds, cropped)

Extracts segments from Rect bounds.


bounds is a list of Rect bounds.

remove_small_segments_from_labels(ar_labels, min_height, min_width)

Removes small segments from a label classification.


Last modified: 26 June 2024