IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
pixelformat_conversion_module.py
Go to the documentation of this file.
1import ctypes
2
3from ids_peak_common.datatypes.pixelformat import PixelFormat
4from ids_peak_common.serialization import IArchive
5from ids_peak_icv import lib_loader
6from ids_peak_icv.backend.datatypes import (peak_icv_image_converter_handle,
7 peak_icv_image_handle)
8from ids_peak_icv.backend.utils import execute_and_map_return_codes
9from ids_peak_icv.datatypes.image import Image
11
12
14 """!
15 @brief
16 Pixel format conversion module that transforms images to a specified output pixel format.
17
18 The pixel format conversion module allows explicit control over the output pixel format.
19
20 \defaults{defaults_module_pixelformat_conversion_py|
21 - PixelFormat.RGB_8
22 }
23
24 @since ids_peak_icv 1.0
25
26 @ingroup ids_peak_icv_python_pipeline_modules
27 """
28
29 DEFAULT_OUTPUT_PIXEL_FORMAT: PixelFormat = PixelFormat.RGB_8
30
31 def __init__(self) -> None:
32 """!
33 @brief
34 Initializes a pixel format conversion module
35 with \ref defaults_module_pixelformat_conversion_py "default output pixelformat".
36
37 @since ids_peak_icv 1.0
38 """
39 super().__init__()
40 image_converter_handle = peak_icv_image_converter_handle()
41 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_ImageConverter_Create,
42 ctypes.pointer(image_converter_handle))
43
44 self._handle = image_converter_handle
47
48 def __del__(self) -> None:
49 if hasattr(self, '_handle'):
50 lib_loader.dll().peak_icv_Preprocessing_ImageConverter_Destroy(self._handle)
51
52 @property
53 def type(self) -> str:
54 """!
55 @brief
56 Returns the type of the module for serialization purposes.
57
58 @return Module type
59
60 @since ids_peak_icv 1.0
61 """
62 return "PixelFormatConversion"
63
64 def reset_to_default(self) -> None:
65 """!
66 @brief
67 Resets the output pixel format
68 to its \ref defaults_module_pixelformat_conversion_py "default value".
69
70 @note The enabled state does not change when calling this function.
71
72 @since ids_peak_icv 1.0
73 """
75
76 @property
77 def output_pixel_format(self) -> PixelFormat:
78 """!
79 @brief
80 Returns the currently set output pixel format.
81
82 @return The current output pixel format.
83
84 @since ids_peak_icv 1.0
85 """
86 return self._output_pixel_format
87
88 @output_pixel_format.setter
89 def output_pixel_format(self, output_pixel_format: PixelFormat) -> None:
90 """!
91 @brief
92 Sets the desired output pixel format.
93
94 @param output_pixel_format: The desired output pixel format.
95
96 @since ids_peak_icv 1.0
97 """
98 self._output_pixel_format = output_pixel_format
99
100 def process(self, input_image: Image) -> Image:
101 """!
102 @brief
103 Converts the input image to the configured output pixel format.
104
105 @note This operation disregards any specified image regions and processes the entire image.
106
107 @param input_image: The input image to convert.
108
109 @return The converted image in the specified pixel format.
110
111 @since ids_peak_icv 1.0
112 """
113
115 return input_image
116
117 if input_image.pixel_format == self._output_pixel_format:
118 return input_image
119
120 output_image_handle = peak_icv_image_handle()
121 execute_and_map_return_codes(
122 lib_loader.dll().peak_icv_Preprocessing_ImageConverter_Convert, self._handle, input_image._handle,
123 ctypes.c_int(
124 self._output_pixel_format.value),
125 ctypes.pointer(output_image_handle))
126
127 return Image(_handle=output_image_handle)
128
129 def release_buffers(self) -> None:
130 """!
131 @brief
132 Frees unused internal buffers.
133
134 This module uses pre-allocated internal buffers to accelerate conversions.
135 When the image size or pixel format changes, new buffers are allocated, which may cause
136 the internal buffer pool to grow over time. This function can be used to release
137 unused buffers and reduce memory usage.
138
139 @note Avoid calling this function too frequently (e.g., after every resize or format change),
140 as it introduces some overhead.
141
142 @since ids_peak_icv 1.0
143 """
144
145 execute_and_map_return_codes(
146 lib_loader.dll().peak_icv_Preprocessing_ImageConverter_ReleaseBuffers, self._handle)
147
148 # -------------------------------------------------------------------------
149
150 def serialize(self, archive: IArchive) -> None:
151 """!
152 @brief
153 Serializes the object's internal state into the provided archive.
154
155 This function populates the given archive with all parameters required to fully represent the current state of the object.
156 It ensures that the object can be reconstructed or transmitted accurately by saving all relevant data members
157 in a consistent and structured format.
158
159 @param archive: The target archive that will store the serialized parameters.
160
161 @since ids_peak_icv 1.0
162 """
163 super().serialize(archive)
164 archive.set("OutputPixelFormat", self._output_pixel_format.string_value)
165
166 def deserialize(self, archive: IArchive) -> None:
167 """!
168 @brief
169 Restores the object's state from the provided archive.
170
171 This function reads and applies all necessary parameters from the given archive to reconstruct the internal state of the object.
172 It ensures that the object is restored to a valid and consistent state.
173
174 @param archive: The source archive containing the serialized parameters.
175
176 @exception CorruptedException: If Archive is malformed, misses keys or the values are invalid
177 @exception NotSupportedException: If the 'Version' entry indicates an unsupported version.
178
179 @note This function requires that the archive contains all expected fields as produced by a corresponding serialize() call.
180
181 @since ids_peak_icv 1.0
182 """
183 super().deserialize(archive)
184
185 self._output_pixel_format = PixelFormat.create_from_string_value(archive.get("OutputPixelFormat", str))
186
187 # -------------------------------------------------------------------------
Image Class for working with algorithms.
Definition image.py:40
Base class for all pipeline modules.
Definition module_base.py:8
Pixel format conversion module that transforms images to a specified output pixel format.
None deserialize(self, IArchive archive)
Restores the object's state from the provided archive.
Image process(self, Image input_image)
Converts the input image to the configured output pixel format.
None __init__(self)
Initializes a pixel format conversion module with default output pixelformat.
None serialize(self, IArchive archive)
Serializes the object's internal state into the provided archive.