IDS peak ICV v1.2.0
Loading...
Searching...
No Matches
undistortion.py
Go to the documentation of this file.
1from __future__ import annotations
2import ctypes
3from enum import Enum
4from typing import Union, Any
5
6from ids_peak_common.datatypes.metadata import Metadata
7from ids_peak_icv import lib_loader
8from ids_peak_icv.calibration.intrinsic_parameters import IntrinsicParameters
9from ids_peak_icv.backend.datatypes import peak_icv_intrinsic_parameters, peak_icv_undistortion_handle
10from ids_peak_icv.backend.utils import (execute_and_map_return_codes, metadata_to_capture_information,
11 check_init_for_classes_with_classmethods_only)
12from ids_peak_icv.datatypes.image import Image, _check_and_get_icv_image
13from ids_peak_icv.datatypes.undistorted_image import UndistortedImage
15from ids_peak_ipl import ids_peak_ipl
16
17
18class Interpolation(Enum):
19 """!
20 @brief
21 Enumeration of interpolation methods used in image transformations.
22
23 These methods determine how pixel values are calculated
24 when performing geometric transformations on images,
25 such as undistortion and scaling.
26
27 @since ids_peak_icv 1.1
28 @ingroup ids_peak_icv_python_transformations
29 """
30
31 BILINEAR = 0
32 """!
33 @brief
34 Calculates the output pixel value
35 as a weighted average of the four closest input pixels.
36 Suitable for smoother results.
37 """
38
39 NEAREST_NEIGHBOR = 1
40 """!
41 @brief
42 Uses the nearest input pixel value directly.
43 Faster but may introduce aliasing or blocky artifacts.
44 """
45
46
48 """!
49 @brief
50 Represents an undistortion transformation for images.
51
52 Provides geometric correction
53 to remove lens distortion using intrinsic calibration parameters.
54 The parameters can be obtained using `CameraCalibration.process()`.
55
56 This class also supports undistorting images with binning factors
57 that differ from those used during calibration,
58 as long as the image metadata includes valid binning information.
59 The binning factor of the image to be undistorted
60 must be greater than or equal
61 to that of the calibration images.
62
63 @note
64 If the metadata of the images to be undistorted
65 differs from the metadata used during calibration (e.g. due to different binning),
66 internal parameters may need to be reinitialized.
67 In such cases, the first call to `process()` may take longer.
68 To avoid this,
69 call `set_capture_information()` beforehand
70 with matching metadata.
71
72 ### Limitations
73 - In-place undistortion is not supported.
74 - Maximum supported image size is 32767 × 32767 pixels.
75
76 @since ids_peak_icv 1.1
77 @ingroup ids_peak_icv_python_transformations
78 """
79
80 _intrinsic_parameters: IntrinsicParameters
81
82 def __init__(self, *args: Any, **kwargs: Any) -> None:
83 """!
84 @brief
85 In order to create an Image, please refer to the classmethods ``create_from_intrinsics`` and
86 ``create_from_intrinsics_and_metadata``.
87
88 @since ids_peak_icv 1.1
89 """
90 self._handle = check_init_for_classes_with_classmethods_only(self.__class__.__name__,
91 peak_icv_undistortion_handle,
92 *args, **kwargs)
93
94 if len(kwargs):
95 self._handle = kwargs["_handle"]
96 self._new_intrinsic_parameters = kwargs["_new_intrinsic_parameters"]
97
98 @classmethod
99 def create_from_intrinsics(cls, intrinsic_parameters: IntrinsicParameters) -> Undistortion:
100 """!
101 @brief
102
103 @param intrinsic_parameters: Intrinsic parameters derived from a camera calibration.
104 @return
105
106 @since ids_peak_icv 1.1
107 """
108 cls._intrinsic_parameters = intrinsic_parameters
111 execute_and_map_return_codes(lib_loader.dll().peak_icv_Transform_Undistortion_Create,
112 ctypes.pointer(cls._handle),
113 cls._intrinsic_parameters._parameters,
114 ctypes.sizeof(cls._intrinsic_parameters._parameters),
115 ctypes.pointer(cls._new_intrinsic_parameters))
116
117 return cls(_handle=cls._handle, _new_intrinsic_parameters=cls._new_intrinsic_parameters)
118
119 @classmethod
120 def create_from_intrinsics_and_metadata(cls, intrinsic_parameters: IntrinsicParameters,
121 metadata: Metadata) -> Undistortion:
122 """!
123 @brief
124
125 @param intrinsic_parameters: Intrinsic parameters derived from a camera calibration.
126 @param metadata: Metadata of an image which contains information about the image capture settings.
127 @return
128 @since ids_peak_icv 1.1
129 """
130 cls._intrinsic_parameters = intrinsic_parameters
133 c_capture_information = metadata_to_capture_information(metadata)
134 execute_and_map_return_codes(
135 lib_loader.dll().peak_icv_Transform_Undistortion_Create_With_Image_CaptureInformation,
136 ctypes.pointer(cls._handle),
137 cls._intrinsic_parameters._parameters,
138 ctypes.sizeof(cls._intrinsic_parameters._parameters),
139 c_capture_information, ctypes.sizeof(c_capture_information),
140 ctypes.pointer(cls._new_intrinsic_parameters))
141
142 return cls(_handle=cls._handle, _new_intrinsic_parameters=cls._new_intrinsic_parameters)
143
144 def __del__(self) -> None:
145 """!
146 @brief
147 Destroys the undistortion object and releases any associated resources.
148
149 @since ids_peak_icv 1.1
150 """
151 if hasattr(self, '_handle'):
152 lib_loader.dll().peak_icv_Image_Destroy(self._handle)
153
154 def process(self, image: Union[Image, ids_peak_ipl.Image]) -> UndistortedImage:
155 """!
156 @brief
157 Applies undistortion to the specified image.
158
159 This function undistorts the image
160 and returns an `UndistortedImage` that includes updated intrinsic parameters.
161
162 The returned image has the same size and pixel format as the input image.
163
164 @note
165 If the input image specifies a region, that region is also undistorted.
166 When no region is specified (i.e., the full image is used), the output will also
167 represent the full image.
168
169 @param image:
170 The image to be undistorted.
171
172 @supportedPixelformats{Undistortion}
173
174 @return
175 An undistorted image with updated intrinsic parameters.
176
177 @exception NotPossibleException:
178 The image binning factor is too small
179 for the undistortion configuration.
180 @exception MismatchException:
181 The image size or capture information
182 does not match the undistortion configuration.
183
184 @since ids_peak_icv 1.1
185 """
186 _image = _check_and_get_icv_image(image)
187
188 output_image = Image.create_from_pixel_format_and_size(_image._pixel_format,
189 Size(_image.width, _image.height))
190
191 execute_and_map_return_codes(lib_loader.dll().peak_icv_Transform_Undistortion_Process,
192 self._handle,
193 _image._handle,
194 output_image._handle)
195
196 return UndistortedImage(output_image,
198
199 @property
200 def interpolation(self) -> Interpolation:
201 """!
202 @brief
203 @return
204 The current @ref Interpolation "interpolation method" used by the undistortion.
205
206 @since ids_peak_icv 1.1
207 """
208 interpolation = ctypes.c_size_t()
209 execute_and_map_return_codes(lib_loader.dll().peak_icv_Transform_Undistortion_GetInterpolation,
210 self._handle,
211 ctypes.pointer(interpolation))
212
213 return Interpolation(interpolation.value)
214
215 @interpolation.setter
216 def interpolation(self, interpolation: Interpolation) -> None:
217 """!
218 @brief
219 Sets the @ref Interpolation "interpolation method" used for undistortion.
220
221 @param interpolation:
222 @ref Interpolation "Interpolation method" to use.
223
224 @since ids_peak_icv 1.1
225 """
226 execute_and_map_return_codes(lib_loader.dll().peak_icv_Transform_Undistortion_SetInterpolation,
227 self._handle,
228 ctypes.c_size_t(interpolation.value))
A size consists of width and height where width and height have to be greater than or equal to zero.
Definition size.py:9
The UndistortedImage class is a class derived from the Image class.
Enumeration of interpolation methods used in image transformations.
Represents an undistortion transformation for images.
None __init__(self, *Any args, **Any kwargs)
In order to create an Image, please refer to the classmethods create_from_intrinsics and create_from_...
Undistortion create_from_intrinsics(cls, IntrinsicParameters intrinsic_parameters)
Undistortion create_from_intrinsics_and_metadata(cls, IntrinsicParameters intrinsic_parameters, Metadata metadata)
None __del__(self)
Destroys the undistortion object and releases any associated resources.
UndistortedImage process(self, Union[Image, ids_peak_ipl.Image] image)
Applies undistortion to the specified image.