Cv2Intrinsic Class#
Overview#
- class Cv2Intrinsic(parameters=None, constants=None)[source]#
Subclass of the
pycvcam.core.Intrinsicclass that represents the OpenCV intrinsic model.Note
This class represents the intrinsic transformation, which is the last step of the process from the
world_pointsto theimage_points.Let \(\vec{x}_d = (x_d, y_d)\) be a point in distorted normalized camera coordinates. The corresponding image point \(\vec{x}_i = (x_i, y_i)\) is obtained using the intrinsic matrix \(K\):
\[\vec{x}_i = K \vec{x}_d\]where the intrinsic matrix is defined as
\[\begin{split}K = \begin{bmatrix} f_x & 0 & c_x \\ 0 & f_y & c_y \\ 0 & 0 & 1 \end{bmatrix}\end{split}\]with
\(f_x, f_y\) the focal lengths expressed in pixels,
\(c_x, c_y\) the principal point coordinates in pixels.
Note
If no distortion model is used, the distorted coordinates \(\vec{x}_d\) are identical to the normalized coordinates \(\vec{x}_n\).
Warning
This implementation does not include the skew parameter of the intrinsic matrix. If skew is required, use
pycvcam.SkewIntrinsic.The transformation is parameterized by 4 parameters and 0 constants:
focal length parameters \((f_x, f_y)\)
principal point parameters \((c_x, c_y)\)
Two shorthand views of the Jacobian matrix are available in the result object:
jacobian_df: Jacobian of the image points with respect to the focal lengths. Shape(..., 2, 2)corresponding to \((f_x, f_y)\).jacobian_dc: Jacobian of the image points with respect to the principal point. Shape(..., 2, 2)corresponding to \((c_x, c_y)\).
Note
The class can be instantiated in two ways:
providing the parameters directly as a numpy array of shape
(4,)containing(f_x, f_y, c_x, c_y),using the
from_matrix()class method to initialize the transformation from an intrinsic matrix.
- Parameters:
parameters (Optional[numpy.ndarray]) – Intrinsic parameters of shape
(4,)ordered as(f_x, f_y, c_x, c_y).constants (Optional[None]) – No constants are used in the Cv2Intrinsic model, must be set to None.
Instantiation#
The pycvcam.Cv2Intrinsic class can be instantiated in two ways:
From a 3×3 intrinsic matrix using the classmethod
from_matrix().From a parameter vector using the classmethod
from_vector().
|
Class method to create a Cv2Intrinsic object from an intrinsic matrix. |
|
Class method to create a Cv2Intrinsic object from an intrinsic vector. |
Accessing Parameters#
The intrinsic parameters and constants can be accessed via the base class
pycvcam.core.Transform:
parameters: returns a vector of intrinsic parameters [f_x, f_y, c_x, c_y].constants: returns any constants (none for Cv2Intrinsic).
Convenience properties are provided for direct access:
Get or set the focal length |
|
Get or set the focal length |
|
Get or set the principal point |
|
Get or set the principal point |
|
Get or set the intrinsic matrix of the intrinsic transformation. |
|
Get or set the intrinsic vector of the intrinsic transformation. |
Performing Projections#
Use the transform and inverse_transform methods from
pycvcam.core.Transform to perform intrinsic projections:
transform: mapsdistorted_points→image_points.inverse_transform: mapsimage_points→distorted_points.
Jacobians with respect to the input points and intrinsic parameters
can be computed by enabling the dx and dp flags:
dx=True: compute ∂image_points / ∂distorted_pointsdp=True: compute ∂image_points / ∂intrinsic_parameters
|
Apply the intrinsic projection from distorted normalized coordinates to image pixel coordinates. |
|
Apply the inverse of the intrinsic projection from image pixel coordinates to distorted normalized coordinates. |
Examples#
Create an intrinsic object from a matrix:
import numpy
from pycvcam import Cv2Intrinsic
intrinsic_matrix = numpy.array([[1000, 0, 320],
[0, 1000, 240],
[0, 0, 1]])
intrinsic = Cv2Intrinsic.from_matrix(intrinsic_matrix)
Transform distorted points to image points:
distorted_points = numpy.array([[100, 200],
[150, 250],
[200, 300]])
# Forward projection
result = intrinsic.transform(distorted_points)
image_points = result.image_points
print("Image points:\n", image_points)
Compute Jacobians:
result = intrinsic.transform(distorted_points, dx=True, dp=True)
jacobian_dx = result.jacobian_dx
jacobian_dp = result.jacobian_dp
print("Jacobian w.r.t input points:\n", jacobian_dx)
print("Jacobian w.r.t intrinsic parameters:\n", jacobian_dp)
Inverse transformation:
inverse_result = intrinsic.inverse_transform(image_points, dx=True, dp=True)
recovered_distorted = inverse_result.distorted_points
print("Recovered distorted points:\n", recovered_distorted)