IDS peak ICV v1.1.0
Loading...
Searching...
No Matches
calibration_view.py
Go to the documentation of this file.
1from __future__ import annotations
2
3import ctypes
4from typing import TypeAlias, Any, cast
5
6import numpy as np
7import numpy.typing as npt
8
9from ids_peak_common.datatypes.geometry.point import Point, Vector
10from ids_peak_icv import lib_loader
11from ids_peak_icv.backend.datatypes import (peak_icv_reprojection_error, peak_icv_calibration_view_handle,
12 peak_icv_extrinsic_parameters, peak_icv_coordinate_system)
13from ids_peak_icv.backend.utils import execute_and_map_return_codes, create_array
14from ids_peak_icv.exceptions import NotPossibleException
15from ids_peak_icv.calibration.extrinsic_parameters import ExtrinsicParameters
16from ids_peak_icv.calibration.reprojection_error import ReprojectionError
19
20# Note: We loose type info here, as it is stored as a pointer to a python object
21ReprojectionErrorArray: TypeAlias = npt.NDArray[np.object_]
22
23
25 """!
26 @brief
27 Class for holding the extrinsic result for one image of a camera calibration and also the reprojection
28 errors of every detected marker.
29
30 @ingroup ids_peak_icv_python_calibration
31 @since ids_peak_icv 1.1
32 """
33
34 @classmethod
35 def _create_empty(cls) -> CalibrationView:
37 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_View_Create,
38 ctypes.pointer(handle))
39
40 return CalibrationView(_handle=handle)
41
42 def __init__(self, *args: Any, **kwargs: Any) -> None:
43 """!
44 @brief
45 Please do not use this function. Refer to CalibrationResult.views to get views from a calibration result.
46
47 @since ids_peak_icv 1.1
48 """
49 try:
50 self._handle = kwargs["_handle"]
51 except KeyError:
52 raise NotPossibleException("{} shall not be initialized".format(self.__class__.__name__))
53 self._errors: np.ndarray | None = None
54
55 def __del__(self) -> None:
56 if hasattr(self, '_handle'):
57 lib_loader.dll().peak_icv_Calibration_View_Destroy(self._handle)
58
59 @property
60 def mean_reprojection_error(self) -> float:
61 """!
62 @brief
63 Provides the mean reprojection error from a calibration view.
64
65 The reprojection error is the distance between the marker points extracted from the calibration
66 images and the projected world points from CalibrationPlateInfo.
67
68 @return Returns the mean reprojection error.
69
70 @since ids_peak_icv 1.1
71 """
72 mean_reprojection_error = ctypes.c_double()
73 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_View_GetMeanReprojectionError,
74 self._handle,
75 ctypes.pointer(mean_reprojection_error))
76 return mean_reprojection_error.value
77
78 @property
79 def maximum_reprojection_error(self) -> float:
80 """!
81 @brief
82 Provides the maximum reprojection error from a calibration view.
83
84 The reprojection error is the distance between the marker points extracted from the calibration
85 images and the projected world points from CalibrationPlateInfo.
86
87 @return Returns the maximum reprojection error.
88
89 @since ids_peak_icv 1.1
90 """
91 maximum_reprojection_error = ctypes.c_double()
92 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_View_GetMaximumReprojectionError,
93 self._handle,
94 ctypes.pointer(maximum_reprojection_error))
95 return maximum_reprojection_error.value
96
97 @property
98 def reprojection_errors(self) -> ReprojectionErrorArray:
99 """!
100 @brief
101 Provides the reprojection errors for every marker in the view.
102
103 The reprojection error is the distance between the marker points extracted from the calibration images and
104 the projected world points from CalibrationPlateInfo.
105
106 @return Returns all reprojection errors.
107
108 @since ids_peak_icv 1.1
109 """
110 if self._errors is not None:
111 return self._errors
112
113 num_reprojection_errors = ctypes.c_size_t()
114 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_View_GetReprojectionErrors_GetCount,
115 self._handle,
116 ctypes.pointer(num_reprojection_errors))
117
118 if num_reprojection_errors.value == 0:
119 self._errors = np.array([])
120 return cast(ReprojectionErrorArray, self._errors)
121
122 errors = create_array(peak_icv_reprojection_error, num_reprojection_errors.value)
123 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_View_GetReprojectionErrors, self._handle,
124 ctypes.pointer(errors), num_reprojection_errors)
125
126 # workaround to fill the array on python3.7 with numpy
127 self._errors = np.empty(len(errors), dtype=ReprojectionError)
128 for i, it in enumerate(errors):
129 self._errors[i] = ReprojectionError(Point(it.position.x, it.position.y),
130 Vector(it.direction.x, it.direction.y), it.length)
131
132 return cast(ReprojectionErrorArray, self._errors)
133
134 @property
135 def extrinsic_parameters(self) -> ExtrinsicParameters:
136 """!
137 @brief
138 Provides the extrinsic parameters for the view.
139
140 A view always contains extrinsic parameters that describe the transformation from the calibration plate coordinate
141 system into the camera coordinate system.
142
143 @return Returns the extrinsic parameters from a calibration view.
144
145 @since ids_peak_icv 1.1
146 """
147 extrinsic_parameters = peak_icv_extrinsic_parameters()
148
149 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_View_GetExtrinsicParameters,
150 self._handle,
151 ctypes.pointer(extrinsic_parameters), ctypes.sizeof(extrinsic_parameters))
152
153 return ExtrinsicParameters(_parameters=extrinsic_parameters)
154
155 @property
156 def convex_hull(self) -> Polygon:
157 """!
158 @brief
159 Provides the convex hull of the found calibration pattern in the view.
160
161 @return Returns the convex hull as polygon from a calibration view.
162
163 @since ids_peak_icv 1.1
164 """
165 polygon = Polygon()
166 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_View_GetConvexHull,
167 self._handle,
168 ctypes.pointer(polygon._handle))
169
170 return polygon
171
172 @property
173 def coordinate_system(self) -> CoordinateSystem:
174 """!
175 @brief
176 Provides the coordinate system of the calibration view that was projected into the image plane.
177 The coordinate system is represented by its origin and three vectors, each corresponding to one of the three coordinate axes (X, Y and
178 Z). These vectors describe the directions of the respective axes projected from 3D into 2D space. Their lengths are determined by twice
179 the distance between neighboring markers on the calibration plate.
180
181 @return Returns the coordinate system from a calibration view.
182
183 @since ids_peak_icv 1.1
184 """
185 coordinate_system = peak_icv_coordinate_system()
186 execute_and_map_return_codes(lib_loader.dll().peak_icv_Calibration_View_GetCoordinateSystem,
187 self._handle,
188 ctypes.pointer(coordinate_system))
189
190 return CoordinateSystem(Point(coordinate_system.origin.x, coordinate_system.origin.y),
191 Point(coordinate_system.x_axis.x, coordinate_system.x_axis.y),
192 Point(coordinate_system.y_axis.x, coordinate_system.y_axis.y),
193 Point(coordinate_system.z_axis.x, coordinate_system.z_axis.y))
Represents a point in 2D space with x and y coordinates.
Definition point.py:6
Class for holding the extrinsic result for one image of a camera calibration and also the reprojectio...
float maximum_reprojection_error(self)
Provides the maximum reprojection error from a calibration view.
Polygon convex_hull(self)
Provides the convex hull of the found calibration pattern in the view.
None __init__(self, *Any args, **Any kwargs)
Please do not use this function.
ReprojectionErrorArray reprojection_errors(self)
Provides the reprojection errors for every marker in the view.
CoordinateSystem coordinate_system(self)
Provides the coordinate system of the calibration view that was projected into the image plane.
float mean_reprojection_error(self)
Provides the mean reprojection error from a calibration view.
Class for handling extrinsic parameters related to camera calibration.
Represents the reprojection error in a calibration view.
Represents the coordinate system of a calibration view that was projected into the 2D plane.
A polygon is a two-dimensional geometric shape that is made up of a finite number of straight line se...
Definition polygon.py:23
Thrown when an operation cannot be performed due to logical constraints.
Definition exceptions.py:85