IDS peak ICV v1.2.0
Loading...
Searching...
No Matches
camera_calibration.py
Go to the documentation of this file.
1# @package CameraCalibration
2
3from __future__ import annotations
4
5import ctypes
6from typing import TypeAlias, Any
7
8import numpy as np
9import numpy.typing as npt
10
11from ids_peak_icv import lib_loader
12from ids_peak_icv.exceptions import NotSupportedException, NotPossibleException
13from ids_peak_icv.calibration.calibration_result import CalibrationResult
14from ids_peak_icv.calibration.calibration_plate import CalibrationPlate
15from ids_peak_icv.backend.datatypes import peak_icv_calibration_result_handle
16from ids_peak_icv.backend.utils import execute_and_map_return_codes, create_array, transform
17
18from ids_peak_icv.datatypes.image import _check_and_get_icv_image
19
20# Note: We loose type info here, as it is stored as a pointer to a python object
21ImageArray: TypeAlias = npt.NDArray[np.object_]
22
23
25 """!
26 @brief
27 Class for calibrating a camera.
28
29 The CameraCalibration calculates the intrinsic and extrinsic parameters,
30 as well as the mean reprojection error.
31
32 @ingroup ids_peak_icv_python_calibration
33 @since ids_peak_icv 1.1
34 """
35
36 @classmethod
37 def create_from_plate(cls, calibration_plate: CalibrationPlate) -> CameraCalibration:
38 """!
39 @brief
40 Creates an instance of class CameraCalibration.
41
42 For a camera calibration a calibration plate is needed.
43
44 @param calibration_plate: A calibration plate.
45
46 @return A new instance of CameraCalibration.
47
48 @since ids_peak_icv 1.1
49 """
50 return cls(_calibration_plate=calibration_plate)
51
52 def __init__(self, *args: Any, **kwargs: Any) -> None:
53 """!
54 @brief
55 In order to create a CameraCalibration, please refer to the classmethods.
56
57 @since ids_peak_icv 1.1
58 """
59 try:
60 self._calibration_plate = kwargs["_calibration_plate"]
61 except KeyError:
63 "Cannot initialize {} with __init__. Please use a classmethod.".format(self.__class__.__name__))
64
65 def process(self, images: ImageArray) -> CalibrationResult:
66 """!
67 @brief
68 Applies the camera calibration and calculates the intrinsic and extrinsic
69 camera parameters based on several views of a calibration pattern.
70 Furthermore, it computes the mean reprojection error.
71
72 @note This operation disregards any specified image regions. It processes the entire image.
73
74 @param images: A list of images containing a calibration plate in different poses.
75
76 @exception MismatchException: If no images are provided, or if the sizes of the images are different.
77 @exception NotSupportedException: If the pattern of the calibration plate is not supported or
78 the image has an unsupported format.
79 @exception TargetNotFoundException: If the marker points were not found on the given image.
80
81 @since ids_peak_icv 1.1
82 """
83 if not isinstance(images, np.ndarray):
85 "Parameter images has to be a numpy array of ids_peak_icv or ids_peak_ipl images.")
86
87 _images = []
88 for image in images:
89 _images.append(_check_and_get_icv_image(image))
90
91 image_handles = create_array(ctypes.c_void_p, len(_images))
92 transform(_images, image_handles, lambda image: image._handle)
93
95 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_Result_Create,
96 ctypes.pointer(result_handle))
97 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_Process, self._calibration_plate._handle,
98 image_handles,
99 ctypes.c_size_t(len(image_handles)), ctypes.pointer(result_handle))
100
101 return CalibrationResult(_handle=result_handle)
Class for holding the results of a camera calibration.
CameraCalibration create_from_plate(cls, CalibrationPlate calibration_plate)
Creates an instance of class CameraCalibration.
CalibrationResult process(self, ImageArray images)
Applies the camera calibration and calculates the intrinsic and extrinsic camera parameters based on ...
None __init__(self, *Any args, **Any kwargs)
In order to create a CameraCalibration, please refer to the classmethods.
Thrown when an operation cannot be performed due to logical constraints.
Definition exceptions.py:85
Thrown when a requested operation is not implemented or supported.
Definition exceptions.py:71