pysdic.generate_synthetic_image#

generate_synthetic_image(camera, mesh, texture, *, displacement=None, n_samples=1, background=0.0, mask=None, margin=10.0)[source]#

Generate the synthetic image of a textured surface mesh seen by a camera.

Each pixel is sampled by \(n \times n\) points of the sensor (n_samples). The material point seen by each sample is found in the (deformed) mesh, and gets the value of the texture at its UV coordinates (see pysdic.interpolate_texture()). The samples that see no point of the mesh get the background value. The value of a pixel is the mean of its samples, which smooths the edges of the mesh (anti-aliasing).

The case is chosen from the dimension \(E\) of the world points of the camera, which must be the dimension of the mesh:

The sample \((i, j)\) of the pixel \((u, v)\) (row, column) is the image point:

\[x = v + \frac{j + 0.5}{n} - 0.5, \qquad y = u + \frac{i + 0.5}{n} - 0.5\]

The points are located in the mesh displaced by displacement, and the texture is evaluated with their natural coordinates: the texture is attached to the material and follows the displacement.

To save time, only the pixels around the projected mesh are computed: the vertices of the deformed mesh are projected once, and the samples are restricted to the pixels intersecting their bounding box extended by margin pixels (see pysdic.compute_projected_bounding_box()). The other pixels get the background value. The margin covers the parts of the surface projected out of the box of the vertices (distortion, curved elements): increase it for a strong distortion, or set margin=None to compute all the pixels.

Note

The image has no blur, no noise and is not quantized: use pysdic.add_gaussian_blur(), pysdic.add_poisson_noise(), pysdic.add_gaussian_noise(), pysdic.add_salt_and_pepper_noise() and pysdic.quantize_image() to get a realistic acquisition.

Parameters:
  • camera (pysdic.Camera) – The camera, with world points of dimension \(E = 2\) or \(3\) (the dimension of the mesh). The size of the image is the size of its sensor.

  • mesh (pysdic.Mesh) – The surface mesh (\(K = 2\)) in the reference configuration, with an element type and a UV map (see pysdic.Mesh.uvmap).

  • texture (Union[pysdic.Image, ArrayLike]) – The texture of the mesh, an pysdic.Image or an array of shape \((H_t, W_t)\) or \((H_t, W_t, C)\) (see pysdic.interpolate_texture()).

  • displacement (Optional[Union[str, ArrayLike]], optional) – The displacement of the vertices of the mesh, the name of a vertex property or an array of shape \((N_v, E)\). By default, the mesh is not displaced.

  • n_samples (Integral, optional (default: 1)) – The number \(n \geq 1\) of samples per pixel along each axis (\(n^2\) samples per pixel).

  • background (Real, optional (default: 0.0)) – The value of the samples that see no point of the mesh.

  • mask (Optional[ArrayLike], optional) – A boolean mask of shape \((H, W)\) or \((H \times W,)\) of the pixels to compute. The other pixels get the background value. By default, all the pixels (restricted by margin).

  • margin (Optional[Real], optional (default: 10.0)) – The margin in pixels (\(\geq 0\)) around the bounding box of the projected vertices of the deformed mesh: only the pixels intersecting the extended box are computed (combined with mask). If None, all the pixels are computed (the behavior without restriction, e.g. if a part of the surface is projected far from its vertices). If no vertex has a finite projection, all the pixels are computed.

Returns:

The image, a float array of shape \((H, W, C)\) with \(C\) the number of channels of the texture (\(C = 1\) for a grayscale texture).

Return type:

numpy.ndarray

Raises:
  • TypeError – If an input has a wrong type.

  • ValueError – If the mesh is not a surface mesh with a UV map, the dimension of the camera is not the dimension of the mesh, the extrinsic cannot be inverted (\(E = 2\)), or an input has a wrong shape or value.

Examples

Images of a speckled plate before and after a uniform stretching, with a camera observing the plane \(Z_w = 0\):

import numpy
from pysdic import Camera, add_gaussian_noise, compute_wave_speckle, generate_synthetic_image, quantize_image

u, v = numpy.meshgrid(numpy.linspace(0, 1, 1024), numpy.linspace(0, 1, 1024))
texture = compute_wave_speckle(numpy.stack((u, v), axis=-1), wavelength=0.01)

camera = Camera(1024, 1280, extrinsic=extrinsic, distortion=distortion, intrinsic=intrinsic)
reference = generate_synthetic_image(camera, mesh, texture, n_samples=3)

displacement = 0.01 * mesh.coordinates * [1.0, 0.0]  # 1 % along x
deformed = generate_synthetic_image(camera, mesh, texture, displacement=displacement, n_samples=3)

images = [
    quantize_image(add_gaussian_noise(image[..., 0], std=1.0, seed=k), bit_depth=8)
    for k, image in enumerate((reference, deformed))
]