IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
hdr.py
Go to the documentation of this file.
1from __future__ import annotations
2
3import ctypes
4from enum import Enum
5from typing import Any, TypeAlias
6
7import numpy as np
8import numpy.typing as npt
9
10from ids_peak_icv import lib_loader
11from ids_peak_icv.exceptions import NotSupportedException
12from ids_peak_icv.backend.datatypes import peak_icv_hdr_handle, peak_icv_image_handle
13from ids_peak_icv.backend.utils import execute_and_map_return_codes, create_array, transform
14from ids_peak_icv.datatypes.image import Image, _check_and_get_icv_image
15from ids_peak_common.datatypes.pixelformat import PixelFormat
16from ids_peak_common.datatypes.geometry.size import Size
17
18# Note: We loose type info here, as it is stored as a pointer to a python object
19ImageArray: TypeAlias = npt.NDArray[np.object_]
20
21
22class HDRAlgorithm(Enum):
23 """!
24 @brief
25 Specifies the HDR reconstruction algorithm.
26
27 Defines the algorithm used to reconstruct a High Dynamic Range (HDR) image
28 from multiple input images captured with different exposure settings.
29
30 @note
31 Currently, only the Debevec HDR reconstruction algorithm is supported.
32 Additional algorithms may be introduced in future releases.
33
34 @ingroup ids_peak_icv_python_hdr
35 @since ids_peak_icv 1.2
36 """
37
38 DEBEVEC = 0
39 """!
40 @brief
41 Debevec HDR reconstruction algorithm.
42
43 Uses the method proposed by Debevec and Malik to estimate the camera
44 response curve (CRC) and combine multiple exposures into an HDR image.
45
46 @since ids_peak_icv 1.2
47 """
48
49
50class HDR:
51 """!
52 @brief
53 Performs High Dynamic Range (HDR) image reconstruction.
54
55 This class provides functionality to generate a High Dynamic Range (HDR)
56 image from a set of input images captured with different exposure settings.
57 The input images are expected to depict the same scene while varying exactly
58 one parameter: either the exposure time OR the gain.
59
60 Internally, the HDR reconstruction is performed using the Debevec algorithm.
61 A key part of this process is determining the *Camera Response Curve* (CRC),
62 which maps digital pixel values to relative radiance.
63 The algorithm is fixed at construction time.
64
65 ### Typical usage
66 - Create an `HDR` object
67 - Optionally calculating the response curve using ``estimate_response_curve()``
68 - Generate the HDR image using `process()`
69
70 @note
71 All input images must have
72 the **same resolution**,
73 the **same pixel format**
74 and being captured from an **identical viewpoint of the same scene**
75 without any change in camera position or scene content.
76 Any mismatch may lead to invalid HDR reconstruction results.
77
78 ### Limitations
79 - All input images must be compatible (same resolution, pixel format, and scene)
80 - At least two input images must be provided
81 - Only the Debevec HDR algorithm is currently supported
82
83 @ingroup ids_peak_icv_python_hdr
84 @since ids_peak_icv 1.2
85 """
86
87 def __init__(self, *args: Any, **kwargs: Any) -> None:
88 """!
89 @brief
90 Constructs an HDR processing object.
91
92 Creates and initializes an HDR object using the default HDR reconstruction
93 algorithm (Debevec).
94
95 @since ids_peak_icv 1.2
96 """
98 execute_and_map_return_codes(
99 lib_loader.dll().peak_icv_HDR_Create,
100 ctypes.pointer(self._handle),
101 )
102
103 def __del__(self) -> None:
104 """!
105 @brief
106 Destroys the HDR object and releases backend resources.
107
108 @since ids_peak_icv 1.2
109 """
110 if hasattr(self, "_handle") and self._handle:
111 lib_loader.dll().peak_icv_HDR_Destroy(self._handle)
112
113 @property
114 def algorithm(self) -> HDRAlgorithm:
115 """!
116 @brief
117 Returns the HDR algorithm used by this object.
118
119 The algorithm is defined at construction time and determines how
120 the input images are combined during HDR reconstruction.
121
122 @return The HDR algorithm identifier.
123
124 @since ids_peak_icv 1.2
125 """
126 algorithm = ctypes.c_int()
127 execute_and_map_return_codes(
128 lib_loader.dll().peak_icv_HDR_GetAlgorithm,
129 self._handle,
130 ctypes.pointer(algorithm),
131 )
132 return HDRAlgorithm(algorithm.value)
133
134 def estimate_response_curve(self, images: ImageArray) -> None:
135 """!
136 @brief
137 Estimates the *Camera Response Curve* (CRC) from a set of input images.
138
139 This method calculates the response characteristics of the camera sensor.
140 The CRC defines the relationship between
141 scene radiance and the measured pixel values.
142
143 All input images must meet the following conditions:
144 - Represent the same scene.
145 - Be compatible in size and pixel format.
146 - Contain valid metadata for either exposure time or gain,
147 and the values for the chosen parameter must vary across the given images
148 (e.g., `ids_peak_common.datatype.MetadataKey.DeviceExposureTime` or
149 `ids_peak_common.datatype.MetadataKey.DeviceGain`).
150
151 Performing this estimation step explicitly can significantly reduce the processing
152 time of subsequent calls to ``process()``, as the curve does not need to be
153 re-calculated.
154
155 If this method is not called, the response curve is estimated automatically
156 during the first call to ``process()``.
157
158 @supportedPixelformats{HDR}
159
160 @param images:
161 Images of the same scene with different exposure time or gain metadata.
162 Must not be empty.
163
164 @since ids_peak_icv 1.3
165 """
166 if not isinstance(images, np.ndarray):
168 "Parameter images has to be a numpy array of ids_peak_icv or ids_peak_ipl images.")
169
170 _images = []
171 for image in images:
172 _images.append(_check_and_get_icv_image(image))
173
174 image_handles = create_array(peak_icv_image_handle, len(_images))
175 transform(_images, image_handles, lambda image: image._handle)
176
177 execute_and_map_return_codes(
178 lib_loader.dll().peak_icv_HDR_EstimateResponseCurve,
179 self._handle,
180 image_handles,
181 ctypes.c_size_t(len(image_handles)),
182 )
183
184 def process(self, images: ImageArray) -> Image:
185 """!
186 @brief
187 Generates an HDR image from the specified input images.
188
189 Processes the provided images and combines them into a single HDR image.
190
191 All input images must meet the following conditions:
192 - Represent the same scene.
193 - Be compatible in size and pixel format.
194 - Contain valid metadata for either exposure time or gain,
195 and the values for the chosen parameter must vary across the given images
196 (e.g., `ids_peak_common.datatype.MetadataKey.DeviceExposureTime` or
197 `ids_peak_common.datatype.MetadataKey.DeviceGain`).
198
199 If the camera response curve has not been explicitly estimated
200 using ``estimate_response_curve()``,
201 an internal estimation step is performed automatically
202 when it is first called.
203 This may increase the overall processing time for the first call.
204
205 @supportedPixelformats{HDR}
206
207 @param images:
208 Images of the same scene with different exposure time or gain metadata.
209 Must not be empty.
210
211 @return
212 The resulting HDR image with pixel format Mono32fIDS resp. RGB32fIDS.
213
214 @since ids_peak_icv 1.2
215 """
216 if not isinstance(images, np.ndarray):
218 "Parameter images has to be a numpy array of ids_peak_icv images.")
219
220 _images = []
221 for image in images:
222 _images.append(_check_and_get_icv_image(image))
223
224 image_handles = create_array(peak_icv_image_handle, len(images))
225 transform(images, image_handles, lambda image: image._handle)
226
227 # Query output pixel format
228 output_pixel_format = ctypes.c_uint()
229 execute_and_map_return_codes(
230 lib_loader.dll().peak_icv_HDR_Process_GetOutputPixelFormat,
231 self._handle,
232 ctypes.c_uint(images[0].pixel_format.value),
233 ctypes.pointer(output_pixel_format),
234 )
235
236 output_image = Image.create_from_pixel_format_and_size(
237 PixelFormat(output_pixel_format.value),
238 Size(images[0].width, images[0].height),
239 )
240
241 execute_and_map_return_codes(
242 lib_loader.dll().peak_icv_HDR_Process,
243 self._handle,
244 image_handles,
245 ctypes.c_size_t(len(image_handles)),
246 output_image._handle,
247 )
248
249 return output_image
Occurs when a requested operation is not implemented or supported (e.g., exporting to an unsupported ...
Definition exceptions.py:60
Specifies the HDR reconstruction algorithm.
Definition hdr.py:22
Performs High Dynamic Range (HDR) image reconstruction.
Definition hdr.py:50
None estimate_response_curve(self, ImageArray images)
Estimates the Camera Response Curve (CRC) from a set of input images.
Definition hdr.py:134
None __init__(self, *Any args, **Any kwargs)
Constructs an HDR processing object.
Definition hdr.py:87
Image process(self, ImageArray images)
Generates an HDR image from the specified input images.
Definition hdr.py:184
None __del__(self)
Destroys the HDR object and releases backend resources.
Definition hdr.py:103
HDRAlgorithm algorithm(self)
Returns the HDR algorithm used by this object.
Definition hdr.py:114