6from ids_peak_common
import Interval, Range, PixelFormat
7from ids_peak_common.serialization
import IArchive
8from ids_peak_common.pipeline.modules.iautofeature_module
import IAutoFeature
9from ids_peak_icv
import lib_loader
11 peak_icv_color_matrix_transformation_handle, peak_common_interval_f,
16from ids_peak_icv.pipeline.datatypes
import ChromaticAdaptionAlgorithm, ColorSpace
22 Color matrix transformation is an image pipeline module that applies a color correction matrix to an image.
24 Provides the features ColorCorrectionFeature, SaturationFeature and ChromaticAdaptionFeature.
26 Default configuration:
27 - Color correction matrix = 3x3 identity matrix
29 - no chromatic adaption correction applied
31 @since ids_peak_icv 1.0
34 DEFAULT_MATRIX: np.ndarray = np.eye(3)
37 Default color correction matrix value.
39 @since ids_peak_icv 1.0
42 DEFAULT_SATURATION: float = 1.0
45 Default saturation value.
47 @since ids_peak_icv 1.0
50 SUPPORTED_PIXEL_FORMATS = [
51 PixelFormat.RGB_8, PixelFormat.RGB_10, PixelFormat.RGB_12, PixelFormat.BGR_8, PixelFormat.BGR_10,
52 PixelFormat.BGR_12, PixelFormat.RGBA_8, PixelFormat.RGBA_10, PixelFormat.RGBA_12, PixelFormat.BGRA_8,
53 PixelFormat.BGRA_10, PixelFormat.BGRA_12
59 Initialize a color matrix transformation module with default settings.
61 @since ids_peak_icv 1.0
66 execute_and_map_return_codes(
67 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_Create,
68 ctypes.pointer(_color_matrix_transformation_handle)
70 self.
_handle = _color_matrix_transformation_handle
76 if hasattr(self,
"_handle")
and self.
_handle:
77 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_Destroy(self.
_handle)
83 Returns the type of the module for serialization purposes.
87 @since ids_peak_icv 1.0
89 return "ColorMatrixTransformation"
95 Return the module version.
97 @return Module version.
99 @since ids_peak_icv 1.3
107 Gets whether this module is enabled.
109 When disabled, :func:`process` returns the input image unchanged.
111 @return True if the module is enabled, False otherwise.
113 @since ids_peak_icv 1.0
121 Enables or disables the module.
123 When disabled, :func:`process` returns the input image unchanged.
125 @param enabled: True to enable, False to disable.
127 @since ids_peak_icv 1.0
138 Gets the current color correction matrix.
140 @return The current color correction matrix.
142 @since ids_peak_icv 1.0
146 c_matrix = peak_icv_color_correction_matrix.from_buffer_copy(
147 np.asarray(matrix, dtype=np.float32).flatten().tobytes())
149 execute_and_map_return_codes(
150 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_GetColorCorrectionMatrix,
152 ctypes.pointer(c_matrix),
154 return cast(np.ndarray, np.frombuffer(memoryview(c_matrix), dtype=np.float32).reshape(3, 3).copy())
157 def matrix(self, matrix: np.ndarray) ->
None:
160 Sets the color correction matrix.
162 @param matrix: The color correction matrix.
164 @since ids_peak_icv 1.0
166 c_matrix = peak_icv_color_correction_matrix.from_buffer_copy(
167 np.asarray(matrix, dtype=np.float32).flatten().tobytes())
169 execute_and_map_return_codes(
170 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_SetColorCorrectionMatrix,
180 Gets the current saturation value.
182 @return The current saturation value.
184 @since ids_peak_icv 1.0
186 saturation = ctypes.c_float()
187 execute_and_map_return_codes(
188 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_GetSaturation,
190 ctypes.byref(saturation))
191 return saturation.value
197 Sets the saturation value.
199 @param saturation: The saturation value.
201 @since ids_peak_icv 1.0
203 execute_and_map_return_codes(
204 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_SetSaturation,
206 ctypes.c_float(saturation))
214 Gets the valid range for saturation.
216 @return The valid saturation range.
218 @since ids_peak_icv 1.0
221 execute_and_map_return_codes(
222 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_GetSaturationRange,
223 self.
_handle, ctypes.pointer(c_interval))
224 return Interval(c_interval.minimum, c_interval.maximum)
230 Gets whether color correction is enabled.
232 When enabled, :func:`process` applies color correction to the input image.
234 @return True if color correction is enabled, False otherwise.
236 @since ids_peak_icv 1.0
238 enabled = ctypes.c_bool()
240 execute_and_map_return_codes(
241 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_ColorCorrection_IsEnabled,
243 ctypes.byref(enabled),
245 return bool(enabled.value)
247 @color_correction_enabled.setter
251 Enables or disables color correction.
253 When enabled, :func:`process` applies color correction to the input image.
255 @param enabled: True to enable, False to disable.
257 @since ids_peak_icv 1.0
259 execute_and_map_return_codes(
260 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_ColorCorrection_SetEnabled,
262 ctypes.c_bool(enabled),
270 Checks if the chromatic adaptation feature is enabled.
272 @return True if chromatic adaptation is currently active, false otherwise.
274 @since ids_peak_icv 1.3
276 enabled = ctypes.c_bool()
278 execute_and_map_return_codes(
279 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_IsEnabled,
281 ctypes.byref(enabled),
283 return bool(enabled.value)
285 @chromatic_adaption_enabled.setter
289 Enables or disables the chromatic adaptation feature.
291 @param enabled: True to enable chromatic adaptation, false to disable it.
293 @since ids_peak_icv 1.3
295 execute_and_map_return_codes(
296 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_SetEnabled,
298 ctypes.c_bool(enabled),
306 Returns the current algorithm used for chromatic adaptation.
308 @return The active ChromaticAdaptionAlgorithm.
310 @since ids_peak_icv 1.3
312 algorithm = ctypes.c_int()
313 execute_and_map_return_codes(
314 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_GetAlgorithm,
316 ctypes.byref(algorithm),
320 @chromatic_adaption_algorithm.setter
324 Sets the algorithm used to calculate the chromatic adaptation transformation.
326 @param algorithm: The ChromaticAdaptionAlgorithm to apply.
328 @since ids_peak_icv 1.3
330 execute_and_map_return_codes(
331 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_SetAlgorithm,
341 Returns the target color space used for chromatic adaptation.
343 @return The current target ColorSpace.
345 @since ids_peak_icv 1.3
347 color_space = ctypes.c_int()
348 execute_and_map_return_codes(
349 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_GetTargetColorSpace,
351 ctypes.byref(color_space),
355 @target_color_space.setter
359 Sets the target color space for the chromatic adaptation transformation.
361 @param color_space: The target ColorSpace.
363 @since ids_peak_icv 1.3
365 execute_and_map_return_codes(
366 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_SetTargetColorSpace,
376 Returns the currently configured source color temperature in Kelvin.
378 @return The color temperature value in Kelvin.
380 @since ids_peak_icv 1.3
382 temperature = ctypes.c_uint32()
383 execute_and_map_return_codes(
384 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_GetColorTemperature,
386 ctypes.byref(temperature),
388 return int(temperature.value)
390 @color_temperature.setter
394 Sets the source color temperature in Kelvin to be used for chromatic adaptation.
396 @param temperature: The color temperature value in Kelvin.
398 @since ids_peak_icv 1.3
400 execute_and_map_return_codes(
401 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_SetColorTemperature,
403 ctypes.c_uint32(int(temperature)),
411 Returns the valid range of values for color temperature.
413 @return A Range denoting the min and max allowed temperature values as well as its allowed increment.
415 @since ids_peak_icv 1.3
418 execute_and_map_return_codes(
419 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_GetColorTemperatureRange,
421 ctypes.byref(c_range),
423 return Range(c_range.minimum, c_range.maximum, c_range.increment)
428 Checks if a specific color temperature has been explicitly set.
430 @return True if a color temperature is configured, false otherwise.
432 @since ids_peak_icv 1.3
434 value = ctypes.c_bool()
435 execute_and_map_return_codes(
436 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_HasColorTemperature,
440 return bool(value.value)
445 Resets the color temperature to its default, uninitialized state.
447 @since ids_peak_icv 1.3
449 execute_and_map_return_codes(
450 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_ChromaticAdaption_ResetColorTemperature,
458 Resets all chromatic adaptation settings (temperature, algorithm, color space) to their default values.
460 @since ids_peak_icv 1.3
470 Gets whether saturation is enabled.
472 When enabled, :func:`process` applies saturation to the input image.
474 @return True if saturation is enabled, False otherwise.
476 @since ids_peak_icv 1.0
478 enabled = ctypes.c_bool()
480 execute_and_map_return_codes(
481 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_Saturation_IsEnabled,
483 ctypes.byref(enabled),
485 return bool(enabled.value)
487 @saturation_enabled.setter
491 Enables or disables saturation.
493 When enabled, :func:`process` applies saturation to the input image.
495 @param enabled: True to enable, False to disable.
497 @since ids_peak_icv 1.0
499 execute_and_map_return_codes(
500 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_Saturation_SetEnabled,
502 ctypes.c_bool(enabled),
506 def process(self, input_image: Image) -> Image:
509 Processes the input image and returns a color corrected and/or saturated output image.
511 @supportedPixelformats{ColorMatrixTransformation}
513 Images with other formats are passed through unmodified.
515 @note This operation disregards any specified image regions and processes the entire image.
517 @param input_image: The input image to be processed.
519 @return The input image after in-place color correction and/or saturation has been applied.
521 @since ids_peak_icv 1.0
529 execute_and_map_return_codes(
530 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_ProcessInplace,
539 Resets all settings to their defaults.
541 Default configuration:
544 - no chromatic adaption correction
546 @note The enabled state does not change when calling this function.
548 @since ids_peak_icv 1.0
557 Serializes the object's internal state into the provided archive.
559 This function populates the given archive with all parameters required to fully represent the current state of the object.
560 It ensures that the object can be reconstructed or transmitted accurately by saving all relevant data members
561 in a consistent and structured format.
563 @param archive: The target archive that will store the serialized parameters.
565 @since ids_peak_icv 1.0
569 saturation_archive = archive.create_archive()
575 archive.set(
"Saturation", saturation_archive)
577 color_correction_matrix_archive = archive.create_archive()
579 color_correction_matrix_archive.set(
"Matrix",
581 archive.set(
"ColorCorrection", color_correction_matrix_archive)
583 chromatic_archive = archive.create_archive()
591 archive.set(
"ChromaticAdaption", chromatic_archive)
596 Restores the object's state from the provided archive.
598 This function reads and applies all necessary parameters from the given archive
599 to reconstruct the internal state of the object.
600 It ensures that the object is restored to a valid and consistent state.
602 @param archive: The source archive containing the serialized parameters.
604 @exception CorruptedException: If Archive is malformed, misses keys or the values are invalid
605 @exception NotSupportedException: If the 'Version' entry indicates an unsupported version.
607 @note This function requires that the archive contains all expected fields as produced by
608 a corresponding serialize() call.
610 @since ids_peak_icv 1.0
614 saturation_archive = archive.get_archive(
"Saturation")
618 color_correction_matrix_archive = archive.get_archive(
"ColorCorrection")
620 self.
matrixmatrixmatrix = np.array(color_correction_matrix_archive.get(
"Matrix", list)).reshape(3, 3)
622 version = archive.get(
"Version", int)
625 chromatic_archive = archive.get_archive(
"ChromaticAdaption")
629 chromatic_archive.get(
"Algorithm", str).upper()]
632 if chromatic_archive.has(
"ColorTemperature"):
647 Returns the final effective matrix calculated by concatenating all enabled transformations
648 (e.g., Chromatic Adaptation, Color Correction, and Saturation).
650 @return The combined effective color correction matrix.
652 @since ids_peak_icv 1.3
655 execute_and_map_return_codes(
656 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_GetEffectiveMatrix,
657 self.
_handle, ctypes.byref(c_matrix)
659 return cast(np.ndarray, np.frombuffer(memoryview(c_matrix), dtype=np.float32).reshape(3, 3).copy())
Base class for all pipeline modules.
int version(self)
Return the module version.
None _validate_version(self, IArchive archive)
bool enabled(self)
Gets whether this module is enabled.
None enabled(self, bool enabled)
Enables or disables the module.
Algorithm used for chromatic adaption.
Color spaces supported by the chromatic adaptation feature.