IDS peak ICV v1.2.0
Loading...
Searching...
No Matches
tone_curve_correction_module.py
Go to the documentation of this file.
1import ctypes
2
3from ids_peak_common import PixelFormat, Interval
4from ids_peak_common.serialization import IArchive
5from ids_peak_icv import lib_loader
6from ids_peak_icv.backend.datatypes import peak_icv_tone_curve_correction_handle, peak_icv_interval_f
7from ids_peak_icv.backend.utils import execute_and_map_return_codes
8from ids_peak_icv.datatypes.image import Image
10
11
13 """!
14 @brief
15 A pipeline module for applying tone curve corrections such as gamma adjustment
16 and digital black offset.
17
18 Both, gamma and digital black corrections can be individually enabled or disabled.
19
20 Default configuration:
21 - gamma = 1.0
22 - digital black = 0.0
23
24 @ingroup ids_peak_icv_python_pipeline_modules
25 @since ids_peak_icv 1.0
26 """
27
28 DEFAULT_GAMMA: float = 1.0
29 DEFAULT_DIGITAL_BLACK: float = 0.0
30
31 SUPPORTED_PIXEL_FORMATS = [
32 PixelFormat.MONO_8, PixelFormat.MONO_10, PixelFormat.MONO_12, PixelFormat.MONO_16, PixelFormat.RGB_8,
33 PixelFormat.RGB_10, PixelFormat.RGB_12, PixelFormat.BGR_8, PixelFormat.BGR_10, PixelFormat.BGR_12,
34 PixelFormat.RGBA_8, PixelFormat.RGBA_10, PixelFormat.RGBA_12,
35 PixelFormat.BGRA_8, PixelFormat.BGRA_10, PixelFormat.BGRA_12
36 ]
37
38 def __init__(self) -> None:
39 """!
40 @brief
41 Initialize a tone curve correction module with default settings.
42
43 @since ids_peak_icv 1.0
44 """
45 super().__init__()
46
48 execute_and_map_return_codes(
49 lib_loader.dll().peak_icv_Preprocessing_ToneCurveCorrection_Create,
50 ctypes.pointer(handle),
51 )
52
53 self._handle = handle
54 self._gamma = 1.0
55 self._digital_black = 0.0
56 self._gamma_enabled = True
58
59 def __del__(self) -> None:
60 if hasattr(self, "_handle") and self._handle:
61 lib_loader.dll().peak_icv_Preprocessing_ToneCurveCorrection_Destroy(self._handle)
62
63 @property
64 def type(self) -> str:
65 """!
66 @brief
67 Returns the type of the module for serialization purposes.
68 @return Module type
69
70 @since ids_peak_icv 1.0
71 """
72 return self.__class__.__name__.replace("Module", "")
73
74 @property
75 def enabled(self) -> bool:
76 """!
77 @brief
78 Get whether this module is enabled.
79
80 When disabled, :func:`process` returns the input image unchanged.
81
82 @return True if the module is enabled, False otherwise.
83
84 @since ids_peak_icv 1.0
85 """
87
88 @enabled.setter
89 def enabled(self, value: bool) -> None:
90 """!
91 @brief
92 Enable or disable the module.
93
94 When disabled, :func:`process` returns the input image unchanged.
95
96 @param value: True to enable, False to disable.
97
98 @since ids_peak_icv 1.0
99 """
100 self._digital_black_enabled = value
101 self._gamma_enabled = value
102
103 @property
104 def gamma(self) -> float:
105 """!
106 @brief
107 Gets the current gamma correction exponent.
108
109 @return The gamma exponent currently set.
110
111 @since ids_peak_icv 1.0
112 """
113 return self._gamma
114
115 @gamma.setter
116 def gamma(self, value: float) -> None:
117 """!
118 @brief
119 Sets the gamma correction exponent.
120
121 This value determines the degree of inverse gamma correction applied during processing,
122 where output values are computed as pow(input, 1/gamma).
123 Higher gamma values lighten the image, while values closer to zero darken it.
124
125 @note The gamma exponent must be within the valid range returned by gamma_range property.
126
127 @param value: The gamma exponent.
128
129 @since ids_peak_icv 1.0
130 """
131 self._gamma = value
132 if self._gamma_enabled:
133 self._apply_gamma(value)
134
135 @property
136 def gamma_range(self) -> Interval:
137 """!
138 @brief
139 Returns the valid range for the gamma correction exponent.
140
141 @return An Interval containing the minimum and maximum allowed gamma values.
142
143 @since ids_peak_icv 1.0
144 """
145 c_interval = peak_icv_interval_f()
146 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_ToneCurveCorrection_GetGammaRange,
147 self._handle, ctypes.pointer(c_interval))
148 return Interval(c_interval.minimum, c_interval.maximum)
149
150 @property
151 def gamma_enabled(self) -> bool:
152 """!
153 @brief
154 Whether gamma correction is enabled.
155
156 @return True if gamma is applied during processing.
157
158 @since ids_peak_icv 1.0
159 """
160 return self._gamma_enabled
161
162 @gamma_enabled.setter
163 def gamma_enabled(self, enabled: bool) -> None:
164 """!
165 @brief
166 Whether gamma correction is enabled.
167
168 @param enabled: True if gamma correction is enabled.
169
170 @since ids_peak_icv 1.0
171 """
172 self._gamma_enabled = enabled
173 if enabled:
174 self._apply_gamma(self._gamma)
175
176 @property
177 def digital_black(self) -> float:
178 """!
179 @brief
180 Gets the current digital black offset.
181
182 @return The digital black value currently set.
183
184 @since ids_peak_icv 1.0
185 """
186 return self._digital_black
187
188 @digital_black.setter
189 def digital_black(self, value: float) -> None:
190 """!
191 @brief
192 Sets the digital black used for digital black correction.
193
194 This value is subtracted from the input image channel values before gamma correction. It typically represents sensor black offset.
195
196 \note The digital black must be within the valid range returned by digital_black_range property.
197
198 @param value: The digital black value in normalized units.
199
200 @since ids_peak_icv 1.0
201 """
202 self._digital_black = value
204 self._apply_digital_black(value)
205
206 @property
207 def digital_black_range(self) -> Interval:
208 """!
209 @brief
210 Returns the valid range for the digital black correction value.
211
212 @return An Interval containing the minimum and maximum allowed digital black values.
213
214 @since ids_peak_icv 1.0
215 """
216 c_interval = peak_icv_interval_f()
217 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_ToneCurveCorrection_GetDigitalBlackRange,
218 self._handle, ctypes.pointer(c_interval))
219 return Interval(c_interval.minimum, c_interval.maximum)
220
221 @property
222 def digital_black_enabled(self) -> bool:
223 """!
224 @brief
225 Whether digital black correction is enabled.
226
227 @return True if the digital black offset is applied.
228
229 @since ids_peak_icv 1.0
230 """
231 return self._digital_black_enabled
232
233 @digital_black_enabled.setter
234 def digital_black_enabled(self, enabled: bool) -> None:
235 """!
236 @brief
237 Whether digital black correction is enabled.
238
239 @param enabled: True if the digital black offset is applied.
240
241 @since ids_peak_icv 1.0
242 """
243 self._digital_black_enabled = enabled
244 if enabled:
246
247 def reset_to_default(self) -> None:
248 """!
249 @brief
250 Resets all module settings.
251
252 Restores the module configuration to its default values:
253 - gamma = 1.0
254 - digital_black = 0.0
255
256 @since ids_peak_icv 1.0
257 """
258 self._gamma = 1.0
259 self._digital_black = 0.0
260 self._apply_gamma(self._gamma)
262
263 def process(self, input_image: Image) -> Image:
264 """!
265 @brief
266 Processes the input image and returns a tone curve corrected output image.
267
268 @supportedPixelformats{ToneCurveCorrection}
269
270 Images with other formats are passed through unmodified.
271
272 This operation disregards any specified image regions and processes the entire image.
273
274 @param input_image: The input image to be processed.
275
276 @return A new image with tone curve correction applied.
277
278 @since ids_peak_icv 1.0
279 """
281 return input_image
282
283 if input_image.pixel_format not in self.SUPPORTED_PIXEL_FORMATS:
284 return input_image
285
287 return input_image
288
289 execute_and_map_return_codes(
290 lib_loader.dll().peak_icv_Preprocessing_ToneCurveCorrection_ProcessInplace,
291 self._handle,
292 input_image._handle,
293 )
294 return input_image
295
296 def serialize(self, archive: IArchive) -> None:
297 """!
298 @brief
299 Serializes the object's internal state into the provided archive.
300
301 This function populates the given archive with all parameters required to fully represent the current state of the object.
302 It ensures that the object can be reconstructed or transmitted accurately by saving all relevant data members
303 in a consistent and structured format.
304
305 @return The serialized module's internal state.
306
307 @since ids_peak_icv 1.0
308 """
309 # do not use base implementation, because enabled is not used in this module
310 archive.set("Version", self.versionversionversion)
311
312 gamma_archive = archive.create_archive()
313 gamma_archive.set("Enabled", self._gamma_enabled)
314 gamma_archive.set("Value", self._gamma)
315 archive.set("Gamma", gamma_archive)
316
317 digital_black_archive = archive.create_archive()
318 digital_black_archive.set("Enabled", self._digital_black_enabled)
319 digital_black_archive.set("Value", self._digital_black)
320 archive.set("DigitalBlack", digital_black_archive)
321
322 def deserialize(self, archive: IArchive) -> None:
323 """!
324 @brief
325 Restores the object's state from the provided archive.
326
327 This function reads and applies all necessary parameters from the given archive to reconstruct the internal state of the object.
328 It ensures that the object is restored to a valid and consistent state.
329
330 @param archive: The source archive containing the serialized parameters.
331
332 @exception CorruptedException: If Archive is malformed, misses keys or the values are invalid
333 @exception NotSupportedException: If the 'Version' entry indicates an unsupported version.
334
335 @note This function requires that the archive contains all expected fields as produced by a corresponding serialize() call.
336
337 @since ids_peak_icv 1.0
338 """
339 super()._validate_version(archive)
340
341 gamma_archive = archive.get_archive("Gamma")
342 self.gamma_enabledgamma_enabledgamma_enabled = gamma_archive.get("Enabled", bool)
343 self.gammagammagamma = float(gamma_archive.get("Value", float))
344
345 digital_black_archive = archive.get_archive("DigitalBlack")
346 self.digital_black_enableddigital_black_enableddigital_black_enabled = digital_black_archive.get("Enabled", bool)
347 self.digital_blackdigital_blackdigital_black = float(digital_black_archive.get("Value", float))
348
349 def _apply_gamma(self, value: float) -> None:
350 """!
351 @brief
352 Apply gamma correction to the backend.
353
354 @since ids_peak_icv 1.0
355 """
356 execute_and_map_return_codes(
357 lib_loader.dll().peak_icv_Preprocessing_ToneCurveCorrection_SetGamma,
358 self._handle,
359 ctypes.c_float(value),
360 )
361
362 def _apply_digital_black(self, value: float) -> None:
363 """!
364 @brief
365 Apply digital black correction to the backend.
366
367 @since ids_peak_icv 1.0
368 """
369 execute_and_map_return_codes(
370 lib_loader.dll().peak_icv_Preprocessing_ToneCurveCorrection_SetDigitalBlack,
371 self._handle,
372 ctypes.c_float(value),
373 )
None enabled(self, bool value)
Sets the enabled state of the module.
Definition imodule.py:35
bool enabled(self)
Gets whether the module is currently enabled.
Definition imodule.py:22
Base class for all pipeline modules.
Definition module_base.py:8
int version(self)
Return the module version.
None enabled(self, bool value)
Enable or disable the module.
bool enabled(self)
Get whether this module is enabled.
A pipeline module for applying tone curve corrections such as gamma adjustment and digital black offs...
None digital_black_enabled(self, bool enabled)
Whether digital black correction is enabled.
None deserialize(self, IArchive archive)
Restores the object's state from the provided archive.
None digital_black(self, float value)
Sets the digital black used for digital black correction.
None _apply_digital_black(self, float value)
Apply digital black correction to the backend.
Image process(self, Image input_image)
Processes the input image and returns a tone curve corrected output image.
None __init__(self)
Initialize a tone curve correction module with default settings.
Interval gamma_range(self)
Returns the valid range for the gamma correction exponent.
Interval digital_black_range(self)
Returns the valid range for the digital black correction value.
None serialize(self, IArchive archive)
Serializes the object's internal state into the provided archive.