pysdic.build_chains_transformation_input_points_term#

build_chains_transformation_input_points_term(seq_transformations, seq_chains, seq_outputs, guess, *, seq_kwargs=None, mode='rJ', weight=1.0, loss=None, loss_scale=1.0, use_cache=False)[source]#

Build a pysolvegn.BatchTerm to optimize the input points \(X\) such that their images by several chains of transformations match the given output points (for example triangulation of points seen by several cameras).

Lets \((T_0, T_1, ..., T_{N_T-1})\) be a sequence of transformations, and \((C_0, C_1, ..., C_{N_C-1})\) a sequence of chains of indices of these transformations, applied in this order:

C_0 = (1, 4, 8) -----> C_0(X) = T_8 ∘ T_4 ∘ T_1(X)

All the chains share the same input points \(X\) and each chain \(C_c\) has its own output points \(Y_c\). The parameters of the transformations are fixed, so each point \(k\) is an independent problem of the batch:

\[\min_{\vec{X}_k} \frac{1}{2} \sum_c \sum_j \rho\left( \left( C_c(\vec{X}_k) - \vec{Y}_{c,k} \right)_j^2 \right)\]

The problems are solved together with pysolvegn.solve_batch: each point is removed from the processing as soon as it satisfies a stopping criterion.

The term can be built in two modes:

  • "rJ": residuals and Jacobians of each point. Robust losses ("soft_l1", "cauchy", "huber", …) are available, with a soft threshold loss_scale in the unit of the output points.

  • "gH": gradients \(J_k^T \vec{r}_k\) and Hessians \(J_k^T J_k\) of each point, with its cost (only the "linear" loss).

Note

The term works on copies of the transformations: the given transformations are never modified.

Warning

The dimension \(n_i\) of the input points must be lower than or equal to the total dimension of the outputs \(\sum_c n_{o,c}\), otherwise the problems are under-determined.

See also

Parameters:
  • seq_transformations (Sequence[Transformation]) – The transformations \((T_0, T_1, ..., T_{N_T-1})\). Their parameters must be set.

  • seq_chains (Sequence[Sequence[Integral]]) – The chains, each chain being a non-empty sequence of indices of seq_transformations, in the order of application. The first transformation of each chain must have the dimension \(n_i\) of the input points.

  • seq_outputs (Sequence[ArrayLike]) – The output points \(Y_c\) of each chain with shape \((\dots, n_{o,c})\), with the same batch shape as guess.

  • guess (ArrayLike) – The initial guess of the input points with shape \((\dots, n_i)\).

  • seq_kwargs (Optional[Sequence[Optional[Dict[str, Any]]]], optional (default: None)) –

    Additional keyword arguments passed to the apply method of each transformation: one dictionary (or None) per transformation of seq_transformations.

    Warning

    The keyword arguments must not depend on the points, since the solver evaluates only the points still in processing.

  • mode (str, optional (default: "rJ")) – The mode of the term, "rJ" or "gH".

  • weight (Union[Real, ArrayLike], optional (default: 1.0)) – The weight of the term: a scalar for all the points, or one weight per point with the batch shape \((\dots)\) of guess.

  • loss (Optional[Union[str, Callable]], optional (default: None)) – The loss function of the term (see pysolvegn.BatchTerm), only for the mode "rJ". None means "linear".

  • loss_scale (Real, optional (default: 1.0)) – The soft threshold \(C\) of the loss function in the unit of the output points (for example in pixels). Only for the mode "rJ", no effect on the "linear" loss.

  • use_cache (bool, optional (default: False)) –

    The evaluation strategy of the functions of the term:

    • False: each function computes only what it returns and nothing is kept in memory. The residuals ("rJ") and the costs ("gH") are computed without the Jacobians, which is efficient when many evaluated points are trial points whose Jacobians are never required (damped steps of Levenberg-Marquardt). In mode "gH", the gradients and the Hessians each compute the Jacobians.

    • True: the first call for given points computes all the operators of the mode (residuals and Jacobians in "rJ", gradients, Hessians and costs in "gH") and keeps them in cache, so the other calls for the same points are free. It is efficient when the Jacobians are required at every evaluated point (Gauss-Newton without damping).

Returns:

  • pysolvegn.BatchTerm – The term to give to pysolvegn.solve_batch.

  • numpy.ndarray – The initial input points with shape \((k, n_i)\), to use as p0, where \(k\) is the number of points (the batch dimensions of guess are flattened).

Return type:

Tuple[BatchTerm, ndarray]

Examples

Triangulate 3D points seen by two calibrated cameras:

seq_transformations = [extrinsic_0, extrinsic_1, distortion, intrinsic]
seq_chains = [(0, 2, 3), (1, 2, 3)]

term, p0 = build_chains_transformation_input_points_term(
    seq_transformations, seq_chains, [image_points_0, image_points_1], guess
)
result = pysolvegn.solve_batch(term, p0, max_iteration=20, xtol=1e-10)
world_points = result.parameters.reshape(guess.shape)