pysdic.DisflowConfig#

class DisflowConfig(finest_scale=0, patch_size=50, patch_stride=10, gradient_descent_iterations=500, use_spatial_propagation=True, use_mean_normalization=True, variational_refinement_iterations=10, variational_refinement_alpha=1.0, variational_refinement_delta=1.0, variational_refinement_gamma=1.0, variational_refinement_epsilon=0.02)[source]#

Parameters of the Dense Inverse Search (DIS) optical flow of OpenCV (cv2.DISOpticalFlow).

The DIS algorithm estimates the flow from coarse to fine on a Gaussian pyramid of the images. At each scale:

  1. Patch inverse search: the image is covered by square patches of patch_size pixels, spaced by patch_stride pixels. The displacement of each patch is found by an inverse compositional gradient descent (gradient_descent_iterations), optionally improved by the displacements of the neighbor patches (use_spatial_propagation).

  2. Densification: the dense flow at each pixel is a weighted average of the displacements of the patches that contain it.

  3. Variational refinement: the dense flow is smoothed by minimizing an energy made of a data term (variational_refinement_delta), a gradient term (variational_refinement_gamma) and a smoothness term (variational_refinement_alpha), with variational_refinement_iterations fixed point iterations.

The configuration is immutable and its values are validated at creation. Use dataclasses.replace() to derive a new configuration:

from dataclasses import replace
from pysdic import DisflowConfig

config = DisflowConfig(patch_size=20, patch_stride=5)
smooth_config = replace(config, variational_refinement_alpha=5.0)

Note

The default values are the ones of pysdic.compute_optical_flow(). They differ from the OpenCV presets: the patches are larger (50 pixels instead of 8) and the gradient descent longer (500 iterations), which suits the speckle patterns of DIC better than natural images.

Parameters:
  • finest_scale (Integral, optional (default: 0)) – Level of the Gaussian pyramid on which the computation stops. 0 is the original resolution, 1 half the resolution, etc. For a value \(s > 0\) the flow is computed on images downsampled by \(2^s\) and then upscaled bilinearly: faster but less accurate. Must be non-negative.

  • patch_size (Integral, optional (default: 50)) – Size (in pixels) of the square patches matched between the images, at each scale. Larger patches are more robust (more texture in each patch) but blur the flow where it varies quickly. The patches must fit in the images at the finest scale (OpenCV raises an error otherwise). Must be at least 1.

  • patch_stride (Integral, optional (default: 10)) – Distance (in pixels) between the corners of two neighbor patches. A smaller stride gives more overlapping patches: a better flow but a slower computation. Must be between 1 and patch_size (a larger stride leaves pixels outside every patch and crashes OpenCV).

  • gradient_descent_iterations (Integral, optional (default: 500)) – Maximum number of iterations of the gradient descent that finds the displacement of each patch. More iterations can improve the flow for large or slow-converging displacements. 0 disables the search (the patches keep their initial displacements). Must be non-negative.

  • use_spatial_propagation (bool, optional (default: True)) – If True, the displacement of a patch is replaced by the one of a neighbor patch when it matches better. This helps to recover from the errors of the coarse scales; disabling it can give a slightly smoother flow.

  • use_mean_normalization (bool, optional (default: True)) – If True, the mean gray level of each patch is subtracted before comparing the patches, which makes the matching robust to illumination changes between the images. Disable it only if the illumination is constant.

  • variational_refinement_iterations (Integral, optional (default: 10)) – Number of fixed point iterations of the variational refinement at each scale. 0 disables the variational refinement. More iterations give a smoother flow. Must be non-negative.

  • variational_refinement_alpha (Real, optional (default: 1.0)) – Weight of the smoothness term of the variational refinement: a larger value gives a smoother flow. Must be finite and non-negative (a negative weight makes the refinement diverge).

  • variational_refinement_delta (Real, optional (default: 1.0)) – Weight of the data term (gray level constancy \(I_2(x + F) = I_1(x)\)) of the variational refinement. Must be finite and non-negative.

  • variational_refinement_gamma (Real, optional (default: 1.0)) – Weight of the gradient term (gradient constancy \(\nabla I_2(x + F) = \nabla I_1(x)\)) of the variational refinement, which is robust to illumination changes. Must be finite and non-negative.

  • variational_refinement_epsilon (Real, optional (default: 0.02)) – Shift \(\varepsilon\) of the robust (Charbonnier) penalty \(\sqrt{r^2 + \varepsilon^2}\) used by the variational refinement: a smaller value is more robust to outliers but less smooth. Must be finite and strictly positive (0 crashes OpenCV).

Raises:
  • TypeError – If a parameter has a wrong type (booleans are rejected for the numbers).

  • ValueError – If a parameter has a wrong value.

create_dis_optical_flow()[source]#

Create a cv2.DISOpticalFlow object with this configuration.

Returns:

A new OpenCV DIS optical flow object, ready for calc.

Return type:

cv2.DISOpticalFlow

Raises:

ImportError – If cv2 (opencv-python) is not installed.