pyblenderSDIC.patterns.create_speckle_BW_image#

create_speckle_BW_image(image_shape: Tuple[int, int], grain_size: Tuple[float, float] = (5.0, 5.0), grain_size_sigma: Tuple[float, float] | None = None, density: float = 0.5, white_intensity: float = 255.0, ratio_BW: float = 1.0, ratio_BW_sigma: float | None = None, invert: bool = False, seed: int | None = None) ndarray[source]#

Generate a black-and-white speckle pattern with Gaussian variation in grain size and coverage ratio.

This function creates a 2D (grayscale) or 3D (RGB) speckle image composed of elliptical grains randomly distributed over the image area. Grain sizes and coverage ratios can be fixed or vary according to Gaussian distributions defined by provided standard deviations. The image can be inverted to produce white grains on a black background.

Generate a default 512x512 grayscale speckle image with black grains on white background:

import matplotlib.pyplot as plt

# Set the parameters for the speckle image
image_shape = (512, 512)
grain_size = (5.0, 5.0)
grain_size_sigma = (1.0, 1.0)
density = 0.8
seed = 42

# Generate the speckle image with specified parameters
speckle_img = create_speckle_BW_image(
    image_shape=image_shape,
    grain_size=grain_size,
    grain_size_sigma=grain_size_sigma,
    density=density,
    seed=seed
)
../_images/speckle_image.png

Example of a speckle image generated with default parameters.#

Note

The size of the grains are clipped to a minimum of 1 pixel.

Parameters:
  • image_shape (tuple of int) – Dimensions of the output image. For grayscale: (height, width). For RGB: (height, width, 3).

  • grain_size (tuple of float, optional) – Mean grain size (in pixels) along the (x, y) axes. Default is (5.0, 5.0).

  • grain_size_sigma (tuple of float, optional) – Standard deviation of grain size along the (x, y) axes. If None, grain size is fixed at the mean value. Default is None.

  • density (float, optional) – Relative density of grains in the image, expressed as a strictly positive float. This parameter influences the number of grains drawn. Default is 0.5.

  • white_intensity (float, optional) – Intensity value for white color in the image. Must be strictly positive. Default is 255.0.

  • ratio_BW (float, optional) – Mean proportion of the image covered by grains. Interpreted as black grain coverage if invert=False, white grain coverage otherwise. Must be in the range [0, 1]. Default is 1.0.

  • ratio_BW_sigma (float, optional) – Standard deviation of the coverage ratio. If None, the coverage ratio is fixed. Default is None.

  • invert (bool, optional) – If True, grains are white on a black background. If False, grains are black on a white background. Default is False.

  • seed (int or None, optional) – Seed for the random number generator to ensure reproducibility. If None, the RNG is not seeded. Must be a non-negative integer less than 2^32. Default is None.

Returns:

Generated speckle image as a 2D array (grayscale) or 3D array (RGB) with float values in [0, white_intensity].

Return type:

numpy.ndarray