IDS peak ICV v1.2.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
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 function and combine multiple exposures into an HDR image.
45 """
46
47
48class HDR:
49 """!
50 @brief
51 Performs High Dynamic Range (HDR) image reconstruction.
52
53 This class combines multiple images of the same scene captured with
54 different exposure settings into a single HDR image.
55
56 Internally, the Debevec algorithm is used.
57 The algorithm is fixed at construction time.
58
59 ### Typical usage
60 - Create an HDR object
61 - Optionally call :meth:`calibrate`
62 - Call :meth:`process` to generate the HDR image
63
64 @ingroup ids_peak_icv_python_hdr
65 @since ids_peak_icv 1.2
66 """
67
68 def __init__(self, *args: Any, **kwargs: Any) -> None:
69 """!
70 @brief
71 Creates an HDR processing object using the default HDR algorithm.
72
73 @since ids_peak_icv 1.2
74 """
76 execute_and_map_return_codes(
77 lib_loader.dll().peak_icv_HDR_Create,
78 ctypes.pointer(self._handle),
79 )
80
81 def __del__(self) -> None:
82 """!
83 @brief
84 Destroys the HDR object and releases backend resources.
85
86 @since ids_peak_icv 1.2
87 """
88 if hasattr(self, "_handle") and self._handle:
89 lib_loader.dll().peak_icv_HDR_Destroy(self._handle)
90
91 @property
92 def algorithm(self) -> HDRAlgorithm:
93 """!
94 @brief
95 @return
96 The HDR reconstruction algorithm used by this object.
97
98 @since ids_peak_icv 1.2
99 """
100 algorithm = ctypes.c_int()
101 execute_and_map_return_codes(
102 lib_loader.dll().peak_icv_HDR_GetAlgorithm,
103 self._handle,
104 ctypes.pointer(algorithm),
105 )
106 return HDRAlgorithm(algorithm.value)
107
108 def calibrate(self, images: ImageArray) -> None:
109 """!
110 @brief
111 Performs an optional HDR calibration step.
112
113 Calibration estimates camera response characteristics and may
114 reduce processing time when calling :meth:`process`.
115
116 @param images:
117 Images of the same scene with different exposure settings.
118 Must not be empty.
119
120 @since ids_peak_icv 1.2
121 """
122 if not isinstance(images, np.ndarray):
124 "Parameter images has to be a numpy array of ids_peak_icv or ids_peak_ipl images.")
125
126 _images = []
127 for image in images:
128 _images.append(_check_and_get_icv_image(image))
129
130 image_handles = create_array(peak_icv_image_handle, len(_images))
131 transform(_images, image_handles, lambda image: image._handle)
132
133 execute_and_map_return_codes(
134 lib_loader.dll().peak_icv_HDR_Calibrate,
135 self._handle,
136 image_handles,
137 ctypes.c_size_t(len(image_handles)),
138 )
139
140 def process(self, images: ImageArray) -> Image:
141 """!
142 @brief
143 Generates an HDR image from the specified input images.
144
145 If the HDR object was not explicitly calibrated using
146 :meth:`calibrate`, calibration is performed implicitly.
147
148 @param images:
149 Images of the same scene with different exposure settings.
150 All input images must contain exposure time metadata.
151 Must not be empty.
152
153 @return
154 The resulting HDR image.
155
156 @since ids_peak_icv 1.2
157 """
158 if not isinstance(images, np.ndarray):
160 "Parameter images has to be a numpy array of ids_peak_icv images.")
161
162 _images = []
163 for image in images:
164 _images.append(_check_and_get_icv_image(image))
165
166 image_handles = create_array(peak_icv_image_handle, len(images))
167 transform(images, image_handles, lambda image: image._handle)
168
169 # Query output pixel format
170 output_pixel_format = ctypes.c_uint()
171 execute_and_map_return_codes(
172 lib_loader.dll().peak_icv_HDR_Process_GetOutputPixelFormat,
173 self._handle,
174 ctypes.c_uint(images[0].pixel_format.value),
175 ctypes.pointer(output_pixel_format),
176 )
177
178 output_image = Image.create_from_pixel_format_and_size(
179 PixelFormat(output_pixel_format.value),
180 Size(images[0].width, images[0].height),
181 )
182
183 execute_and_map_return_codes(
184 lib_loader.dll().peak_icv_HDR_Process,
185 self._handle,
186 image_handles,
187 ctypes.c_size_t(len(image_handles)),
188 output_image._handle,
189 )
190
191 return output_image
A size consists of width and height where width and height have to be greater than or equal to zero.
Definition size.py:9
Enum listing all supported pixel formats and their internal IDs.
Thrown when a requested operation is not implemented or supported.
Definition exceptions.py:71
Specifies the HDR reconstruction algorithm.
Definition hdr.py:22
Performs High Dynamic Range (HDR) image reconstruction.
Definition hdr.py:48
None __init__(self, *Any args, **Any kwargs)
Creates an HDR processing object using the default HDR algorithm.
Definition hdr.py:68
Image process(self, ImageArray images)
Generates an HDR image from the specified input images.
Definition hdr.py:140
None __del__(self)
Destroys the HDR object and releases backend resources.
Definition hdr.py:81
HDRAlgorithm algorithm(self)
Definition hdr.py:92
None calibrate(self, ImageArray images)
Performs an optional HDR calibration step.
Definition hdr.py:108