.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "../../docs/source/_gallery/study_optimization.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_.._.._docs_source__gallery_study_optimization.py: .. _sphx_glr__gallery_study_optimization.py: Study of camera calibration optimization ======================================== This example shows how to use :func:`pysolvegn.study_optimization` to study the Gauss-Newton Hessian of a camera calibration problem. We consider a simplified camera calibration problem in which the camera intrinsic parameters and the radial/tangential distortion parameters are estimated from synthetic image observations. The complete camera parameter vector is: ``[fx, fy, cx, cy, k1, k2, p1, p2, k3]`` Unlike the affine parametrization example, all nine parameters are independent optimization variables. No parametrization is used. The purpose of this example is therefore to study the observability, conditioning, covariance, and weak parameter combinations of the complete nine-dimensional camera calibration problem at a given initial point. No optimization step is performed. ``study_optimization`` evaluates the initial state by internally calling ``solve`` with ``max_iteration=0``. .. GENERATED FROM PYTHON SOURCE LINES 32-68 Camera model ------------ We use the Brown-Conrady camera distortion model. For normalized coordinates ``(x, y)``, the distorted coordinates are: .. math:: x_d = x(1 + k_1 r^2 + k_2 r^4 + k_3 r^6) + 2p_1xy + p_2(r^2 + 2x^2) .. math:: y_d = y(1 + k_1 r^2 + k_2 r^4 + k_3 r^6) + p_1(r^2 + 2y^2) + 2p_2xy where: .. math:: r^2 = x^2 + y^2. The pixel coordinates are: .. math:: u = f_x x_d + c_x .. math:: v = f_y y_d + c_y .. GENERATED FROM PYTHON SOURCE LINES 68-114 .. code-block:: Python import numpy as np import pysolvegn np.random.seed(0) def camera_model(params, points): """ Compute distorted image coordinates. Parameters ---------- params : array-like Complete camera parameter vector: [fx, fy, cx, cy, k1, k2, p1, p2, k3] points : ndarray Normalized image points with shape ``(n_points, 2)``. Returns ------- ndarray Image coordinates with shape ``(n_points, 2)``. """ fx, fy, cx, cy, k1, k2, p1, p2, k3 = params x = points[:, 0] y = points[:, 1] r2 = x**2 + y**2 radial = 1.0 + k1 * r2 + k2 * r2**2 + k3 * r2**3 x_distorted = x * radial + 2.0 * p1 * x * y + p2 * (r2 + 2.0 * x**2) y_distorted = y * radial + p1 * (r2 + 2.0 * y**2) + 2.0 * p2 * x * y u = fx * x_distorted + cx v = fy * y_distorted + cy return np.column_stack((u, v)) .. GENERATED FROM PYTHON SOURCE LINES 115-122 Generate synthetic calibration observations -------------------------------------------- We use the same synthetic camera parameters as in the affine parametrization example. Here, however, all nine parameters are considered independent. .. GENERATED FROM PYTHON SOURCE LINES 122-178 .. code-block:: Python true_fx = 850.0 true_fy = 850.0 true_cx = 640.0 true_cy = 480.0 true_k1 = -0.20 true_k2 = 0.05 true_p1 = 0.001 true_p2 = -0.002 true_k3 = -0.01 true_parameters = np.array( [ true_fx, true_fy, true_cx, true_cy, true_k1, true_k2, true_p1, true_p2, true_k3, ] ) # Generate normalized calibration points. x = np.linspace(-0.7, 0.7, 15) y = np.linspace(-0.5, 0.5, 11) xx, yy = np.meshgrid(x, y) normalized_points = np.column_stack( ( xx.ravel(), yy.ravel(), ) ) image_points_true = camera_model( true_parameters, normalized_points, ) # Add measurement noise. image_points = image_points_true + np.random.normal( scale=0.5, size=image_points_true.shape, ) .. GENERATED FROM PYTHON SOURCE LINES 179-186 Define the residual ------------------- The residual function directly receives the complete nine-dimensional parameter vector. There is no parametrization between the solver and the camera model. .. GENERATED FROM PYTHON SOURCE LINES 186-205 .. code-block:: Python def residual_function(params): predicted_points = camera_model( params, normalized_points, ) return (predicted_points - image_points).ravel() data_term = pysolvegn.Term.from_rJ( residual_func=residual_function, loss="linear", weight=1.0, finite_difference="central", ) .. GENERATED FROM PYTHON SOURCE LINES 206-212 Define the initial parameters ----------------------------- All nine camera parameters are independent optimization variables: ``[fx, fy, cx, cy, k1, k2, p1, p2, k3]`` .. GENERATED FROM PYTHON SOURCE LINES 212-229 .. code-block:: Python initial_parameters = np.array( [ 800.0, # fx 800.0, # fy 640.0, # cx 480.0, # cy 0.0, # k1 0.0, # k2 0.0, # p1 0.0, # p2 0.0, # k3 ] ) .. GENERATED FROM PYTHON SOURCE LINES 230-244 Study the optimization problem ------------------------------ ``study_optimization`` evaluates the least-squares problem at the initial parameter vector. It internally calls ``solve`` with: ``max_iteration=0`` Consequently, no optimization step is performed. The Gauss-Newton Hessian is computed directly in the complete nine-dimensional parameter space. .. GENERATED FROM PYTHON SOURCE LINES 244-253 .. code-block:: Python pysolvegn.study_optimization( terms=[data_term], p0=initial_parameters, title="Camera calibration at initialization - complete parameter vector", ) .. rst-class:: sphx-glr-script-out .. code-block:: none ====================================================================== Camera calibration at initialization - complete parameter vector ---------------------------------------------------------------------- Solver state ------------ The following quantities are taken directly from the current Gauss-Newton solver. Number of terms : 1 Number of equations : 330 Number of parameters : 9 Total cost : 1.819651e+04 ||g||_inf : 3.530980e+05 Term information ---------------- Term Type Equations Weight Cost 0 rJ 330 1.000000e+00 1.819651e+04 Hessian analysis ---------------- The Hessian below is the exact Gauss-Newton system built by the solver. It is expressed in the input parameter space. Largest singular value : 8.084605e+07 Smallest singular value : 3.933677e-01 Condition number : 2.055229e+08 Trace(H) : 1.326619e+08 Singular values of H -------------------- Index λ² 1/λ² 0 8.084605e+07 1.236919e-08 1 4.321302e+07 2.314117e-08 2 8.413086e+06 1.188624e-07 3 1.878292e+05 5.323985e-06 4 1.735561e+03 5.761826e-04 5 7.111897e+01 1.406095e-02 6 6.950117e+01 1.438825e-02 7 2.032913e+01 4.919049e-02 8 3.933677e-01 2.542151e+00 Parameter sensitivity --------------------- Degrees of freedom : 321 Estimated residual variance σ² : 1.133739e+02 Parameter Value Variance Std Std / |Value| 0 8.000000e+02 1.459932e+02 1.208277e+01 1.510346e-02 1 8.000000e+02 1.476336e+02 1.215046e+01 1.518807e-02 2 6.400000e+02 1.594142e+00 1.262593e+00 1.972802e-03 3 4.800000e+02 1.631248e+00 1.277203e+00 2.660840e-03 4 0.000000e+00 1.807013e-02 1.344252e-01 inf 5 0.000000e+00 1.286937e-01 3.587390e-01 inf 6 0.000000e+00 6.228596e-06 2.495716e-03 inf 7 0.000000e+00 3.253514e-06 1.803750e-03 inf 8 0.000000e+00 8.280042e-02 2.877506e-01 inf SVD parameter-space directions ------------------------------ The right singular vectors of H describe directions in parameter space. Large singular values correspond to strongly observable directions, while small singular values correspond to weakly observable directions. Best observable directions -------------------------- Mode 1 (λ² = 8.084605e+07) p[0] : -3.848789e-15 p[1] : -8.401314e-17 p[2] : -1.077605e-03 p[3] : -5.092241e-16 p[4] : -1.364810e-12 p[5] : -1.142972e-12 p[6] : -1.445313e-17 p[7] : -9.999994e-01 p[8] : +1.972964e-12 Mode 2 (λ² = 4.321302e+07) p[0] : +6.021964e-16 p[1] : +1.946712e-15 p[2] : -1.501405e-16 p[3] : +1.486593e-03 p[4] : +1.676481e-12 p[5] : +1.460052e-12 p[6] : +9.999989e-01 p[7] : -1.093396e-17 p[8] : +1.055423e-13 Mode 3 (λ² = 8.413086e+06) p[0] : +1.459622e-03 p[1] : +6.404659e-04 p[2] : +2.262723e-14 p[3] : -1.110826e-14 p[4] : +8.427191e-01 p[5] : +4.642804e-01 p[6] : -2.119426e-12 p[7] : -1.143166e-12 p[8] : +2.725173e-01 Worst observable directions --------------------------- Mode 1 (λ² = 3.933677e-01) p[0] : -7.048471e-01 p[1] : -7.089623e-01 p[2] : +1.152923e-11 p[3] : -1.259196e-11 p[4] : +7.457941e-03 p[5] : -1.816313e-02 p[6] : +3.281979e-14 p[7] : +2.721600e-14 p[8] : +1.332288e-02 Mode 2 (λ² = 2.032913e+01) p[0] : +7.093300e-01 p[1] : -7.046978e-01 p[2] : +1.253719e-11 p[3] : +2.109827e-11 p[4] : +1.874491e-03 p[5] : -1.118110e-02 p[6] : -1.866204e-14 p[7] : +1.595944e-14 p[8] : +1.110931e-02 Mode 3 (λ² = 6.950117e+01) p[0] : +2.395001e-11 p[1] : -5.353517e-12 p[2] : -4.306155e-10 p[3] : -9.999989e-01 p[4] : +3.477660e-12 p[5] : -1.500382e-11 p[6] : +1.486593e-03 p[7] : +4.640063e-13 p[8] : +1.466257e-11 Right singular vectors Vt ------------------------- Each row of Vt is one parameter-space singular vector. [[-3.848789e-15 -8.401314e-17 -1.077605e-03 -5.092241e-16 -1.364810e-12 -1.142972e-12 -1.445313e-17 -9.999994e-01 1.972964e-12] [ 6.021964e-16 1.946712e-15 -1.501405e-16 1.486593e-03 1.676481e-12 1.460052e-12 9.999989e-01 -1.093396e-17 1.055423e-13] [ 1.459622e-03 6.404659e-04 2.262723e-14 -1.110826e-14 8.427191e-01 4.642804e-01 -2.119426e-12 -1.143166e-12 2.725173e-01] [ 3.578971e-03 2.756579e-03 2.184034e-13 -7.162668e-14 5.112797e-01 -5.317644e-01 -9.391749e-15 -1.422374e-12 -6.751291e-01] [ 5.149466e-03 2.766769e-02 -2.740159e-11 2.123226e-11 1.683998e-01 -7.079657e-01 6.473410e-13 1.960928e-12 6.852985e-01] [ 6.263689e-13 -1.776642e-11 -9.999994e-01 4.306153e-10 -4.372595e-12 1.894519e-11 -6.401102e-13 1.077605e-03 -1.862888e-11] [ 2.395001e-11 -5.353517e-12 -4.306155e-10 -9.999989e-01 3.477660e-12 -1.500382e-11 1.486593e-03 4.640063e-13 1.466257e-11] [ 7.093300e-01 -7.046978e-01 1.253719e-11 2.109827e-11 1.874491e-03 -1.118110e-02 -1.866204e-14 1.595944e-14 1.110931e-02] [-7.048471e-01 -7.089623e-01 1.152923e-11 -1.259196e-11 7.457941e-03 -1.816313e-02 3.281979e-14 2.721600e-14 1.332288e-02]] ====================================================================== .. GENERATED FROM PYTHON SOURCE LINES 254-265 Study the optimization problem at convergence ----------------------------------------------- The problem is first solved with :func:`pysolvegn.solve`, which returns a :class:`pysolvegn.SolveResult`. The optimized parameters are stored in ``result.parameters``. ``study_optimization`` is then called at these converged parameters: the residual variance, the covariance and the standard deviations of the parameters are now meaningful, since the residuals only contain the measurement noise. .. GENERATED FROM PYTHON SOURCE LINES 265-284 .. code-block:: Python result = pysolvegn.solve( terms=[data_term], p0=initial_parameters, max_iteration=100, ftol=1e-6, xtol=1e-6, gtol=1e-6, ) print("Success:", result.success) print(result.message) converged_parameters = result.parameters pysolvegn.study_optimization( terms=[data_term], p0=converged_parameters, title="Camera calibration at convergence - complete parameter vector", ) .. rst-class:: sphx-glr-script-out .. code-block:: none Success: True [ftol] Convergence achieved: 0 <= F_previous - F < ftol * F with ftol = 1e-06. [xtol] Convergence achieved: ||Δp|| < xtol * (xtol + ||p||) with xtol = 1e-06. ====================================================================== Camera calibration at convergence - complete parameter vector ---------------------------------------------------------------------- Solver state ------------ The following quantities are taken directly from the current Gauss-Newton solver. Number of terms : 1 Number of equations : 330 Number of parameters : 9 Total cost : 3.976094e+01 ||g||_inf : 1.541694e-04 Term information ---------------- Term Type Equations Weight Cost 0 rJ 330 1.000000e+00 3.976094e+01 Hessian analysis ---------------- The Hessian below is the exact Gauss-Newton system built by the solver. It is expressed in the input parameter space. Largest singular value : 9.135298e+07 Smallest singular value : 3.934213e-01 Condition number : 2.322014e+08 Trace(H) : 1.498924e+08 Singular values of H -------------------- Index λ² 1/λ² 0 9.135298e+07 1.094655e-08 1 4.881911e+07 2.048378e-08 2 9.505952e+06 1.051973e-07 3 2.122227e+05 4.712032e-06 4 1.960704e+03 5.100209e-04 5 7.111640e+01 1.406145e-02 6 6.950613e+01 1.438722e-02 7 1.758374e+01 5.687072e-02 8 3.934213e-01 2.541804e+00 Parameter sensitivity --------------------- Degrees of freedom : 321 Estimated residual variance σ² : 2.477317e-01 Parameter Value Variance Std Std / |Value| 0 8.504093e+02 3.199857e-01 5.656728e-01 6.651771e-04 1 8.502887e+02 3.234749e-01 5.687485e-01 6.688887e-04 2 6.399000e+02 3.483467e-03 5.902090e-02 9.223458e-05 3 4.800564e+02 3.564165e-03 5.970063e-02 1.243617e-04 4 -2.029155e-01 3.346674e-05 5.785044e-03 2.850963e-02 5 5.573570e-02 2.477793e-04 1.574101e-02 2.824223e-01 6 9.604699e-04 1.204667e-08 1.097573e-04 1.142746e-01 7 -1.876755e-03 6.293312e-09 7.933040e-05 4.226999e-02 8 -1.354995e-02 1.598785e-04 1.264431e-02 9.331625e-01 SVD parameter-space directions ------------------------------ The right singular vectors of H describe directions in parameter space. Large singular values correspond to strongly observable directions, while small singular values correspond to weakly observable directions. Best observable directions -------------------------- Mode 1 (λ² = 9.135298e+07) p[0] : -1.991704e-06 p[1] : -2.152097e-07 p[2] : +1.013757e-03 p[3] : -1.023681e-15 p[4] : -2.641005e-10 p[5] : -1.297154e-10 p[6] : -1.645213e-12 p[7] : +9.999995e-01 p[8] : -7.085234e-11 Mode 2 (λ² = 4.881911e+07) p[0] : +2.061259e-07 p[1] : +9.234252e-07 p[2] : +3.221719e-15 p[3] : +1.398599e-03 p[4] : +1.638098e-10 p[5] : +7.871298e-11 p[6] : +9.999990e-01 p[7] : +2.254484e-12 p[8] : +4.245828e-11 Mode 3 (λ² = 9.505952e+06) p[0] : +1.248949e-03 p[1] : +5.517282e-04 p[2] : +1.494267e-12 p[3] : -2.367986e-12 p[4] : +8.427191e-01 p[5] : +4.642811e-01 p[6] : -9.530794e-10 p[7] : +2.908371e-09 p[8] : +2.725175e-01 Worst observable directions --------------------------- Mode 1 (λ² = 3.934213e-01) p[0] : -7.049201e-01 p[1] : -7.089384e-01 p[2] : +1.160572e-06 p[3] : +8.387421e-07 p[4] : +6.847315e-03 p[5] : -1.703883e-02 p[6] : +7.987812e-07 p[7] : -1.557740e-06 p[8] : +1.252027e-02 Mode 2 (λ² = 1.758374e+01) p[0] : -7.092549e-01 p[1] : +7.048197e-01 p[2] : -5.264294e-04 p[3] : -3.833210e-04 p[4] : -1.594619e-03 p[5] : +9.588059e-03 p[6] : +3.145978e-08 p[7] : -7.272709e-07 p[8] : -9.580242e-03 Mode 3 (λ² = 6.950613e+01) p[0] : +2.712800e-04 p[1] : -2.707598e-04 p[2] : +6.707952e-06 p[3] : -9.999989e-01 p[4] : +6.412273e-07 p[5] : -3.791557e-06 p[6] : +1.398599e-03 p[7] : -6.318193e-09 p[8] : +3.781567e-06 Right singular vectors Vt ------------------------- Each row of Vt is one parameter-space singular vector. [[-1.991704e-06 -2.152097e-07 1.013757e-03 -1.023681e-15 -2.641005e-10 -1.297154e-10 -1.645213e-12 9.999995e-01 -7.085234e-11] [ 2.061259e-07 9.234252e-07 3.221719e-15 1.398599e-03 1.638098e-10 7.871298e-11 9.999990e-01 2.254484e-12 4.245828e-11] [ 1.248949e-03 5.517282e-04 1.494267e-12 -2.367986e-12 8.427191e-01 4.642811e-01 -9.530794e-10 2.908371e-09 2.725175e-01] [-3.289797e-03 -2.524416e-03 6.457754e-11 5.480795e-11 -5.112818e-01 5.317628e-01 3.022380e-09 -7.113869e-09 6.751311e-01] [ 5.707630e-03 2.507155e-02 2.042762e-07 1.440906e-07 1.684221e-01 -7.080178e-01 -2.453070e-08 1.655767e-08 6.853347e-01] [-3.725557e-04 3.718578e-04 9.999993e-01 6.506157e-06 -8.817786e-07 5.211836e-06 -9.366102e-09 -1.013757e-03 -5.197922e-06] [ 2.712800e-04 -2.707598e-04 6.707952e-06 -9.999989e-01 6.412273e-07 -3.791557e-06 1.398599e-03 -6.318193e-09 3.781567e-06] [-7.092549e-01 7.048197e-01 -5.264294e-04 -3.833210e-04 -1.594619e-03 9.588059e-03 3.145978e-08 -7.272709e-07 -9.580242e-03] [-7.049201e-01 -7.089384e-01 1.160572e-06 8.387421e-07 6.847315e-03 -1.703883e-02 7.987812e-07 -1.557740e-06 1.252027e-02]] ====================================================================== .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.019 seconds) .. _sphx_glr_download_.._.._docs_source__gallery_study_optimization.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: study_optimization.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: study_optimization.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: study_optimization.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_