pycvcam.optimize_input_points#
- optimize_input_points(transform, output_points, guess=None, *, transpose=False, max_iter=10, eps=1e-08, verbose=False, _skip=False)[source]#
Optimize the input points of the transformation using the given output points.
Deprecated since version 2.1.8: Consider using the
optimize_input_points_gn()function instead, which provides a more robust optimization process for handling non-linear problems, and allows for more flexible stopping criteria.Estimate the optimized input points of the transformation such that the transformed input points match the output points.
Warning
This method can only be used if the dimensions are the same, i.e. input_dim == output_dim.
Lets consider a set of output points \(X_O\) with shape (…, dim) and a set of input points \(\vec{X}_I\) with shape (…, input_dim). We search \(\vec{X}_I = \vec{X}_{I_0} + \delta \vec{X}_I\) such that:
\[\vec{X}_O = \text{Transform}(\vec{X}_I, \lambda) = T(\vec{X}_{I_0} + \delta \vec{X}_I, \lambda)\]We have:
\[\nabla_{X} T (\vec{X}_{I_0}, \lambda) \delta \vec{X}_I = \vec{X}_O - T(\vec{X}_{I_0}, \lambda)\]The corrections are computed using the following equations :
\[J \delta \vec{X}_I = R\]Where \(J = \nabla_{X} T (\vec{X}_{I_0}, \lambda)\) is the Jacobian matrix of the transformation with respect to the input points, and \(R = \vec{X}_O - T(\vec{X}_{I_0}, \lambda)\) is the residual vector.
\(\vec{X}_{I_0}\) is the initial guess for the input points, if None, it use the output points as the initial guess.
Note
The
_skipparameter is used to skip the checks for the transformation parameters and assume the output points are given in the (n_points, dim) float format. Please use this parameter with caution, as it may lead to unexpected results if the transformation parameters are not set correctly.Warning
The points are converting to float before applying the inverse transformation. See
pycvcam.core.Packagefor more details on the default data types used in the package.- Parameters:
transform (Transform) – The transformation object to optimize.
output_points (numpy.ndarray) – The output points to be matched. Shape (…, dim) (or (dim, …) if transpose is True).
guess (Optional[numpy.ndarray], optional) – The initial guess for the input points of the transformation with shape (…, dim). If None, the output points are used as the initial guess. Default is None.
transpose (bool, optional) – If True, the output points are transposed to shape (dim, …). Default is False.
max_iter (int, optional) – The maximum number of iterations for the optimization. Default is 10.
eps (float, optional) – The convergence threshold for the optimization. Default is 1e-8.
verbose (bool, optional) – If True, print the optimization progress and diagnostics. Default is False.
_skip (bool, optional) – If True, skip the checks for the transformation parameters and assume the output points are given in the (n_points, dim) float format. The guess must be given in the (n_points, dim) float format. transpose is ignored if this parameter is set to True.
- Returns:
The optimized input points of the transformation with shape (…, dim).
- Return type:
- Raises:
ValueError – If the output points do not have the expected shape, or if the input and output dimensions do not match the transformation’s input and output dimensions.
TypeError – If the output points or guess are not numpy arrays, or if the guess is not a numpy array.
Examples
Lets assume, we want to optimize the input points of a Cv2Distortion object to match a set of distorted points:
import numpy from pycvcam import Cv2Distortion from pycvcam.optimize import optimize_input_points # Create a Cv2Distortion object with known parameters distortion = Cv2Distortion(parameters=numpy.array([1e-3, 2e-3, 1e-3, 1e-4, 2e-3]), n_params=5) # Generate some random distorted points distorted_points = numpy.random.rand(10, 2) # Random 2D points # Optimize the input points to match the distorted points optimized_input_points = optimize_input_points(distortion, distorted_points) # shape (10, 2) print("Optimized Input Points:", optimized_input_points)