pyfisheye.internal.optimisation package

Submodules

pyfisheye.internal.optimisation.linear module

exception pyfisheye.internal.optimisation.linear.OptimRuntimeError(msg=None)

Bases: RuntimeError

pyfisheye.internal.optimisation.linear.build_inv_lookup_table(intrinsics, image_radius, n_samples=10000)
Build a lookup table for the inverse polynomial f(theta) = rho. This can be used to backproject

points more quickly.

Parameters:
  • intrinsics (ndarray) – The polynomial coefficients for the model in ascending order.

  • image_radius (ndarray) – The image radius in pixels.

  • n_samples (int) – How many discrete samples should be included in the lookup table.

Return type:

tuple[ndarray, ndarray]

Returns:

A tuple containing the theta and rho values, sorted in ascending order by theta. Use np.interp to evaluate the lookup table.

pyfisheye.internal.optimisation.linear.intrinsics_and_z_translation(pattern_observations, pattern_world_coords, distortion_centre, extrinsics, image_radius, monotonic=False, num_rho_samples=250)
Parameters:
  • pattern_observations (ndarray) – Pixel coordinate pattern observations.

  • pattern_world_coords (ndarray) – Pattern corner points in the pattern coordinate system (Z=0) for a single pattern.

  • distortion_centre (ndarray) – x-y distortion centre.

  • extrinsics (ndarray) – 4 extrinsic solutions per observation.

  • image_radius (float) – The image radius in pixels.

  • monotonic (bool) – If False, then the solution will be computed using least squares via pseudoinverse. Otherwise, a solver supporting inequality constraints will be used instead to enforce monotonicty, i.e. f’(rho) >= 0 for all rho.

  • num_rho_samples (int) – Matters only when monotonic=True. Determines the number of samples used to generate the constraint matrix. This can have a signficiant impact on the convergence time.

Return type:

tuple[ndarray, ndarray]

Returns:

The intrinsic parameters (5,) in ascending order of power followed by the z-translation for each observation.

pyfisheye.internal.optimisation.linear.linear_refinement_extrinsics(pattern_observations, pattern_world_coords, distortion_centre, extrinsics, intrinsics, image_radius)
Solves all linear equations simultanesouly using the estimated intrinsic parameters to

refine the extrinsic parameters.

Parameters:
  • distortion_centre (ndarray) – x-y distortion centre.

  • pattern_observations (ndarray) – The pattern coordinates in image space centred around the initial centre of distortion (middle of the image). Size should be (N, M, 2) where N is the number of observations of the pattern and M is the total number of corners in the calibration pattern stored in row-major order.

  • extrinsics (ndarray) – The (N, 3, 3) extrinsics transformation matrix containing the first two columns of the rotation matrix and the translation vector.

  • intrinsics (ndarray) – An array of 5 polynomial coefficients in ascending order of power.

  • image_radius (float) – The image radius in pixels.

Return type:

ndarray

Returns:

The refined extrinsics with the same shape as the input extrinsics.

pyfisheye.internal.optimisation.linear.linear_refinement_intrinsics(pattern_observations, pattern_world_coords, distortion_centre, extrinsics, image_radius)
Uses the refined extrinsics from linear_refinement_extrinsics

to solve a linear system of equations and thus improve the current estimate for the intrinsic parameters.

Parameters:
  • pattern_observations (ndarray) – The pattern coordinates in image space centred around the initial centre of distortion (middle of the image). Size should be (N, M, 2) where N is the number of observations of the pattern and M is the total number of corners in the calibration pattern stored in row-major order.

  • pattern_world_coords (ndarray) – The pattern coordinates in world space with z = 0.

  • distortion_centre (ndarray) – The distortion centre x-y in pixels.

  • extrinsics (ndarray) – The (N, 3, 3) extrinsics transformation matrix computed by partial_extrinsics() which has all z-translation componens set to np.nan. This will be modified in-place such that z-components are set to the linear estimate.

Return type:

ndarray

Returns:

The refined intrinsics with the same shape as the input intrinsics.

pyfisheye.internal.optimisation.linear.partial_extrinsics(pattern_observations, pattern_world_coords, distortion_centre)
Computes four possible extrinsic configurations for each pattern observation by solving

a system of linear homogenous equations.

Parameters:
  • pattern_observations (ndarray) – Pixel coordinate pattern observations.

  • pattern_world_coords (ndarray) – Pattern corner points in the pattern coordinate system (Z=0) for a single pattern.

  • distortion_centre (ndarray) – x-y distortion centre.

Return type:

ndarray

Returns:

Extrinsic configuration as a N,4,3,3 array where the first 2 column are the first 2 column vectors of the rotation matrix and the last column is the translation vector with the z-component set to NaN. The dimension with length 4 corresponds to the 4 possible solutions.

pyfisheye.internal.optimisation.linear.select_best_extrinsic_solution(pattern_observations, pattern_world_coords, distortion_centre, extrinsics, image_radius)
Selects the extrinsic solution in the same quadrant as its corresponding observation

and resulting in a polynomial fit which tends to positive infinity.

Parameters:
  • pattern_observations (ndarray) – Pixel coordinate pattern observations.

  • pattern_world_coords (ndarray) – Pattern corner points in the pattern coordinate system (Z=0) for a single pattern.

  • distortion_centre (ndarray) – x-y distortion centre.

  • extrinsics (ndarray) – 4 extrinsic solutions per observation.

  • image_radius (float) – The image radius in pixels.

Return type:

ndarray

Returns:

N,3,3 array containing the best solution for each observation.

pyfisheye.internal.optimisation.nonlinear module

pyfisheye.internal.optimisation.nonlinear.nonlinear_refinement(pattern_observations, pattern_world_coords, distortion_centre, extrinsics, intrinsics, stretch_matrix, wnls_threshold)
Use LM to perform a nonlinear refinement of all calibration parameters at once.

Use Huber’s function for robustness to outliers.

Parameters:
  • pattern_observations (ndarray)

  • pattern_world_coords (ndarray)

  • distortion_centre (ndarray)

  • extrinsics (ndarray)

  • intrinsics (ndarray)

  • stretch_matrix (ndarray)

Return type:

OptimResult

Returns:

Optimisation result.

pyfisheye.internal.optimisation.optim_result module

class pyfisheye.internal.optimisation.optim_result.OptimResult(extrinsics: ndarray, intrinsics: ndarray, dist_centre: ndarray, scaling_mat: ndarray)

Bases: NamedTuple

Result returned by nonlinear refinement.

dist_centre: ndarray

Alias for field number 2

extrinsics: ndarray

Alias for field number 0

intrinsics: ndarray

Alias for field number 1

scaling_mat: ndarray

Alias for field number 3

Module contents

pyfisheye.internal.optimisation.intrinsics_and_z_translation(pattern_observations, pattern_world_coords, distortion_centre, extrinsics, image_radius, monotonic=False, num_rho_samples=250)
Parameters:
  • pattern_observations (ndarray) – Pixel coordinate pattern observations.

  • pattern_world_coords (ndarray) – Pattern corner points in the pattern coordinate system (Z=0) for a single pattern.

  • distortion_centre (ndarray) – x-y distortion centre.

  • extrinsics (ndarray) – 4 extrinsic solutions per observation.

  • image_radius (float) – The image radius in pixels.

  • monotonic (bool) – If False, then the solution will be computed using least squares via pseudoinverse. Otherwise, a solver supporting inequality constraints will be used instead to enforce monotonicty, i.e. f’(rho) >= 0 for all rho.

  • num_rho_samples (int) – Matters only when monotonic=True. Determines the number of samples used to generate the constraint matrix. This can have a signficiant impact on the convergence time.

Return type:

tuple[ndarray, ndarray]

Returns:

The intrinsic parameters (5,) in ascending order of power followed by the z-translation for each observation.

pyfisheye.internal.optimisation.linear_refinement_extrinsics(pattern_observations, pattern_world_coords, distortion_centre, extrinsics, intrinsics, image_radius)
Solves all linear equations simultanesouly using the estimated intrinsic parameters to

refine the extrinsic parameters.

Parameters:
  • distortion_centre (ndarray) – x-y distortion centre.

  • pattern_observations (ndarray) – The pattern coordinates in image space centred around the initial centre of distortion (middle of the image). Size should be (N, M, 2) where N is the number of observations of the pattern and M is the total number of corners in the calibration pattern stored in row-major order.

  • extrinsics (ndarray) – The (N, 3, 3) extrinsics transformation matrix containing the first two columns of the rotation matrix and the translation vector.

  • intrinsics (ndarray) – An array of 5 polynomial coefficients in ascending order of power.

  • image_radius (float) – The image radius in pixels.

Return type:

ndarray

Returns:

The refined extrinsics with the same shape as the input extrinsics.

pyfisheye.internal.optimisation.linear_refinement_intrinsics(pattern_observations, pattern_world_coords, distortion_centre, extrinsics, image_radius)
Uses the refined extrinsics from linear_refinement_extrinsics

to solve a linear system of equations and thus improve the current estimate for the intrinsic parameters.

Parameters:
  • pattern_observations (ndarray) – The pattern coordinates in image space centred around the initial centre of distortion (middle of the image). Size should be (N, M, 2) where N is the number of observations of the pattern and M is the total number of corners in the calibration pattern stored in row-major order.

  • pattern_world_coords (ndarray) – The pattern coordinates in world space with z = 0.

  • distortion_centre (ndarray) – The distortion centre x-y in pixels.

  • extrinsics (ndarray) – The (N, 3, 3) extrinsics transformation matrix computed by partial_extrinsics() which has all z-translation componens set to np.nan. This will be modified in-place such that z-components are set to the linear estimate.

Return type:

ndarray

Returns:

The refined intrinsics with the same shape as the input intrinsics.

pyfisheye.internal.optimisation.nonlinear_refinement(pattern_observations, pattern_world_coords, distortion_centre, extrinsics, intrinsics, stretch_matrix, wnls_threshold)
Use LM to perform a nonlinear refinement of all calibration parameters at once.

Use Huber’s function for robustness to outliers.

Parameters:
  • pattern_observations (ndarray)

  • pattern_world_coords (ndarray)

  • distortion_centre (ndarray)

  • extrinsics (ndarray)

  • intrinsics (ndarray)

  • stretch_matrix (ndarray)

Return type:

OptimResult

Returns:

Optimisation result.

pyfisheye.internal.optimisation.partial_extrinsics(pattern_observations, pattern_world_coords, distortion_centre)
Computes four possible extrinsic configurations for each pattern observation by solving

a system of linear homogenous equations.

Parameters:
  • pattern_observations (ndarray) – Pixel coordinate pattern observations.

  • pattern_world_coords (ndarray) – Pattern corner points in the pattern coordinate system (Z=0) for a single pattern.

  • distortion_centre (ndarray) – x-y distortion centre.

Return type:

ndarray

Returns:

Extrinsic configuration as a N,4,3,3 array where the first 2 column are the first 2 column vectors of the rotation matrix and the last column is the translation vector with the z-component set to NaN. The dimension with length 4 corresponds to the 4 possible solutions.

pyfisheye.internal.optimisation.select_best_extrinsic_solution(pattern_observations, pattern_world_coords, distortion_centre, extrinsics, image_radius)
Selects the extrinsic solution in the same quadrant as its corresponding observation

and resulting in a polynomial fit which tends to positive infinity.

Parameters:
  • pattern_observations (ndarray) – Pixel coordinate pattern observations.

  • pattern_world_coords (ndarray) – Pattern corner points in the pattern coordinate system (Z=0) for a single pattern.

  • distortion_centre (ndarray) – x-y distortion centre.

  • extrinsics (ndarray) – 4 extrinsic solutions per observation.

  • image_radius (float) – The image radius in pixels.

Return type:

ndarray

Returns:

N,3,3 array containing the best solution for each observation.