6from ids_peak_common
import Interval, PixelFormat
9from ids_peak_icv
import lib_loader
11 peak_icv_color_matrix_transformation_handle, peak_icv_interval_f)
20 Color matrix transformation is an image pipeline module that applies a color correction matrix to an image.
22 Provides the features ColorCorrectionFeature and SaturationFeature.
24 Default configuration:
25 - Color correction matrix = 3x3 identity matrix
28 @since ids_peak_icv 1.0
31 DEFAULT_MATRIX: np.ndarray = np.eye(3)
34 Default color correction matrix value.
36 @since ids_peak_icv 1.0
39 DEFAULT_SATURATION: float = 1.0
42 Default saturation value.
44 @since ids_peak_icv 1.0
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
56 Initialize a color matrix transformation module with default settings.
58 @since ids_peak_icv 1.0
71 execute_and_map_return_codes(
72 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_Create,
73 ctypes.pointer(_color_matrix_transformation_handle)
75 self.
_handle = _color_matrix_transformation_handle
78 if hasattr(self,
"_handle")
and self.
_handle:
79 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_Destroy(self.
_handle)
85 Returns the type of the module for serialization purposes.
89 @since ids_peak_icv 1.0
91 return "ColorMatrixTransformation"
97 Get whether this module is enabled.
99 When disabled, :func:`process` returns the input image unchanged.
101 @return True if the module is enabled, False otherwise.
103 @since ids_peak_icv 1.0
111 Enable or disable the module.
113 When disabled, :func:`process` returns the input image unchanged.
115 @param enabled: True to enable, False to disable.
117 @since ids_peak_icv 1.0
127 Gets the current color correction matrix.
129 @return The color correction matrix currently set.
131 @since ids_peak_icv 1.0
136 def matrix(self, matrix: np.ndarray) ->
None:
139 Sets the current color correction matrix.
141 @param matrix: The color correction matrix.
143 @since ids_peak_icv 1.0
153 Gets the current saturation value.
155 @return The saturation value currently set.
157 @since ids_peak_icv 1.0
165 Sets the current saturation value.
167 @param saturation: The saturation value.
169 @since ids_peak_icv 1.0
179 Gets the valid range for saturation.
181 @return The valid saturation range.
183 @since ids_peak_icv 1.0
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)
195 Whether color correction is enabled.
197 @return True if color correction is applied during processing.
199 @since ids_peak_icv 1.0
203 @color_correction_enabled.setter
207 Whether color correction is enabled.
209 @param enabled: True if color correction is enabled.
211 @since ids_peak_icv 1.0
221 Whether saturation is enabled.
223 @return True if saturation is applied during processing.
225 @since ids_peak_icv 1.0
229 @saturation_enabled.setter
233 Whether saturation is enabled.
235 @param enabled: True if saturation is enabled.
237 @since ids_peak_icv 1.0
242 def process(self, input_image: Image) -> Image:
245 Processes the input image and returns a color corrected and/or saturated output image.
247 @supportedPixelformats{ColorMatrixTransformation}
249 Images with other formats are passed through unmodified.
251 This operation disregards any specified image regions and processes the entire image.
253 @param input_image: The input image to be processed.
255 @return A new image with color correction and/or saturation applied.
257 @since ids_peak_icv 1.0
265 execute_and_map_return_codes(
266 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_ProcessInplace,
275 Resets all module settings.
277 Restores the module configuration to its default values:
281 @since ids_peak_icv 1.0
289 Serializes the object's internal state into the provided archive.
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.
295 @return The serialized module's internal state.
297 @since ids_peak_icv 1.0
301 saturation_archive = archive.create_archive()
304 archive.set(
"Saturation", saturation_archive)
306 color_correction_matrix_archive = archive.create_archive()
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)
314 Restores the object's state from the provided archive.
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.
320 @param archive: The source archive containing the serialized parameters.
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.
325 @note This function requires that the archive contains all expected fields as produced by
326 a corresponding serialize() call.
328 @since ids_peak_icv 1.0
332 saturation_archive = archive.get_archive(
"Saturation")
336 color_correction_matrix_archive = archive.get_archive(
"ColorCorrection")
338 self.
_matrix_matrix = np.array(color_correction_matrix_archive.get(
"Matrix", list)).reshape(3, 3)
347 c_matrix = peak_icv_color_correction_matrix.from_buffer_copy(
348 np.asarray(matrix, dtype=np.float32).flatten().tobytes())
350 execute_and_map_return_codes(
351 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_SetColorCorrectionMatrix,
358 execute_and_map_return_codes(
359 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_SetSaturation,
361 ctypes.c_float(saturation)
366 execute_and_map_return_codes(
367 lib_loader.dll().peak_icv_Preprocessing_ColorMatrixTransformation_GetEffectiveMatrix,
368 self.
_handle, ctypes.byref(c_matrix)
370 return cast(np.ndarray, np.frombuffer(memoryview(c_matrix), dtype=np.float32).reshape(3, 3).copy())
None reset_to_default(self)
Resets the module to its default state.
None enabled(self, bool value)
Sets the enabled state of the module.
bool enabled(self)
Gets whether the module is currently enabled.
Base class for all pipeline modules.
None _validate_version(self, IArchive archive)
None enabled(self, bool value)
Enable or disable the module.
bool enabled(self)
Get whether this module is enabled.