← Back to all transforms

Solarize

Description

Invert all pixel values above a threshold.

    This transform applies a solarization effect to the input image. Solarization is a phenomenon in
    photography in which the image recorded on a negative or on a photographic print is wholly or
    partially reversed in tone. Dark areas appear light or light areas appear dark.

    In this implementation, all pixel values above a threshold are inverted.

    Args:
        threshold (float | tuple[float, float]): Range for solarizing threshold.
            If threshold is a single int, the range will be [threshold, threshold].
            If it's a tuple of (min, max), the range will be [min, max].
            The threshold should be in the range [0, 255] for uint8 images or [0, 1.0] for float images.
            Default: 128.
        p (float): Probability of applying the transform. Default: 0.5.

    Targets:
        image

    Image types:
        uint8, float32

    Note:
        - For uint8 images, pixel values above the threshold are inverted as: 255 - pixel_value
        - For float32 images, pixel values above the threshold are inverted as: 1.0 - pixel_value
        - The threshold is applied to each channel independently
        - This transform can create interesting artistic effects or be used for data augmentation

    Raises:
        TypeError: If the input image data type is not supported.

    Examples:
        >>> import numpy as np
        >>> import albumentations as A
        >>>
        # Solarize uint8 image with fixed threshold
        >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
        >>> transform = A.Solarize(threshold=128, p=1.0)
        >>> solarized_image = transform(image=image)['image']
        >>>
        # Solarize uint8 image with random threshold
        >>> transform = A.Solarize(threshold=(100, 200), p=1.0)
        >>> solarized_image = transform(image=image)['image']
        >>>
        # Solarize float32 image
        >>> image = np.random.rand(100, 100, 3).astype(np.float32)
        >>> transform = A.Solarize(threshold=0.5, p=1.0)
        >>> solarized_image = transform(image=image)['image']

    Mathematical Formulation:
        For each pixel value p and threshold t:
        if p > t:
            p_new = max_value - p
        else:
            p_new = p

        Where max_value is 255 for uint8 images and 1.0 for float32 images.

    See Also:
        Invert: For inverting all pixel values regardless of a threshold.
    

Parameters

  • threshold: float | tuple[float, float] (default: (128, 128))
  • p: float (default: 0.5)

Targets

  • Image

Try it out

Original Image:

Original

Result:

Transform result will appear here