Apply a stochastic identity-centered convolution kernel with configurable spectral strength and channel sharing for images and volumes.
The kernel is a discrete impulse plus a zero-mean Gaussian field. kernel_range controls the
odd side length K (the spectral resolution in PRIME), while strength_range controls the
perturbation energy. The random field is scaled by strength / K so the expected perturbation
energy remains comparable across kernel sizes.
kernel_rangeInclusive odd range for the square kernel side length. Values must be greater than or equal to 3. Default: (3, 7).
strength_rangeNon-negative range for the random field strength. Zero is an exact identity. Default: (0.0, 1.0).
per_channelIf True, sample an independent kernel for each channel. If False, share one kernel across all channels. Default: False.
border_modeOpenCV border policy. Supported values are constant, replicate, reflect,
and reflect-101; wrap is rejected because it is not supported by the convolution backend. Default:
cv2.BORDER_REFLECT_101.
pProbability of applying the transform. Default: 0.5.
>>> import numpy as np
>>> import albumentations as A
>>> import cv2
>>> image = np.random.default_rng(137).random((128, 128, 3), dtype=np.float32)
>>> transform = A.Compose(
... [
... A.StochasticConvolution(
... kernel_range=(3, 7),
... strength_range=(0.05, 0.25),
... border_mode=cv2.BORDER_REFLECT_101,
... p=1.0,
... ),
... ],
... seed=137,
... )
>>> transformed = transform(image=image)["image"]
Use `per_channel=True` for independent spectral perturbations, or `strength_range=(0.0, 0.0)` for an
exact identity while keeping the transform in a pipeline.cv2.BORDER_CONSTANT uses zero padding, matching the PRIME reference implementation. The default
reflect-101 border is the project-wide image-friendly choice.