IDS peak ICV v1.3.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_common_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
69 @return Module type
70
71 @since ids_peak_icv 1.0
72 """
73 return self.__class__.__name__.replace("Module", "")
74
75 @property
76 def enabled(self) -> bool:
77 """!
78 @brief
79 Gets whether this module is enabled.
80
81 When disabled, :func:`process` returns the input image unchanged.
82
83 @return True if the module is enabled, False otherwise.
84
85 @since ids_peak_icv 1.0
86 """
88
89 @enabled.setter
90 def enabled(self, enabled: bool) -> None:
91 """!
92 @brief
93 Enables or disables the module.
94
95 When disabled, :func:`process` returns the input image unchanged.
96
97 @param enabled: True to enable, False to disable.
98
99 @since ids_peak_icv 1.0
100 """
103
104 @property
105 def gamma(self) -> float:
106 """!
107 @brief
108 Gets the current gamma correction exponent.
109
110 @return The gamma exponent currently set.
111
112 @since ids_peak_icv 1.0
113 """
114 return self._gamma
115
116 @gamma.setter
117 def gamma(self, value: float) -> None:
118 """!
119 @brief
120 Sets the gamma correction exponent.
121
122 This value determines the degree of inverse gamma correction applied during processing,
123 where output values are computed as pow(input, 1/gamma).
124 Higher gamma values lighten the image, while values closer to zero darken it.
125
126 @note The gamma exponent must be within the valid range returned by gamma_range property.
127
128 @param value: The gamma exponent.
129
130 @since ids_peak_icv 1.0
131 """
132 self._gamma = value
133 if self._gamma_enabled:
134 self._apply_gamma(value)
135
136 @property
137 def gamma_range(self) -> Interval:
138 """!
139 @brief
140 Returns the valid range for the gamma correction exponent.
141
142 @return An Interval containing the minimum and maximum allowed gamma values.
143
144 @since ids_peak_icv 1.0
145 """
146 c_interval = peak_common_interval_f()
147 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_ToneCurveCorrection_GetGammaRange,
148 self._handle, ctypes.pointer(c_interval))
149 return Interval(c_interval.minimum, c_interval.maximum)
150
151 @property
152 def gamma_enabled(self) -> bool:
153 """!
154 @brief
155 Whether gamma correction is enabled.
156
157 @return True if gamma is applied during processing.
158
159 @since ids_peak_icv 1.0
160 """
161 return self._gamma_enabled
162
163 @gamma_enabled.setter
164 def gamma_enabled(self, enabled: bool) -> None:
165 """!
166 @brief
167 Whether gamma correction is enabled.
168
169 @param enabled: True if gamma correction is enabled.
170
171 @since ids_peak_icv 1.0
172 """
173 self._gamma_enabled = enabled
174 self._apply_gamma(self._gamma if enabled else self.DEFAULT_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_common_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
245 self._digital_black if enabled else self.DEFAULT_DIGITAL_BLACK
246 )
247
248 def reset_to_default(self) -> None:
249 """!
250 @brief
251 Resets all settings to their defaults.
252
253 Default configuration:
254 - gamma = 1.0
255 - digital_black = 0.0
256
257 @note The enabled state does not change when calling this function.
258
259 @since ids_peak_icv 1.0
260 """
261 self._gamma = 1.0
262 self._digital_black = 0.0
263 self._apply_gamma(self._gamma)
265
266 def process(self, input_image: Image) -> Image:
267 """!
268 @brief
269 Processes the input image and returns a tone curve corrected output image.
270
271 @supportedPixelformats{ToneCurveCorrection}
272
273 Images with other formats are passed through unmodified.
274
275 This operation disregards any specified image regions and processes the entire image.
276
277 @param input_image: The input image to be processed.
278
279 @return A new image with tone curve correction applied.
280
281 @since ids_peak_icv 1.0
282 """
284 return input_image
285
286 if input_image.pixel_format not in self.SUPPORTED_PIXEL_FORMATS:
287 return input_image
288
290 return input_image
291
292 execute_and_map_return_codes(
293 lib_loader.dll().peak_icv_Preprocessing_ToneCurveCorrection_ProcessInplace,
294 self._handle,
295 input_image._handle,
296 )
297 return input_image
298
299 def serialize(self, archive: IArchive) -> None:
300 """!
301 @brief
302 Serializes the object's internal state into the provided archive.
303
304 This function populates the given archive with all parameters required to fully represent the current state of the object.
305 It ensures that the object can be reconstructed or transmitted accurately by saving all relevant data members
306 in a consistent and structured format.
307
308 @param archive: The target archive that will store the serialized parameters.
309
310 @since ids_peak_icv 1.0
311 """
312 # do not use base implementation, because enabled is not used in this module
313 archive.set("Version", self.versionversionversion)
314
315 gamma_archive = archive.create_archive()
316 gamma_archive.set("Enabled", self._gamma_enabled)
317 gamma_archive.set("Value", self._gamma)
318 archive.set("Gamma", gamma_archive)
319
320 digital_black_archive = archive.create_archive()
321 digital_black_archive.set("Enabled", self._digital_black_enabled)
322 digital_black_archive.set("Value", self._digital_black)
323 archive.set("DigitalBlack", digital_black_archive)
324
325 def deserialize(self, archive: IArchive) -> None:
326 """!
327 @brief
328 Restores the object's state from the provided archive.
329
330 This function reads and applies all necessary parameters from the given archive to reconstruct the internal state of the object.
331 It ensures that the object is restored to a valid and consistent state.
332
333 @param archive: The source archive containing the serialized parameters.
334
335 @exception CorruptedException: If Archive is malformed, misses keys or the values are invalid
336 @exception NotSupportedException: If the 'Version' entry indicates an unsupported version.
337
338 @note This function requires that the archive contains all expected fields as produced by a corresponding serialize() call.
339
340 @since ids_peak_icv 1.0
341 """
342 super()._validate_version(archive)
343
344 gamma_archive = archive.get_archive("Gamma")
345 self.gamma_enabledgamma_enabledgamma_enabled = gamma_archive.get("Enabled", bool)
346 self.gammagammagamma = float(gamma_archive.get("Value", float))
347
348 digital_black_archive = archive.get_archive("DigitalBlack")
349 self.digital_black_enableddigital_black_enableddigital_black_enabled = digital_black_archive.get("Enabled", bool)
350 self.digital_blackdigital_blackdigital_black = float(digital_black_archive.get("Value", float))
351
352 def _apply_gamma(self, value: float) -> None:
353 """!
354 @brief
355 Apply gamma correction to the backend.
356
357 @since ids_peak_icv 1.0
358 """
359 execute_and_map_return_codes(
360 lib_loader.dll().peak_icv_Preprocessing_ToneCurveCorrection_SetGamma,
361 self._handle,
362 ctypes.c_float(value),
363 )
364
365 def _apply_digital_black(self, value: float) -> None:
366 """!
367 @brief
368 Apply digital black correction to the backend.
369
370 @since ids_peak_icv 1.0
371 """
372 execute_and_map_return_codes(
373 lib_loader.dll().peak_icv_Preprocessing_ToneCurveCorrection_SetDigitalBlack,
374 self._handle,
375 ctypes.c_float(value),
376 )
Base class for all pipeline modules.
Definition module_base.py:8
int version(self)
Return the module version.
bool enabled(self)
Gets whether this module is enabled.
None enabled(self, bool enabled)
Enables or disables the module.
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.