brails.utils.segmentation_utils module

This module provides utilities for visualizing segmentation results.

SegmentationUtils()

Utility class for creating visualizations of segmentation masks on images.

class brails.utils.segmentation_utils.SegmentationUtils

Bases: object

Utility class for creating visualizations of segmentation masks on images.

This class provides static methods to overlay a segmentation mask onto a base image and to convert class-labeled masks to RGB images.

Please use the following statement to import SegmentationUtils.

from brails.utils import SegmentationUtils
static create_overlaid_image(base_image, mask_image, output_dir)

Overlay a segmentation mask on a base image and save the result.

The segmentation mask is blended with the original image using a fixed transparency (alpha) to produce a visually interpretable output.

Parameters:
  • base_image (str or Path) – Path to the original image file.

  • mask_image (np.ndarray) – A 2D array of class labels representing the segmentation mask.

  • output_dir (str or Path) – Directory where the overlaid image will be saved. Will be created if it does not exist.

Returns:

None

Example

This example assumes that a valid image file named example_image.jpg exists in the current working directory.

>>> import numpy as np
>>> from brails.utils import SegmentationUtils
>>> mask = np.zeros((100, 100), dtype=int)
>>> mask[25:75, 25:75] = 1
>>> SegmentationUtils.create_overlaid_image(
...     base_image='example_image.jpg',
...     mask_image=mask,
...     output_dir='overlaid_images'
... )
static decode_segmap(image, nc=5)

Convert a class-labeled mask to an RGB image.

Parameters:
  • image (np.ndarray) – 2D array of class indices representing the mask.

  • nc (int, optional) – Number of classes. Default is 5.

Returns:

RGB image (H, W, 3) representation of the mask.

Return type:

np.ndarray

Example

>>> import numpy as np
>>> from brails.utils import SegmentationUtils
>>> mask = np.zeros((50, 50), dtype=int)
>>> mask[10:30, 10:30] = 2
>>> rgb_mask = SegmentationUtils.decode_segmap(mask)
>>> rgb_mask.shape
(50, 50, 3)