Skip to content

ElasticNoise & RandomElasticNoise Modules

Note

ElasticNoise Module

The ElasticNoise class applies elastic deformation noise to an image, introducing smooth, random distortions by manipulating displacement fields.

Attributes

  • alpha : float

    The scaling factor for the displacement field. A higher alpha value results in stronger distortions.

  • sigma : float

    The standard deviation for the Gaussian filter applied to the displacement field. A larger sigma value results in smoother distortions.

Methods

add_noise()

add_noise(self, image: Image) -> Image:
Public method that applies elastic deformation noise to the input image by distorting its pixels based on displacement fields.

  • Parameters:

    • image : Image

      The input image to which elastic noise will be added.

  • Returns:

    • Image:

      The image with elastic noise applied.

_elastic_noise()

_elastic_noise(self, image: Image) -> Image:
Internal method that applies elastic noise by generating random displacement fields and distorting the image accordingly.

  • Parameters:

    • image : Image

      The image to which the elastic deformation will be applied.

  • Returns:

    • Image:

      The image with elastic deformation applied.

Usage Example

from PIL import Image
from iftg.noises import ElasticNoise

# Open an image
image = Image.open('path/to/image.tif')

# Create an ElasticNoise object with alpha 12 and sigma 3
elastic_noise = ElasticNoise(alpha=12, sigma=3)

# Apply elastic noise to the image
noisy_image = elastic_noise.add_noise(image)

# Save the noisy image
noisy_image.save('path/to/noisy_image.tif')


RandomElasticNoise Module

The RandomElasticNoise class extends this functionality by randomly selecting the distortion intensity and smoothness from specified ranges, adding more variation to the applied noise.

Attributes

  • alpha_range : tuple[float, float]

    The range of possible values for alpha (scaling factor for displacement field)

  • sigma_range : tuple[float, float]

    The range of possible values for sigma (standard deviation for Gaussian filter)

Methods

add_noise()

Note

Each call to add_noise() results in a different elasticity noises, adding variability to the elasticity adjustment. This is particularly useful when processing multiple images to introduce diverse elasticity levels.

add_noise(self, image: Image) -> Image:
Public method that applies elastic noise with random alpha and sigma values to the input image.

  • Parameters:

    • image : Image

      The input image to which random elastic noise will be applied.

  • Returns:

    • Image:

      The image with random elastic noise applied.

Usage Example

from PIL import Image
from iftg.noises import RandomElasticNoise

# Open an image
image = Image.open('path/to/image.tif')

# Create a RandomElasticNoise object with alpha range (15, 25) and sigma range (3, 5)
random_elastic_noise = RandomElasticNoise(alpha_range=(15, 25), sigma_range=(3, 5))

# Apply random elastic noise to the image
random_noisy_image = random_elastic_noise.add_noise(image)

# Save the randomly noisy image
random_noisy_image.save('path/to/random_noisy_image.tif')