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 (seepysdic.interpolate_texture()). The samples that see no point of the mesh get thebackgroundvalue. 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:
\(E = 2\) (2D surface mesh): the camera is inverted to get the world point of each sample, \(\vec{x}_w = \text{Extrinsic}^{-1}(\text{Distortion}^{-1}(\text{Intrinsic}^{-1}(\vec{x}_i)))\) (see
pysdic.undistort_points()), then the point is located in the mesh (seepysdic.Mesh.locate_points()). This covers a camera of the plane (pysdic.PlanarRigidExtrinsic,pysdic.PlanarHomographicExtrinsic) and a 3D camera observing the plane \(Z_w = 0\) (pysdic.WorldPlanePerspectiveExtrinsic,pysdic.WorldPlaneOrthographicExtrinsic), whose inverse is the intersection of the ray of the sample with the plane. The extrinsic must implementinvert.\(E = 3\) (3D surface mesh): the ray of each sample is computed (see
pysdic.compute_rays()) and intersected with the mesh (seepysdic.Mesh.cast_rays()). The first intersection is the point seen by the sample, so the occlusions are taken into account. The surfaces seen from the back are rendered as the others (the orientation of the surfaces is a convention of the user). Requires the optional packageopen3d.
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
marginpixels (seepysdic.compute_projected_bounding_box()). The other pixels get thebackgroundvalue. 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 setmargin=Noneto 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()andpysdic.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 (seepysdic.Mesh.uvmap).texture (Union[
pysdic.Image, ArrayLike]) – The texture of the mesh, anpysdic.Imageor an array of shape \((H_t, W_t)\) or \((H_t, W_t, C)\) (seepysdic.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
backgroundvalue. By default, all the pixels (restricted bymargin).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 withmask). IfNone, 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:
- 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)) ]