IDS peak ICV v1.1.0
Loading...
Searching...
No Matches
color_matrix_transformation_module.py
Go to the documentation of this file.
1import ctypes
2from typing import cast
3
4import numpy as np
5
6from ids_peak_common import Interval, PixelFormat
7from ids_peak_common.serialization import IArchive
9from ids_peak_icv import lib_loader
10from ids_peak_icv.backend.datatypes import (peak_icv_color_correction_matrix,
11 peak_icv_color_matrix_transformation_handle, peak_icv_interval_f)
12from ids_peak_icv.backend.utils import execute_and_map_return_codes
13from ids_peak_icv.datatypes.image import Image
15
16
18 """!
19 @brief
20 Color matrix transformation is an image pipeline module that applies a color correction matrix to an image.
21
22 Provides the features ColorCorrectionFeature and SaturationFeature.
23
24 Default configuration:
25 - Color correction matrix = 3x3 identity matrix
26 - Saturation = 1.0
27
28 @since ids_peak_icv 1.0
29 """
30
31 DEFAULT_MATRIX: np.ndarray = np.eye(3)
32 """!
33 @brief
34 Default color correction matrix value.
35
36 @since ids_peak_icv 1.0
37 """
38
39 DEFAULT_SATURATION: float = 1.0
40 """!
41 @brief
42 Default saturation value.
43
44 @since ids_peak_icv 1.0
45 """
46
47 SUPPORTED_PIXEL_FORMATS = [
48 PixelFormat.RGB_8, PixelFormat.RGB_10, PixelFormat.RGB_12, PixelFormat.BGR_8, PixelFormat.BGR_10,
49 PixelFormat.BGR_12, PixelFormat.RGBA_8, PixelFormat.RGBA_10, PixelFormat.RGBA_12, PixelFormat.BGRA_8,
50 PixelFormat.BGRA_10, PixelFormat.BGRA_12
51 ]
52
53 def __init__(self) -> None:
54 """!
55 @brief
56 Initialize a color matrix transformation module with default settings.
57
58 @since ids_peak_icv 1.0
59 """
60 super().__init__()
61
64 self._matrix_matrix: None | np.ndarray = None
65 self._saturation_saturation: float | None = None
66 self._auto_feature_module_auto_feature_module: IAutoFeature | None = None
67
69
70 _color_matrix_transformation_handle = peak_icv_color_matrix_transformation_handle()
71 execute_and_map_return_codes(
72 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_Create,
73 ctypes.pointer(_color_matrix_transformation_handle)
74 )
75 self._handle = _color_matrix_transformation_handle
76
77 def __del__(self) -> None:
78 if hasattr(self, "_handle") and self._handle:
79 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_Destroy(self._handle)
80
81 @property
82 def type(self) -> str:
83 """!
84 @brief
85 Returns the type of the module for serialization purposes.
86
87 @return Module type.
88
89 @since ids_peak_icv 1.0
90 """
91 return "ColorMatrixTransformation"
92
93 @property
94 def enabled(self) -> bool:
95 """!
96 @brief
97 Get whether this module is enabled.
98
99 When disabled, :func:`process` returns the input image unchanged.
100
101 @return True if the module is enabled, False otherwise.
102
103 @since ids_peak_icv 1.0
104 """
106
107 @enabled.setter
108 def enabled(self, enabled: bool) -> None:
109 """!
110 @brief
111 Enable or disable the module.
112
113 When disabled, :func:`process` returns the input image unchanged.
114
115 @param enabled: True to enable, False to disable.
116
117 @since ids_peak_icv 1.0
118 """
119 self._color_correction_enabled = enabled
120 self._saturation_enabled = enabled
122
123 @property
124 def matrix(self) -> np.ndarray:
125 """!
126 @brief
127 Gets the current color correction matrix.
128
129 @return The color correction matrix currently set.
130
131 @since ids_peak_icv 1.0
132 """
133 return cast(np.ndarray, self._matrix_matrix)
134
135 @matrix.setter
136 def matrix(self, matrix: np.ndarray) -> None:
137 """!
138 @brief
139 Sets the current color correction matrix.
140
141 @param matrix: The color correction matrix.
142
143 @since ids_peak_icv 1.0
144 """
145 self._matrix_matrix = matrix
146 self._apply_matrix()
148
149 @property
150 def saturation(self) -> float:
151 """!
152 @brief
153 Gets the current saturation value.
154
155 @return The saturation value currently set.
156
157 @since ids_peak_icv 1.0
158 """
159 return cast(float, self._saturation_saturation)
160
161 @saturation.setter
162 def saturation(self, saturation: float) -> None:
163 """!
164 @brief
165 Sets the current saturation value.
166
167 @param saturation: The saturation value.
168
169 @since ids_peak_icv 1.0
170 """
171 self._saturation_saturation = saturation
172 self._apply_saturation()
174
175 @property
176 def saturation_range(self) -> Interval:
177 """!
178 @brief
179 Gets the valid range for saturation.
180
181 @return The valid saturation range.
182
183 @since ids_peak_icv 1.0
184 """
185 c_interval = peak_icv_interval_f()
186 execute_and_map_return_codes(
187 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_GetSaturationRange,
188 self._handle, ctypes.pointer(c_interval))
189 return Interval(c_interval.minimum, c_interval.maximum)
190
191 @property
192 def color_correction_enabled(self) -> bool:
193 """!
194 @brief
195 Whether color correction is enabled.
196
197 @return True if color correction is applied during processing.
198
199 @since ids_peak_icv 1.0
200 """
201 return self._color_correction_enabled
202
203 @color_correction_enabled.setter
204 def color_correction_enabled(self, enabled: bool) -> None:
205 """!
206 @brief
207 Whether color correction is enabled.
208
209 @param enabled: True if color correction is enabled.
210
211 @since ids_peak_icv 1.0
212 """
213 self._color_correction_enabled = enabled
214 self._apply_matrix()
216
217 @property
218 def saturation_enabled(self) -> bool:
219 """!
220 @brief
221 Whether saturation is enabled.
222
223 @return True if saturation is applied during processing.
224
225 @since ids_peak_icv 1.0
226 """
227 return self._saturation_enabled
228
229 @saturation_enabled.setter
230 def saturation_enabled(self, enabled: bool) -> None:
231 """!
232 @brief
233 Whether saturation is enabled.
234
235 @param enabled: True if saturation is enabled.
236
237 @since ids_peak_icv 1.0
238 """
239 self._saturation_enabled = enabled
240 self._apply_saturation()
241
242 def process(self, input_image: Image) -> Image:
243 """!
244 @brief
245 Processes the input image and returns a color corrected and/or saturated output image.
246
247 @supportedPixelformats{ColorMatrixTransformation}
248
249 Images with other formats are passed through unmodified.
250
251 This operation disregards any specified image regions and processes the entire image.
252
253 @param input_image: The input image to be processed.
254
255 @return A new image with color correction and/or saturation applied.
256
257 @since ids_peak_icv 1.0
258 """
260 return input_image
261
262 if input_image.pixel_format not in self.SUPPORTED_PIXEL_FORMATS:
263 return input_image
264
265 execute_and_map_return_codes(
266 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_ProcessInplace,
267 self._handle,
268 input_image._handle
269 )
270 return input_image
271
272 def reset_to_default(self) -> None:
273 """!
274 @brief
275 Resets all module settings.
276
277 Restores the module configuration to its default values:
278 - matrix = identity
279 - saturation = 1.0
280
281 @since ids_peak_icv 1.0
282 """
285
286 def serialize(self, archive: IArchive) -> None:
287 """!
288 @brief
289 Serializes the object's internal state into the provided archive.
290
291 This function populates the given archive with all parameters required to fully represent the current state of the object.
292 It ensures that the object can be reconstructed or transmitted accurately by saving all relevant data members
293 in a consistent and structured format.
294
295 @return The serialized module's internal state.
296
297 @since ids_peak_icv 1.0
298 """
299 super().serialize(archive)
300
301 saturation_archive = archive.create_archive()
302 saturation_archive.set("Enabled", self._saturation_enabled)
303 saturation_archive.set("Value", cast(float, self._saturation_saturation))
304 archive.set("Saturation", saturation_archive)
305
306 color_correction_matrix_archive = archive.create_archive()
307 color_correction_matrix_archive.set("Enabled", self._color_correction_enabled)
308 color_correction_matrix_archive.set("Matrix", cast(list[float], cast(np.ndarray, self._matrix_matrix).flatten().tolist()))
309 archive.set("ColorCorrection", color_correction_matrix_archive)
310
311 def deserialize(self, archive: IArchive) -> None:
312 """!
313 @brief
314 Restores the object's state from the provided archive.
315
316 This function reads and applies all necessary parameters from the given archive
317 to reconstruct the internal state of the object.
318 It ensures that the object is restored to a valid and consistent state.
319
320 @param archive: The source archive containing the serialized parameters.
321
322 @exception CorruptedException: If Archive is malformed, misses keys or the values are invalid
323 @exception NotSupportedException: If the 'Version' entry indicates an unsupported version.
324
325 @note This function requires that the archive contains all expected fields as produced by
326 a corresponding serialize() call.
327
328 @since ids_peak_icv 1.0
329 """
330 super()._validate_version(archive)
331
332 saturation_archive = archive.get_archive("Saturation")
333 self._saturation_enabled = saturation_archive.get("Enabled", bool)
334 self._saturation_saturation = saturation_archive.get("Value", float)
335
336 color_correction_matrix_archive = archive.get_archive("ColorCorrection")
337 self._color_correction_enabled = color_correction_matrix_archive.get("Enabled", bool)
338 self._matrix_matrix = np.array(color_correction_matrix_archive.get("Matrix", list)).reshape(3, 3)
339
340 def _set_autofeature_module(self, auto_feature_module: IAutoFeature | None) -> None:
341 self._auto_feature_module_auto_feature_module = auto_feature_module
343
344 def _apply_matrix(self) -> None:
345 matrix = self._matrix_matrix if self._color_correction_enabled else self.DEFAULT_MATRIX
346
347 c_matrix = peak_icv_color_correction_matrix.from_buffer_copy(
348 np.asarray(matrix, dtype=np.float32).flatten().tobytes())
349
350 execute_and_map_return_codes(
351 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_SetColorCorrectionMatrix,
352 self._handle,
353 c_matrix
354 )
355
356 def _apply_saturation(self) -> None:
357 saturation = cast(float, self._saturation_saturation) if self._saturation_enabled else self.DEFAULT_SATURATION
358 execute_and_map_return_codes(
359 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_SetSaturation,
360 self._handle,
361 ctypes.c_float(saturation)
362 )
363
364 def _effective_matrix(self) -> np.ndarray:
366 execute_and_map_return_codes(
367 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_GetEffectiveMatrix,
368 self._handle, ctypes.byref(c_matrix)
369 )
370 return cast(np.ndarray, np.frombuffer(memoryview(c_matrix), dtype=np.float32).reshape(3, 3).copy())
371
374 matrix = (self._effective_matrix() if (self._saturation_enabled
375 or self._color_correction_enabled) else np.identity(3, dtype=float))
376 self._auto_feature_module_auto_feature_module.set_color_correction_matrix(matrix.flatten().tolist())
377
None reset_to_default(self)
Resets the module to its default state.
Definition imodule.py:79
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
Color matrix transformation is an image pipeline module that applies a color correction matrix to an ...
None deserialize(self, IArchive archive)
Restores the object's state from the provided archive.
Image process(self, Image input_image)
Processes the input image and returns a color corrected and/or saturated output image.
None __init__(self)
Initialize a color matrix transformation module with default settings.
None serialize(self, IArchive archive)
Serializes the object's internal state into the provided archive.
Base class for all pipeline modules.
Definition module_base.py:8
None enabled(self, bool value)
Enable or disable the module.
bool enabled(self)
Get whether this module is enabled.