Superpixels

Targets:
image
volume
Image Types:uint8, float32

Transform images partially/completely to their superpixel representation.

Arguments
p_replace
tuple[float, float] | float
[0,0.1]

Defines for any segment the probability that the pixels within that segment are replaced by their average color (otherwise, the pixels are not changed).

  • A probability of 0.0 would mean, that the pixels in no segment are replaced by their average color (image is not changed at all).
  • A probability of 0.5 would mean, that around half of all segments are replaced by their average color.
  • A probability of 1.0 would mean, that all segments are replaced by their average color (resulting in a voronoi image).

Behavior based on chosen data types for this parameter:

  • If a float, then that float will always be used.
  • If tuple (a, b), then a random probability will be sampled from the interval [a, b] per image. Default: (0.1, 0.3)
n_segments
tuple[int, int] | int
[100,100]

Rough target number of how many superpixels to generate. The algorithm may deviate from this number. Lower value will lead to coarser superpixels. Higher values are computationally more intensive and will hence lead to a slowdown. If tuple (a, b), then a value from the discrete interval [a..b] will be sampled per image. Default: (15, 120)

max_size
int | None
128

Maximum image size at which the augmentation is performed. If the width or height of an image exceeds this value, it will be downscaled before the augmentation so that the longest side matches max_size. This is done to speed up the process. The final output image has the same size as the input image. Note that in case p_replace is below 1.0, the down-/upscaling will affect the not-replaced pixels too. Use None to apply no down-/upscaling. Default: 128

interpolation
0 | 6 | 1 | 2 | 3 | 4 | 5
1

Flag that is used to specify the interpolation algorithm. Should be one of: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4. Default: cv2.INTER_LINEAR.

p
float
0.5

Probability of applying the transform. Default: 0.5.

Examples
>>> import numpy as np
>>> import albumentations as A
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)

# Apply superpixels with default parameters
>>> transform = A.Superpixels(p=1.0)
>>> augmented_image = transform(image=image)['image']

# Apply superpixels with custom parameters
>>> transform = A.Superpixels(
...     p_replace=(0.5, 0.7),
...     n_segments=(50, 100),
...     max_size=None,
...     interpolation=cv2.INTER_NEAREST,
...     p=1.0
... )
>>> augmented_image = transform(image=image)['image']
Notes
  • This transform can significantly change the visual appearance of the image.
  • The transform makes use of a superpixel algorithm, which tends to be slow. If performance is a concern, consider using max_size to limit the image size.
  • The effect of this transform can vary greatly depending on the p_replace and n_segments parameters.
  • When p_replace is high, the image can become highly abstracted, resembling a voronoi diagram.
  • The transform preserves the original image type (uint8 or float32).