imgaug.augmentables.segmaps

Classes dealing with segmentation maps.

E.g. masks, semantic or instance segmentation maps.

imgaug.augmentables.segmaps.SegmentationMapOnImage(*args, **kwargs)[source]

Deprecated. Use SegmentationMapsOnImage instead. (Note the plural ‘Maps’ instead of old ‘Map’.).

Object representing a segmentation map associated with an image.

class imgaug.augmentables.segmaps.SegmentationMapsOnImage(arr, shape, nb_classes=None)[source]

Bases: imgaug.augmentables.base.IAugmentable

Object representing a segmentation map associated with an image.

Variables:

DEFAULT_SEGMENT_COLORS (list of tuple of int) – Standard RGB colors to use during drawing, ordered by class index.

Parameters:
  • arr ((H,W) ndarray or (H,W,C) ndarray) – Array representing the segmentation map(s). May have dtypes bool, int or uint.
  • shape (tuple of int) – Shape of the image on which the segmentation map(s) is/are placed. Not the shape of the segmentation map(s) array, unless it is identical to the image shape (note the likely difference between the arrays in the number of channels). This is expected to be (H, W) or (H, W, C) with C usually being 3. If there is no corresponding image, use (H_arr, W_arr) instead, where H_arr is the height of the segmentation map(s) array (analogous W_arr).
  • nb_classes (None or int, optional) – Deprecated.

Methods

copy(self[, arr, shape]) Create a shallow copy of the segmentation map object.
deepcopy(self[, arr, shape]) Create a deep copy of the segmentation map object.
draw(self[, size, colors]) Render the segmentation map as an RGB image.
draw_on_image(self, image[, alpha, resize, …]) Draw the segmentation map as an overlay over an image.
get_arr(self) Return the seg.map array, with original dtype and shape ndim.
get_arr_int(self, *args, **kwargs) Deprecated.
pad(self[, top, right, bottom, left, mode, cval]) Pad the segmentation maps at their top/right/bottom/left side.
pad_to_aspect_ratio(self, aspect_ratio[, …]) Pad the segmentation maps until they match a target aspect ratio.
resize(self, sizes[, interpolation]) Resize the seg.map(s) array given a target size and interpolation.
scale(self, *args, **kwargs) Deprecated.
DEFAULT_SEGMENT_COLORS = [(0, 0, 0), (230, 25, 75), (60, 180, 75), (255, 225, 25), (0, 130, 200), (245, 130, 48), (145, 30, 180), (70, 240, 240), (240, 50, 230), (210, 245, 60), (250, 190, 190), (0, 128, 128), (230, 190, 255), (170, 110, 40), (255, 250, 200), (128, 0, 0), (170, 255, 195), (128, 128, 0), (255, 215, 180), (0, 0, 128), (128, 128, 128), (255, 255, 255), (115, 12, 37), (30, 90, 37), (127, 112, 12), (0, 65, 100), (122, 65, 24), (72, 15, 90), (35, 120, 120), (120, 25, 115), (105, 122, 30), (125, 95, 95), (0, 64, 64), (115, 95, 127), (85, 55, 20), (127, 125, 100), (64, 0, 0), (85, 127, 97), (64, 64, 0), (127, 107, 90), (0, 0, 64), (64, 64, 64)]
copy(self, arr=None, shape=None)[source]

Create a shallow copy of the segmentation map object.

Parameters:
  • arr (None or (H,W) ndarray or (H,W,C) ndarray, optional) – Optionally the arr attribute to use for the new segmentation map instance. Will be copied from the old instance if not provided. See __init__() for details.
  • shape (None or tuple of int, optional) – Optionally the shape attribute to use for the the new segmentation map instance. Will be copied from the old instance if not provided. See __init__() for details.
Returns:

Shallow copy.

Return type:

imgaug.augmentables.segmaps.SegmentationMapsOnImage

deepcopy(self, arr=None, shape=None)[source]

Create a deep copy of the segmentation map object.

Parameters:
  • arr (None or (H,W) ndarray or (H,W,C) ndarray, optional) – Optionally the arr attribute to use for the new segmentation map instance. Will be copied from the old instance if not provided. See __init__() for details.
  • shape (None or tuple of int, optional) – Optionally the shape attribute to use for the the new segmentation map instance. Will be copied from the old instance if not provided. See __init__() for details.
Returns:

Deep copy.

Return type:

imgaug.augmentables.segmaps.SegmentationMapsOnImage

draw(self, size=None, colors=None)[source]

Render the segmentation map as an RGB image.

Parameters:
  • size (None or float or iterable of int or iterable of float, optional) – Size of the rendered RGB image as (height, width). See imresize_single_image() for details. If set to None, no resizing is performed and the size of the segmentation map array is used.
  • colors (None or list of tuple of int, optional) – Colors to use. One for each class to draw. If None, then default colors will be used.
Returns:

Rendered segmentation map (dtype is uint8). One per C in the original input array (H,W,C).

Return type:

list of (H,W,3) ndarray

draw_on_image(self, image, alpha=0.75, resize='segmentation_map', colors=None, draw_background=False, background_class_id=0, background_threshold=None)[source]

Draw the segmentation map as an overlay over an image.

Parameters:
  • image ((H,W,3) ndarray) – Image onto which to draw the segmentation map. Expected dtype is uint8.
  • alpha (float, optional) – Alpha/opacity value to use for the mixing of image and segmentation map. Larger values mean that the segmentation map will be more visible and the image less visible.
  • resize ({‘segmentation_map’, ‘image’}, optional) – In case of size differences between the image and segmentation map, either the image or the segmentation map can be resized. This parameter controls which of the two will be resized to the other’s size.
  • colors (None or list of tuple of int, optional) – Colors to use. One for each class to draw. If None, then default colors will be used.
  • draw_background (bool, optional) – If True, the background will be drawn like any other class. If False, the background will not be drawn, i.e. the respective background pixels will be identical with the image’s RGB color at the corresponding spatial location and no color overlay will be applied.
  • background_class_id (int, optional) – Class id to interpret as the background class. See draw_background.
  • background_threshold (None, optional) – Deprecated. This parameter is ignored.
Returns:

Rendered overlays as uint8 arrays. Always a list containing one RGB image per segmentation map array channel.

Return type:

list of (H,W,3) ndarray

get_arr(self)[source]

Return the seg.map array, with original dtype and shape ndim.

Here, “original” denotes the dtype and number of shape dimensions that was used when the SegmentationMapsOnImage instance was created, i.e. upon the call of SegmentationMapsOnImage.__init__(). Internally, this class may use a different dtype and shape to simplify computations.

Note

The height and width may have changed compared to the original input due to e.g. pooling operations.

Returns:Segmentation map array. Same dtype and number of dimensions as was originally used when the SegmentationMapsOnImage instance was created.
Return type:ndarray
get_arr_int(self, *args, **kwargs)[source]

Deprecated. Use SegmentationMapsOnImage.get_arr() instead.

Return the seg.map array, with original dtype and shape ndim.

pad(self, top=0, right=0, bottom=0, left=0, mode='constant', cval=0)[source]

Pad the segmentation maps at their top/right/bottom/left side.

Parameters:
  • top (int, optional) – Amount of pixels to add at the top side of the segmentation map. Must be 0 or greater.
  • right (int, optional) – Amount of pixels to add at the right side of the segmentation map. Must be 0 or greater.
  • bottom (int, optional) – Amount of pixels to add at the bottom side of the segmentation map. Must be 0 or greater.
  • left (int, optional) – Amount of pixels to add at the left side of the segmentation map. Must be 0 or greater.
  • mode (str, optional) – Padding mode to use. See pad() for details.
  • cval (number, optional) – Value to use for padding if mode is constant. See pad() for details.
Returns:

Padded segmentation map with height H'=H+top+bottom and width W'=W+left+right.

Return type:

imgaug.augmentables.segmaps.SegmentationMapsOnImage

pad_to_aspect_ratio(self, aspect_ratio, mode='constant', cval=0, return_pad_amounts=False)[source]

Pad the segmentation maps until they match a target aspect ratio.

Depending on which dimension is smaller (height or width), only the corresponding sides (left/right or top/bottom) will be padded. In each case, both of the sides will be padded equally.

Parameters:
  • aspect_ratio (float) – Target aspect ratio, given as width/height. E.g. 2.0 denotes the image having twice as much width as height.
  • mode (str, optional) – Padding mode to use. See pad() for details.
  • cval (number, optional) – Value to use for padding if mode is constant. See pad() for details.
  • return_pad_amounts (bool, optional) – If False, then only the padded instance will be returned. If True, a tuple with two entries will be returned, where the first entry is the padded instance and the second entry are the amounts by which each array side was padded. These amounts are again a tuple of the form (top, right, bottom, left), with each value being an integer.
Returns:

  • imgaug.augmentables.segmaps.SegmentationMapsOnImage – Padded segmentation map as SegmentationMapsOnImage instance.
  • tuple of int – Amounts by which the instance’s array was padded on each side, given as a tuple (top, right, bottom, left). This tuple is only returned if return_pad_amounts was set to True.

resize(self, sizes, interpolation='nearest')[source]

Resize the seg.map(s) array given a target size and interpolation.

Parameters:
  • sizes (float or iterable of int or iterable of float) – New size of the array in (height, width). See imresize_single_image() for details.
  • interpolation (None or str or int, optional) – The interpolation to use during resize. Nearest neighbour interpolation ("nearest") is almost always the best choice. See imresize_single_image() for details.
Returns:

Resized segmentation map object.

Return type:

imgaug.augmentables.segmaps.SegmentationMapsOnImage

scale(self, *args, **kwargs)[source]

Deprecated. Use SegmentationMapsOnImage.resize() instead. resize() has the exactly same interface.

Resize the seg.map(s) array given a target size and interpolation.