Superpixels
Transform images partially/completely to their superpixel representation.
p_replaceDefines 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.0would mean, that the pixels in no segment are replaced by their average color (image is not changed at all). - A probability of
0.5would mean, that around half of all segments are replaced by their average color. - A probability of
1.0would 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 thatfloatwill 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_segmentsRough 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_sizeMaximum 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
interpolationFlag 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.
pProbability of applying the transform. Default: 0.5.
>>> 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']- 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_sizeto limit the image size. - The effect of this transform can vary greatly depending on the
p_replaceandn_segmentsparameters. - When
p_replaceis high, the image can become highly abstracted, resembling a voronoi diagram. - The transform preserves the original image type (uint8 or float32).