IDS peak ICV v1.1.0
Loading...
Searching...
No Matches
calibration_result.py
Go to the documentation of this file.
1from __future__ import annotations
2
3import ctypes
4from typing import TypeAlias, Any
5
6import numpy as np
7import numpy.typing as npt
8
9from ids_peak_icv import lib_loader
10from ids_peak_icv.exceptions import ICVException
11from ids_peak_icv.backend.datatypes import (peak_icv_calibration_result_handle, peak_icv_calibration_view_handle,
12 peak_icv_calibration_parameters)
13from ids_peak_icv.backend.utils import (execute_and_map_return_codes, create_array,
14 check_init_for_classes_with_classmethods_only)
15from ids_peak_icv.calibration.calibration_parameters import CalibrationParameters
16from ids_peak_icv.calibration.calibration_view import CalibrationView
17
18# Note: We loose type info here, as it is stored as a pointer to a python object
19CalibrationViewArray: TypeAlias = npt.NDArray[np.object_]
20
21
23 """!
24 @brief
25 Class for holding the results of a camera calibration.
26
27 The CalibrationResult stores intrinsic and extrinsic parameters as well as the
28 mean reprojection error calculated by CameraCalibration.process().
29
30 @ingroup ids_peak_icv_python_calibration
31 @since ids_peak_icv 1.1
32 """
33
34 @classmethod
35 def create_from_file(cls, file_path: str) -> CalibrationResult:
36 """!
37 @brief
38 Creates an instance of CalibrationResult by loading the calibration result from a file.
39
40 @param file_path: An existing file path to a JSON file containing the calibration result.
41 @return A new instance of CalibrationResult
42
43 @exception IOException: If the given file_path does not exist, or the permissions are not sufficient
44 to read it.
45 @exception CorruptedException: If the file content is corrupted.
46
47 @since ids_peak_icv 1.1
48 """
49 c_file_path = file_path.encode('utf-8')
50
52
53 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_Result_CreateFromFile,
54 ctypes.pointer(handle),
55 ctypes.c_char_p(c_file_path),
56 ctypes.c_size_t(len(c_file_path)))
57 return cls(_handle=handle)
58
59 @classmethod
60 def _create_empty(cls) -> CalibrationResult:
62 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_Result_Create,
63 ctypes.pointer(handle))
64
65 return CalibrationResult(_handle=handle)
66
67 def __init__(self, *args: Any, **kwargs: Any) -> None:
68 """!
69 @brief
70 In order to create a CalibrationResult, please refer to the classmethods.
71
72 @since ids_peak_icv 1.1
73 """
74 self._handle = check_init_for_classes_with_classmethods_only(self.__class__.__name__,
75 peak_icv_calibration_result_handle,
76 *args, **kwargs)
77 self._views: np.ndarray | None = None
78
79 def __del__(self) -> None:
80 if hasattr(self, '_handle'):
81 lib_loader.dll().peak_icv_Calibration_Result_Destroy(self._handle)
82
83 @property
84 def mean_reprojection_error(self) -> float:
85 """!
86 @brief
87 Provides the mean reprojection error from calibration result.
88
89 The mean reprojection error is the root-mean-square of distances between the marker points extracted from the
90 calibration images and the projected world points from the used calibration plate.
91
92 @since ids_peak_icv 1.1
93 """
94 c_mean_reprojection_error = ctypes.c_double()
95
96 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_Result_GetMeanReprojectionError,
97 self._handle, ctypes.pointer(c_mean_reprojection_error))
98 return c_mean_reprojection_error.value
99
100 @property
101 def views(self) -> CalibrationViewArray:
102 """!
103 @brief
104 Provides a list of ``CalibrationView``.
105
106 The order in which the CalibrationViews are provided, is equivalent to the order of images provided to
107 CameraCalibration.CameraCalibration.Process.
108
109 @since ids_peak_icv 1.1
110 """
111 if self._views is not None:
112 return self._views
113
114 view_count = ctypes.c_size_t()
115 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_Result_GetCalibrationViews_GetCount,
116 self._handle, ctypes.pointer(view_count))
117
118 if view_count.value == 0:
119 # note: this is needed as doxygen reports something about npt NDArray not being defined
120 # @cond HIDE_FROM_DOXYGEN
121 self._views = np.array([])
122 # @endcond
123 return self._views
124
125 view_handles = create_array(peak_icv_calibration_view_handle, view_count.value)
126 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_View_Array_Create,
127 view_handles, view_count)
128 try:
129 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_Result_GetCalibrationViews,
130 self._handle, view_handles, view_count)
131 views = np.array([CalibrationView(_handle=h) for h in view_handles])
132 except ICVException as e:
133 lib_loader.dll().peak_icv_Calibration_View_Array_Destroy(view_handles, view_count)
134 raise e
135
136 self._views = views
137 return self._views
138
139 def save(self, file_path: str) -> None:
140 """!
141 @brief
142 Saves calibration result to a JSON file for later use.
143
144 @param file_path: An existing file path to save calibration result.
145
146 @exception IOException: If the specified file path is invalid, you lack the necessary permissions, or
147 the file already exists.
148
149 @since ids_peak_icv 1.1
150 """
151 c_file_path = file_path.encode('utf-8')
152 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_Result_SaveToFile,
153 self._handle,
154 ctypes.c_char_p(c_file_path))
155
156 @property
157 def calibration_parameters(self) -> CalibrationParameters:
158 """!
159 @brief
160 Provides the @p CalibrationParameters that contain the necessary information to use the camera calibration.
161
162 @return An instance of CalibrationParameters
163
164 @since ids_peak_icv 1.1
165 """
167 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_Result_ToParameters,
168 self._handle, ctypes.pointer(parameters), ctypes.sizeof(parameters))
169
170 return CalibrationParameters(_parameters=parameters)
Represents the calibration parameters, which include intrinsic and extrinsic parameters.
Class for holding the results of a camera calibration.
None __init__(self, *Any args, **Any kwargs)
In order to create a CalibrationResult, please refer to the classmethods.
None save(self, str file_path)
Saves calibration result to a JSON file for later use.
CalibrationResult create_from_file(cls, str file_path)
Creates an instance of CalibrationResult by loading the calibration result from a file.
CalibrationViewArray views(self)
Provides a list of CalibrationView.
float mean_reprojection_error(self)
Provides the mean reprojection error from calibration result.
Class for holding the extrinsic result for one image of a camera calibration and also the reprojectio...