2from typing
import Union, cast, Sequence
13 NotSupportedException)
26from ids_peak_icv.pipeline.features
import (MirrorFeature, RotationFeature,
27 BinningFeature, DecimationFeature, GainFeature, ColorCorrectionFeature,
28 DigitalBlackFeature, GammaFeature, HotpixelCorrectionFeature,
29 SaturationFeature, SharpeningFeature)
39 Image processing pipeline for sequential transformation of raw image frames.
41 The `Pipeline` class performs a series of configurable image processing steps on raw or pre-processed image data.
42 It is designed to support a complete transformation workflow, from raw sensor input to final output format,
43 with each stage modular and optionally enabled or configured.
46 1. Unpack (Always Active) – Automatically unpacks packed sensor data formats. This stage is internal and cannot be configured.
48 2. Hot Pixel Correction – Identifies and corrects defective pixels (hot pixels).
50 3. Binning – Reduces resolution and improves signal-to-noise ratio by combining adjacent pixels.
52 4. Decimation – Downscales the image by skipping pixels in a defined pattern.
54 5. Mirror – Optionally mirrors the image left-right and/or up-down.
56 6. Rotation – Rotates the image by 90, 180, or 270 degrees.
58 7. Gain – Applies digital master gain to control image brightness, or digital color gains to adjust white balance.
60 8. Debayer (Configured Internally) - Converts Bayer pattern raw data to RGB format and Expands monochrome images
61 to RGB when a color output format is requested. This behavior is automatically determined based on the
62 input and output pixel formats.
64 9. ColorCorrection – Applies a color correction matrix.
66 10. Auto Features – Optionally applies dynamic adjustments for example auto brightness or auto white balance.
67 To enable Auto Features, an external module must be provided via::
69 auto_feature_module = AutoFeatureModule(remote_device_node_map)
70 pipeline.auto_feature_module = auto_feature_module
72 This module is part of the **Auto Feature Library (AFL)**.
74 11. Mono conversion (Configured Internally) – Converts RGB images to monochrome when a monochrome output format is requested.
76 12. Sharpening – Enhances image detail using edge-based sharpening filters.
78 13. Gamma – Applies gamma correction to adjust brightness and contrast.
80 14. Pixel Format Conversion (Configured Internally) – Converts the processed image to the desired output pixel format.
82 **Smart Pixel Format Configuration Inference**
84 The pipeline includes built-in logic to automatically configure certain internal stages based on the input and
85 output formats. For example:
87 - If a Bayer image is provided and the output format is RGB, DebayerModule "debayering" is automatically applied.
88 - If a monochrome image is input and the output format is RGB, the Debayer Module takes over the mono-to-RGB expansion.
89 - If the output format is monochrome, Mono conversion is enabled.
90 - If a Bayer pixel format is set as output format, the following features or modules are not processing:
91 Debayer, Mono conversion, Sharpening and Gamma.
92 - The Pixel Format Conversion module is responsible for converting to the specified output format.
93 If necessary, the bit depth is reduced during this step.
95 Use the following method to set the desired final pixel format::
97 pipeline.output_pixel_format(PixelFormat.RGB_8)
99 @note There might be cases where some modules, even though they are enabled, do not apply their configuration,
100 if the pixel format is not supported. For more information, have a look at the specific pipeline modules "process"
105 The pipeline can be configured in two ways:
107 Module Access: Each configurable processing module can be accessed directly, allowing individual configuration::
109 pipeline.gain.master = 1.2f
110 pipeline.gamma.value = 2.2f
112 Settings File: Load or save all configuration settings::
114 pipeline.import_settings_from_file("config.json")
115 pipeline.export_settings_to_file("config.json")
117 This modular and extensible design enables flexible integration into a variety of image processing applications,
118 from camera pipelines to offline image editing.
120 @since ids_peak_icv 1.0
121 @ingroup ids_peak_icv_python_pipeline
127 Initializes all internal modules to their default configuration.
129 @since ids_peak_icv 1.0
138 mono_conversion_module.enabled =
False
167 The pipeline version as an integer.
169 @since ids_peak_icv 1.0
177 The pipeline type identifier as a string, primarily used for serialization.
179 @since ids_peak_icv 1.0
181 return "DefaultPipeline"
183 def process(self, image: Union[Image, IImageView]) -> Image:
186 Applies the pipeline to the given image and returns the result as a new image.
188 @param image: Input image to be processed.
190 @return Processed image result.
192 @since ids_peak_icv 1.0
194 if isinstance(image, IImageView):
195 return cast(Image, super().
process(Image.create_from_image_view(image)))
197 return cast(Image, super().
process(image))
202 Converts the current pipeline settings into a string format.
204 Serializes all relevant configuration parameters of the pipeline into a single string,
205 which can be stored or transferred for later use.
210 - Creation timestamp (ISO UTC)
211 - All module settings (type + serialized data)
213 @return A string representation of the pipeline's current configuration state.
215 @since ids_peak_icv 1.0
217 from datetime
import datetime, timezone
222 "Creation": datetime.now(timezone.utc).isoformat()
229 module.serialize(data_archive)
234 modules.append(module_archive)
236 archive.set(
"Modules", cast(list[IArchive], modules))
240 serializer.write(archive, buffer)
242 return buffer.getvalue()
247 Restores pipeline settings from a previously serialized string.
249 Parses the provided string and applies the contained configuration values to
250 update the internal state of the pipeline accordingly.
253 - Validation of version (must be >= 1).
254 - Validation of pipeline type (must match this pipeline).
255 - Restoring each module's settings by invoking its `deserialize` method.
257 @param settings: A string containing the serialized pipeline configuration.
259 @exception CorruptedException: If the version or type is invalid.
260 @exception NotPossibleException: If required module settings are missing.
262 @since ids_peak_icv 1.0
267 buffer = StringIO(settings)
268 archive = deserializer.read(buffer)
270 pipeline_type = archive.get(
"Type", str)
273 f
"The settings for {pipeline_type} are not compatible with this pipeline ({self.type})."
276 version = archive.get(
"Version", int)
279 "The given version in the settings exceeds the pipeline version. "
280 f
"Given settings version: {version}, pipeline version: {self.version}. "
281 "The module version has to be >= the given pipeline settings version.")
283 module_archives = {m.get(
"Type", str): m.get_archive(
"Data")
for m
in archive.get(
"Modules", list)}
286 module_type = module.type
287 if module_type
not in module_archives:
289 f
"The settings for module '{module_type}' are missing!"
291 module.deserialize(module_archives[module_type])
296 Resets all modules in the pipeline to their default state. Also resets their enabled state.
298 @since ids_peak_icv 1.0
301 module.reset_to_default()
302 module.enabled =
True
307 def processing_policy(self) -> ProcessingPolicy:
310 The currently used processing policy.
312 @since ids_peak_icv 1.0
316 @processing_policy.setter
317 def processing_policy(self, policy: ProcessingPolicy) ->
None:
320 Defines the processing policy for the pipeline.
322 @param policy: Desired processing policy.
324 @since ids_peak_icv 1.0
334 The current output pixel format as a `PixelFormat` enum.
336 @since ids_peak_icv 1.0
340 @output_pixel_format.setter
344 Defines the desired pixel format for processed images.
346 The pipeline will convert images to this format during processing.
349 When set to a Bayer format,
350 the *ColorCorrection*, *Gamma* and *Sharpening* features
351 will not process the image because they do not support Bayer formats.
352 This may result in unexpected image quality if these features are enabled.
354 @param output_pixel_format: The new pixel format as a `PixelFormat` enum.
356 @since ids_peak_icv 1.0
358 is_bayer_format = output_pixel_format.is_single_channel
and output_pixel_format.has_channel(Channel.BAYER)
359 is_mono_format = output_pixel_format.is_single_channel
and output_pixel_format.has_intensity_channel
362 self.
_debayer_module.conversion_policy = DebayerConversionPolicy.BYPASS
365 self.
_debayer_module.conversion_policy = DebayerConversionPolicy.BAYER_ONLY
369 self.
_debayer_module.conversion_policy = DebayerConversionPolicy.BAYER_AND_MONO
372 self.
_debayer_module.conversion_policy = DebayerConversionPolicy.BAYER_AND_MONO
379 bit_depth = output_pixel_format.storage_bits_per_channel
395 Provides access to the gamma feature for brightness and contrast adjustments.
397 The gamma feature applies inverse gamma correction to adjust image luminance,
398 preparing images for linear domain processing or display.
400 @return A reference to the gamma feature.
402 @since ids_peak_icv 1.0
410 Provides access to the digital black feature for sensor offset correction.
412 The digital black feature compensates for sensor digital black offsets by subtracting a configurable
413 black level and normalizing the result.
415 @return A reference to the digital black feature.
417 @since ids_peak_icv 1.0
425 Provides access to the gain feature for brightness and white balance adjustments.
427 The gain feature allows adjustment of master gain (overall brightness) and individual
428 color channel gains (red, green, blue) for white balance correction.
430 @return A reference to the gain feature.
432 @since ids_peak_icv 1.0
440 Provides access to the color correction feature for color space transformations.
442 The color correction feature applies a 3x3 transformation matrix to perform color balancing.
444 @return A reference to the color correction feature.
446 @since ids_peak_icv 1.0
454 Provides access to the saturation feature for color intensity adjustments.
456 The saturation feature allows adjustment of color saturation levels, making colors more vivid
457 (higher saturation) or more muted (lower saturation).
459 @return A reference to the saturation feature.
461 @since ids_peak_icv 1.0
469 Provides access to the sharpening feature for image detail enhancement.
471 The sharpening feature applies edge-based sharpening filters to enhance image detail
472 and improve perceived image sharpness.
474 @return A reference to the sharpening feature.
476 @since ids_peak_icv 1.0
484 Provides access to the hot pixel correction feature for defective pixel repair.
486 The hot pixel correction feature identifies and corrects defective pixels (hot pixels)
487 in camera images by replacing them with interpolated values from neighboring pixels.
489 @return A reference to the hotpixel correction feature.
491 @since ids_peak_icv 1.0
499 Provides access to the mirror feature for image mirroring operations.
501 The mirror feature allows horizontal (left-right) and vertical (up-down) mirroring
502 of images during pipeline processing.
504 @return A reference to the mirror feature.
506 @since ids_peak_icv 1.0
514 Provides access to the rotation feature for image rotation operations.
516 The rotation feature allows rotating images in 90-degree increments (0°, 90°, 180°, 270°)
517 during pipeline processing.
519 @return A reference to the rotation feature.
521 @since ids_peak_icv 1.0
529 Provides access to the binning feature for resolution reduction and noise improvement.
531 The binning feature reduces image resolution by combining adjacent pixels through
532 averaging or summation, improving signal-to-noise ratio at the cost of resolution.
534 @return A reference to the BinningFeature.
536 @since ids_peak_icv 1.0
544 Provides access to the decimation feature for resolution reduction by pixel skipping.
546 The decimation feature reduces image resolution by skipping pixels in a defined pattern,
547 providing a faster alternative to binning for resolution reduction.
549 A reference to the DecimationFeature.
551 @return A reference to the DecimationFeature.
553 @since ids_peak_icv 1.0
565 The used auto feature module.
569 @autofeature_module.setter
573 Sets an auto feature module for automatic image adjustments.
575 These adjustments can include features such as
576 image brightness, white balance, and focus,
577 depending on the capabilities of the provided module.
580 If there is no auto feature module specified,
581 this step will be skipped during processing.
583 @param auto_feature_module: The auto feature module.
600 Retrieves the sequence of processing modules for the pipeline in execution order.
602 @return Sequence of pipeline modules.
604 @since ids_peak_icv 1.0
615 if output_pixel_format
in [PixelFormat.RGB_8, PixelFormat.RGB_10, PixelFormat.RGB_12,
616 PixelFormat.RGB_10_PACKED_32]:
617 return DebayerChannelLayout.RGB
619 if output_pixel_format
in [PixelFormat.RGBA_8, PixelFormat.RGBA_10, PixelFormat.RGBA_12]:
620 return DebayerChannelLayout.RGBA
622 if output_pixel_format
in [PixelFormat.BGR_8, PixelFormat.BGR_10, PixelFormat.BGR_12,
623 PixelFormat.BGR_10_PACKED_32]:
624 return DebayerChannelLayout.BGR
626 if output_pixel_format
in [PixelFormat.BGRA_8, PixelFormat.BGRA_10, PixelFormat.BGRA_12]:
627 return DebayerChannelLayout.BGRA
630 f
"There is not conversion to a debayer channel layout implemented for the given pixel format. "
631 f
"Given pixel format: {output_pixel_format}")
634 return (pixel_format.has_channel(Channel.RED)
and
635 pixel_format.has_channel(Channel.GREEN)
and
636 pixel_format.has_channel(Channel.BLUE))
str type(self)
Returns the type identifier of the pipeline.
Sequence[IModule] _modules(self)
Retrieves the sequence of all modules in the pipeline, in execution order.
Base class for implementing a custom pipeline.
Thrown when input data or a file is malformed, unreadable, or otherwise corrupted.
Thrown on unexpected internal system errors.
Thrown when an operation cannot be performed due to logical constraints.
Thrown when a requested operation is not implemented or supported.
Debayer is an image pipeline module that converts Bayer RAW images into standard color formats.
Downsampling is used to decrease the image size.
Gain is an image pipeline module that applies gain correction to an image.
Hotpixel correction is an image pipeline module for detecting and correcting hot pixels in camera ima...
Mono conversion is an image pipeline module that converts images to a monochrome format.
Sharpening is an image pipeline module that applies a sharpening filter to enhance image detail.
A pipeline module for applying tone curve corrections such as gamma adjustment and digital black offs...
Unpack is an image pipeline module that converts packed pixel formats into corresponding unpacked Bay...
Image processing pipeline for sequential transformation of raw image frames.
int version(self)
The pipeline version as an integer.
_tone_curve_correction_module
None reset_to_default(self)
Resets all modules in the pipeline to their default state.
None autofeature_module(self, IAutoFeature|None auto_feature_module)
Sets an auto feature module for automatic image adjustments.
bool _has_rgb_channels(self, PixelFormat pixel_format)
_hotpixel_correction_feature
DigitalBlackFeature digital_black(self)
Provides access to the digital black feature for sensor offset correction.
_hotpixel_correction_module
ColorCorrectionFeature color_correction(self)
Provides access to the color correction feature for color space transformations.
IAutoFeature|None autofeature_module(self)
The used auto feature module.
None import_settings_from_string(self, str settings)
Restores pipeline settings from a previously serialized string.
GammaFeature gamma(self)
Provides access to the gamma feature for brightness and contrast adjustments.
str type(self)
The pipeline type identifier as a string, primarily used for serialization.
_color_correction_feature
BinningFeature binning(self)
Provides access to the binning feature for resolution reduction and noise improvement.
ProcessingPolicy _processing_policy
tuple[IModule,...] _modules(self)
Retrieves the sequence of processing modules for the pipeline in execution order.
SaturationFeature saturation(self)
Provides access to the saturation feature for color intensity adjustments.
str export_settings_to_string(self)
Converts the current pipeline settings into a string format.
PixelFormat output_pixel_format(self)
The current output pixel format as a PixelFormat enum.
Image process(self, Union[Image, IImageView] image)
Applies the pipeline to the given image and returns the result as a new image.
None __init__(self)
Initializes all internal modules to their default configuration.
DebayerChannelLayout _channel_layout_from_output_pixelformat(self, PixelFormat output_pixel_format)
SharpeningFeature sharpening(self)
Provides access to the sharpening feature for image detail enhancement.
DecimationFeature decimation(self)
Provides access to the decimation feature for resolution reduction by pixel skipping.
_color_matrix_transformation_module
HotpixelCorrectionFeature hotpixel_correction(self)
Provides access to the hot pixel correction feature for defective pixel repair.
RotationFeature rotation(self)
Provides access to the rotation feature for image rotation operations.
None|IAutoFeature _auto_feature_module
MirrorFeature mirror(self)
Provides access to the mirror feature for image mirroring operations.
GainFeature gain(self)
Provides access to the gain feature for brightness and white balance adjustments.
Binning is used to decrease the image size.
Color correction applies a 3x3 color correction matrix to the image.
Decimation is used to decrease the image size.
Digital black applies digital black correction to an image.
Gain is an image pipeline feature that applies gain correction to an image.
Gamma applies an inverse gamma transformation to an image.
HotpixelCorrection detects and corrects hot pixels in camera images.
Mirror provides functionality to flip the image left-right or up-down.
Rotation provides fixed-angle rotation functionality.
Adjusts the saturation of an image by modifying a 3x3 color correction matrix.
Provides access to sharpening settings within an image processing pipeline.
A simple key-value store with type checking, similar to a dict.
A deserializer that reads serialized data and reconstructs IArchive objects.
A serializer that converts IArchive objects into a textual representation.