pysdic.display_distortion_field#

display_distortion_field(distortion, image_size, intrinsic=None, *, axis='n', reduction=4, contour_step=1.0, contour_bounds=None, mask=None, ax=None, show=True, intrinsic_kwargs=None, distortion_kwargs=None)[source]#

Display the displacement field generated by a distortion model on the image.

A regular grid of image points \(\vec{x}\) is built and the displacement of each point is computed once with pysdic.distort_points() (with P=intrinsic):

\[\Delta(\vec{x}) = \text{Intrinsic}(\text{Distortion}(\text{Intrinsic}^{-1}(\vec{x}))) - \vec{x}\]

One or several quantities are then displayed side by side: the displacement along one axis (\(\Delta_x\) or \(\Delta_y\)) and/or its magnitude \(\|\Delta\|\), with optional contour lines.

Note

If intrinsic is None, the distortion is directly applied to the image points, so the displacements are expressed in the coordinate system of the distortion (for example normalized coordinates). Adapt contour_step accordingly (for example 0.001).

See also

Parameters:
  • distortion (Transformation) – The distortion transformation (2D to 2D) to display.

  • image_size (Tuple[Integral, Integral]) – The size of the image (height, width) in pixels.

  • intrinsic (Optional[Transformation], optional (default: None)) – The intrinsic transformation (2D to 2D) of the camera. If None, no intrinsic is applied.

  • axis (Union[int, str, Sequence[Union[int, str]]], optional (default: "n")) – The displayed quantity: 0 or "x" for the horizontal displacement, 1 or "y" for the vertical displacement and "n" for the magnitude of the displacement. A list or tuple of these values (e.g. ("x", "y", "n")) displays several quantities side by side, in the given order. The same quantity cannot be given twice ("x" and 0 are the same).

  • reduction (Integral, optional (default: 4)) – The reduction factor of the grid: the field is evaluated approximately every reduction pixels.

  • contour_step (Union[Optional[Real], Sequence[Optional[Real]]], optional (default: 1.0)) – The distance between two contour levels, the levels are \(k \cdot \text{contour\_step}\) with \(k\) an integer. The colormap is discretized with the same levels. If None, no contours are drawn and the colormap is continuous. A single value is used for all the displayed quantities, a list/tuple gives one value per quantity of axis.

  • contour_bounds (Union[Optional[Tuple[Real, Real]], Sequence[Optional[Tuple[Real, Real]]]], optional (default: None)) – The range (min, max) of the contour levels and of the colormap. If None, the range of the field is used (symmetric around 0 for the displacements along one axis). Useful to keep the same colors on several figures (e.g. the frames of a video). A single pair is used for all the displayed quantities, a list/tuple gives one pair (or None) per quantity of axis.

  • mask (Optional[ArrayLike], optional (default: None)) – A boolean mask of shape (height, width) of the pixels of the image where the field is computed. The points of the reduced grid are associated with their nearest pixel: outside the mask, the field is not computed and set to NaN (not drawn).

  • ax (Optional[Union[matplotlib.axes.Axes, Sequence[matplotlib.axes.Axes]]], optional (default: None)) – The axes to draw on (they are cleared): one axes per quantity of axis, in the same order (a single axes is accepted for a single quantity). If None, a new figure with the quantities side by side is created.

  • show (bool, optional (default: True)) – Whether to show the figure (with matplotlib.pyplot.show).

  • intrinsic_kwargs (Optional[Dict[str, Any]], optional (default: None)) – Additional keyword arguments passed to the invert method of the intrinsic transformation.

  • distortion_kwargs (Optional[Dict[str, Any]], optional (default: None)) – Additional keyword arguments passed to the apply method of the distortion transformation.

Returns:

The axes containing the distortion fields, in the order of axis: a single axes if axis is a single quantity, a tuple of axes otherwise.

Return type:

Union[matplotlib.axes.Axes, Tuple[matplotlib.axes.Axes, …]]

Raises:
  • TypeError – If an input has a wrong type (e.g. a non-boolean mask).

  • ValueError – If an input has a wrong value or length, a quantity is given twice, the mask has not the shape of the image or selects no point of the grid, or a field contains only NaN.

Examples

from pysdic import display_distortion_field, PinholeIntrinsic, BrownDistortion

intrinsic = PinholeIntrinsic.from_matrix([[1000.0, 0.0, 320.0],
                                          [0.0, 1000.0, 240.0],
                                          [0.0, 0.0, 1.0]])
distortion = BrownDistortion([0.1, -0.05, 0.001, 0.002, 0.01])

# The three quantities side by side, with fixed bounds
ax_x, ax_y, ax_n = display_distortion_field(
    distortion, (480, 640), intrinsic,
    axis=("x", "y", "n"),
    contour_step=(2.0, 2.0, 1.0),
    contour_bounds=[(-20, 20), (-20, 20), (0, 20)],
)