pysdic.workflows.compute_optical_flow_displacement#

compute_optical_flow_displacement(analysis, ref_frame, ref_view, def_frame, def_views, *, disflow_config=None, displacement_estimate=None, margin=None, channel=0, distortion_kwargs=None)[source]#

Estimate the displacement of the vertices of the mesh at a deformed frame by optical flow and triangulation, for example to initialize a pysdic.SDICCorrelationTerm.

The vertices are followed from one reference view to \(K \geq 2\) views of the deformed frame, then triangulated:

  1. The mesh displaced by \(U_{ref}\) (displacement of ref_frame) is projected in the reference view: image points \(x_{ref}\).

  2. The optical flow from the reference view to the first deformed view def_views[0] (usually the same camera, a temporal motion) gives the image points \(x_{def,0}\).

  3. The optical flow from the first deformed view to each other deformed view def_views[k] (usually other cameras at the same frame, a stereo motion) gives the image points \(x_{def,k}\).

  4. The rays of the \(K\) deformed views through these image points are intersected (pysdic.intersect_rays(), least squares point of the \(K\) rays): 3D positions \(X_{def}\), and \(U_{def} = X_{def} - X\).

Each optical flow is computed on the ROI of the source image points (widened by margin), and the flow is interpolated bilinearly at the points.

Affine alignment. For each optical flow between the views 1 and 2, an affine map \(x \mapsto A x + b\) is fitted by least squares between the image points of the vertices in the view 1 and their estimated image points in the view 2 (projection of the mesh displaced by displacement_estimate): the mean motion of the region at first order (rotation, scale, shear and translation of the change of point of view and of the estimated displacement). The image of the view 2 is aligned on the view 1, \(\tilde{I}_2(x) = I_2(A x + b)\), with the interpolation function of its pysdic.Image (0 outside its ROI); the optical flow \(F\) is computed between \(I_1\) and \(\tilde{I}_2\), and \(x_2 = A (x_1 + F(x_1)) + b\). The alignment is applied to the image, not given as an initial flow, because the patches of the DIS optical flow only translate: a rotation of a few degrees between two cameras makes large patches fail.

Important

The optical flow only corrects a small motion around the affine alignment (a few pixels to a few tens of pixels, depending on the texture and on disflow_config). For a large displacement, give a displacement_estimate (e.g. the result of the previous frame).

A vertex gets a NaN displacement if it is lost in one of the views (not finite, behind a camera or outside an image, flow not defined) or if the triangulated point is behind one of the deformed cameras. Replace these values before pysdic.SDICAnalysis.set_displacement(), which needs finite displacements (see the example).

Important

The occlusions are not detected: a vertex hidden by another part of the surface gets the flow of the visible surface. Check the ray_distances of the result, or use pysdic.compute_visibility().

Note

The source images of the optical flows are the full image data (view.image.image), the aligned images are interpolated in the ROI of their pysdic.Image. The images must be of unsigned integer type (see pysdic.compute_optical_flow()).

Parameters:
  • analysis (pysdic.SDICAnalysis) – The analysis, with a 3D mesh (\(E = 3\)) and the displacement of ref_frame set.

  • ref_frame (Integral) – The reference frame. Its displacement \(U_{ref}\) must be set (see pysdic.SDICAnalysis.set_zero_displacement()).

  • ref_view (Integral) – The index of the reference view in ref_frame.

  • def_frame (Integral) – The deformed frame, different from ref_frame.

  • def_views (Sequence[Integral]) – The indices of at least two distinct views of def_frame used for the triangulation. The flow goes from the reference view to def_views[0], then from def_views[0] to each other view.

  • disflow_config (Optional[pysdic.DisflowConfig], optional) – The parameters of the optical flow. By default, DisflowConfig().

  • displacement_estimate (Optional[ArrayLike], optional) – An estimation of the displacement at def_frame, of shape \((N_v, 3)\), only used for the affine alignments. NaN values are accepted (these vertices are not used for the alignments), e.g. the displacement of a previous result. By default, \(U_{ref}\): the surface has not moved since the reference frame (not a zero displacement, which would bring the surface back to the coordinates of the mesh).

  • margin (Optional[Integral], optional) – The number of pixels added around the image points to define the ROI of the optical flows (non-negative). By default, the patch_size of the configuration, so that the patches of the border contain the points.

  • channel (Integral, optional (default: 0)) – The channel of the images used for the optical flow (images with several channels).

  • distortion_kwargs (Optional[Dict[str, Any]], optional) – The options of the inversion of the distortion for the rays (see pysdic.compute_rays()).

Returns:

The estimated displacement, the distances to the rays and the image points of the vertices in the views.

Return type:

pysdic.workflows.OpticalFlowDisplacementResult

Raises:
  • TypeError – If an input has a wrong type, or an image is not of unsigned integer type.

  • ValueError – If the mesh is not 3D, def_frame is ref_frame, def_views does not contain at least two distinct views, the images of an optical flow have different shapes, or less than 3 vertices can be used for an affine alignment.

  • IndexError – If a frame or a view index is out of range.

Examples

Initialize the frame 1 of a stereo analysis, the vertices not measured keeping the displacement of the reference frame:

from pysdic import DisflowConfig
from pysdic.workflows import compute_optical_flow_displacement

analysis.set_zero_displacement(0)
result = compute_optical_flow_displacement(
    analysis, ref_frame=0, ref_view=0, def_frame=1, def_views=(0, 1),
    disflow_config=DisflowConfig(patch_size=30, patch_stride=6),
)
print(f"{result.measured.sum()} vertices measured, max distance {numpy.nanmax(result.ray_distances):.2e}")
analysis.set_displacement(1, numpy.where(result.measured[:, None], result.displacement, analysis.get_displacement(0)))