.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/workflows/example_workflow_quality_control.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__gallery_workflows_example_workflow_quality_control.py: .. _example_workflows_quality_control: Controlling the quality of a measure ==================================== .. contents:: Table of Contents :local: :depth: 1 :backlinks: top After a measure, :mod:`pysdic.workflows` gives two controls: - :func:`pysdic.workflows.compute_elements_residual_rms`: the RMS of the residuals per element and per pairing, and the badly correlated elements (reflections, occlusions); - :func:`pysdic.workflows.compute_uncertainty`: the covariance of the displacements from the Jacobian of the term and the noise of the images. .. important:: Requires the optional package ``open3d`` (ray casting of the 3D surface). .. GENERATED FROM PYTHON SOURCE LINES 26-32 A measure with a reflection --------------------------- The plate of :ref:`example_sdic_displacement` moves between the frames 0 and 1, with a noise of 1 gray level. In the image of the first camera at the frame 1, a reflection saturates a disk of 10 pixels around the point :math:`(0.5, 0.25, 0)` of the plate. .. GENERATED FROM PYTHON SOURCE LINES 32-83 .. code-block:: Python import numpy from pysdic import ( Camera, Image, PerspectiveExtrinsic, PinholeIntrinsic, add_gaussian_noise, compute_projected_roi, compute_wave_speckle, create_3d_quadrangle_4_heightmap, create_bicamera_analysis, generate_synthetic_image, quantize_image, ) height, width = 180, 240 cameras = [ Camera( height, width, extrinsic=PerspectiveExtrinsic.from_rt(rvec=[numpy.pi, side * 0.15, 0.0], tvec=[side * 0.6, 0.0, 5.0]), intrinsic=PinholeIntrinsic.from_matrix([[300.0, 0.0, 120.0], [0.0, 300.0, 90.0], [0.0, 0.0, 1.0]]), ) for side in (1, -1) ] plate = create_3d_quadrangle_4_heightmap(x_bounds=(-2.5, 2.5), y_bounds=(-2.0, 2.0), n_x=41, n_y=33) x, y = numpy.linspace(-2.5, 2.5, 500), numpy.linspace(2.0, -2.0, 400) texture = compute_wave_speckle(numpy.stack(numpy.meshgrid(x, y), axis=-1), wavelength=0.15) def true_displacement(points: numpy.ndarray) -> numpy.ndarray: x, y = points[:, 0], points[:, 1] return numpy.stack((0.04 + 0.02 * y, -0.03 + 0.01 * x, 0.05 + 0.02 * x), axis=1) mesh = create_3d_quadrangle_4_heightmap(x_bounds=(-1.0, 1.0), y_bounds=(-0.6, 0.6), n_x=7, n_y=5) truth = true_displacement(mesh.coordinates) reflection = numpy.array([[0.5, 0.25, 0.0]]) def acquire(camera: Camera, frame: int, seed: int, reflect: bool = False) -> Image: rendered = generate_synthetic_image(camera, plate, texture, displacement=frame * true_displacement(plate.coordinates), n_samples=2) data = add_gaussian_noise(rendered[..., 0], std=1.0, seed=seed) if reflect: cx, cy = camera.project(reflection + true_displacement(reflection)).image_points[0] rows, columns = numpy.mgrid[0:height, 0:width] data[(columns - cx) ** 2 + (rows - cy) ** 2 <= 10.0**2] = 255.0 data = quantize_image(data, bit_depth=8) return Image.from_array(data, roi=compute_projected_roi(mesh, camera, warp_by=frame * truth, margin=10)) images = [ [acquire(camera, frame, seed=2 * frame + k, reflect=(k == 0 and frame == 1)) for frame in range(2)] for k, camera in enumerate(cameras) ] analysis = create_bicamera_analysis(mesh, cameras[0], cameras[1], images[0], images[1]) analysis.set_zero_displacement(0) .. GENERATED FROM PYTHON SOURCE LINES 84-86 The displacement of the frame 1 is measured with the temporal pairings of both cameras and the stereo pairing of the frame 1, without gray level correction (same lighting). .. GENERATED FROM PYTHON SOURCE LINES 86-106 .. code-block:: Python import pysolvegn from pysdic import Pairing, SDICCorrelationTerm, SDICUnknowns from pysdic.workflows import create_grid_integration_points pairings = [Pairing.temporal(0, 0, 1), Pairing.temporal(1, 0, 1), Pairing.stereo(1, 0, 1)] analysis.set_displacement(1, 0.7 * truth) unknowns = SDICUnknowns([1], []) def measure(correlation: SDICCorrelationTerm) -> SDICCorrelationTerm: analysis.set_displacement(1, 0.7 * truth) # Same initial guess for each measure result = pysolvegn.solve(terms=[correlation.term], p0=analysis.pack_unknowns(unknowns), max_iteration=30) analysis.apply_unknowns(unknowns, result.parameters) print(f"Maximum error on the displacement: {numpy.abs(analysis.get_displacement(1) - truth).max():.2e}") return correlation correlation = measure(SDICCorrelationTerm(analysis, unknowns, create_grid_integration_points(mesh, 6), pairings)) .. rst-class:: sphx-glr-script-out .. code-block:: none Maximum error on the displacement: 1.37e-01 .. GENERATED FROM PYTHON SOURCE LINES 107-113 Badly correlated elements ------------------------- The RMS of the residuals of each element is compared with the other elements of the pairing (median and robust spread): the elements of the reflection stand out in the two pairings that use the view ``(1, 0)``. .. GENERATED FROM PYTHON SOURCE LINES 113-134 .. code-block:: Python import matplotlib.pyplot as plt from pysdic import ImagePlotter from pysdic.workflows import compute_elements_residual_rms quality = compute_elements_residual_rms(analysis, correlation.integration_points, correlation.compute_residuals()) for pairing, rms, outliers in zip(quality.pairings, quality.pairing_rms, quality.outliers): print(f"{pairing}: RMS {rms:.1f} gray levels, outliers {numpy.flatnonzero(outliers)}") print(f"Badly correlated elements: {quality.bad_elements}") fig, axes = plt.subplots(1, 3, figsize=(16, 4)) for ax, pairing, rms in zip(axes, quality.pairings, quality.rms): plotter = ImagePlotter(analysis.get_view(*pairing.view_2), ax=ax) plotter.add_image() plotter.add_mesh( mesh, warp_by=analysis.get_displacement(1), element_property=rms, property_cmap="viridis", face_opacity=0.7, show_vertices=False, show_bar=True, ) plotter.set_title(f"RMS per element, {pairing}") plt.show() .. image-sg:: /_gallery/workflows/images/sphx_glr_example_workflow_quality_control_001.png :alt: RMS per element, Pairing((frame 0, view 0) -> (frame 1, view 0)), RMS per element, Pairing((frame 0, view 1) -> (frame 1, view 1)), RMS per element, Pairing((frame 1, view 0) -> (frame 1, view 1)) :srcset: /_gallery/workflows/images/sphx_glr_example_workflow_quality_control_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Pairing((frame 0, view 0) -> (frame 1, view 0)): RMS 24.3 gray levels, outliers [15 16 17 21 22 23] Pairing((frame 0, view 1) -> (frame 1, view 1)): RMS 3.5 gray levels, outliers [16 17 21 22 23] Pairing((frame 1, view 0) -> (frame 1, view 1)): RMS 23.8 gray levels, outliers [14 15 16 17 20 21 22 23] Badly correlated elements: [14 15 16 17 20 21 22 23] .. GENERATED FROM PYTHON SOURCE LINES 135-137 The integration points of the bad elements are removed and the frame is measured again, with a new term on the same unknowns (:meth:`pysdic.SDICCorrelationTerm.with_integration_points`). .. GENERATED FROM PYTHON SOURCE LINES 137-141 .. code-block:: Python good_elements = numpy.setdiff1d(numpy.arange(mesh.n_elements), quality.bad_elements) correlation = measure(correlation.with_integration_points(create_grid_integration_points(mesh, 6, elements=good_elements))) .. rst-class:: sphx-glr-script-out .. code-block:: none Maximum error on the displacement: 2.10e-02 .. GENERATED FROM PYTHON SOURCE LINES 142-150 Uncertainty of the displacements -------------------------------- The covariance :math:`s^2 H^{-1} (J^T W J) H^{-1}` of the displacement of each vertex comes from the Jacobian of the term at convergence. The variance :math:`s^2` of the residuals is estimated from the residuals (default), or given by the noise of the images (:math:`s^2 = 2 \sigma^2`, here :math:`\sigma \approx 1` gray level with the quantization): both agree when the residuals only come from the noise. .. GENERATED FROM PYTHON SOURCE LINES 150-162 .. code-block:: Python from pysdic.workflows import compute_uncertainty estimated = compute_uncertainty(correlation) from_noise = compute_uncertainty(correlation, image_noise_std=1.0) print(f"Residual variance: {estimated.residual_variance:.2f} (residuals), {from_noise.residual_variance:.2f} (noise)") std = estimated.standard_deviations[1] # (N_v, 3) error = numpy.abs(analysis.get_displacement(1) - truth) for c, name in enumerate("XYZ"): print(f"{name}: median standard deviation {numpy.nanmedian(std[:, c]):.1e}, RMS error {numpy.sqrt(numpy.mean(error[:, c]**2)):.1e}") .. rst-class:: sphx-glr-script-out .. code-block:: none Residual variance: 477.36 (residuals), 2.00 (noise) X: median standard deviation 4.4e-03, RMS error 1.2e-02 Y: median standard deviation 4.5e-03, RMS error 9.2e-03 Z: median standard deviation 1.7e-02, RMS error 1.6e-02 .. GENERATED FROM PYTHON SOURCE LINES 163-166 The standard deviation of :math:`U_Z`, interpolated on the faces from the vertices: the out-of-plane component is the least precise (it only comes from the stereo angle), and the vertices on the border of the mesh, supported by fewer points, are less precise than the inner ones. .. GENERATED FROM PYTHON SOURCE LINES 166-174 .. code-block:: Python plotter = ImagePlotter(analysis.get_view(1, 1)) plotter.add_image() plotter.add_mesh( mesh, warp_by=analysis.get_displacement(1), vertex_property=std[:, 2], property_cmap="magma", face_opacity=0.8, show_vertices=False, show_bar=True, ) plotter.set_title("Standard deviation of $U_Z$") plotter.show() .. image-sg:: /_gallery/workflows/images/sphx_glr_example_workflow_quality_control_002.png :alt: Standard deviation of $U_Z$ :srcset: /_gallery/workflows/images/sphx_glr_example_workflow_quality_control_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.477 seconds) .. _sphx_glr_download__gallery_workflows_example_workflow_quality_control.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_workflow_quality_control.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_workflow_quality_control.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_workflow_quality_control.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_