IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
unpack_module.py
Go to the documentation of this file.
1import ctypes
2
3from ids_peak_common.serialization import IArchive
4from ids_peak_common.datatypes.pixelformat import PixelFormat
5from ids_peak_icv import lib_loader
6from ids_peak_icv.backend.datatypes import peak_icv_image_converter_handle, peak_icv_image_handle
7from ids_peak_icv.backend.utils import execute_and_map_return_codes
9from ids_peak_icv.pipeline.processing_policy import ProcessingPolicy
10from ids_peak_icv.datatypes.image import Image
11
12
14 """!
15 @brief
16 Unpack is an image pipeline module that converts packed pixel formats into corresponding unpacked Bayer or mono formats.
17
18 The unpack module is always enabled and cannot be disabled. It converts input images
19 with packed pixel formats into their corresponding unpacked Bayer or monochrome formats.
20 The output bit depth depends on the applied processing policy and the specified target bit depth.
21
22 Default configuration:
23 - Target bit depth: 8
24 - Processing policy: ProcessingPolicy.FAST
25
26 @ingroup ids_peak_icv_python_pipeline_modules
27 @since ids_peak_icv 1.0
28 """
29
30 DEFAULT_POLICY = ProcessingPolicy.FAST
31 DEFAULT_TARGET_BIT_DEPTH = 8
32
33 def __init__(self) -> None:
34 """!
35 @brief
36 Creates an instance of class UnpackModule.
37 @since ids_peak_icv 1.0
38 """
39 super().__init__()
40
41 image_converter_handle = peak_icv_image_converter_handle()
42 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_ImageConverter_Create,
43 ctypes.pointer(image_converter_handle))
44
45 self._handle = image_converter_handle
47 self._policy: ProcessingPolicy = self.DEFAULT_POLICY
48
49 def __del__(self) -> None:
50 if hasattr(self, '_handle'):
51 lib_loader.dll().peak_icv_Preprocessing_ImageConverter_Destroy(self._handle)
52
53 @property
54 def target_bit_depth(self) -> int:
55 """!
56 @brief
57 Get the target bit depth used during image processing.
58
59 This value specifies the bit depth the module will aim for when unpacking
60 packed pixel formats. The actual output bit depth depends on the
61 selected :class:`ProcessingPolicy`.
62
63 @return The currently configured target bit depth.
64
65 @since ids_peak_icv 1.0
66 """
67 return self._target_bit_depth
68
69 @target_bit_depth.setter
70 def target_bit_depth(self, bit_depth: int) -> None:
71 """!
72 @brief
73 Set the target bit depth for image processing.
74
75 The target bit depth acts as a reference when deciding how to
76 unpack packed pixel formats. The actual resulting bit depth
77 depends on the selected :class:`ProcessingPolicy`:
78
79 - **Fast**: The output bit depth is the *minimum* of the input image bit depth
80 and the target bit depth.
81 - **Balanced**: The output bit depth is the *same* as the input image bit depth
82 (the target value is ignored).
83 - **Enhanced**: The output bit depth is the *maximum* of the input image bit depth
84 and the target bit depth.
85
86 This allows you to downsample, preserve, or enhance bit depth depending
87 on your use case.
88
89 @param bit_depth: The desired target bit depth (e.g., 8, 10, 12).
90
91 @since ids_peak_icv 1.0
92 """
93 self._target_bit_depth = bit_depth
94
95 @property
96 def processing_policy(self) -> ProcessingPolicy:
97 """!
98 @brief
99 Get the currently configured processing policy.
100 @return The active processing policy.
101
102 @since ids_peak_icv 1.0
103 """
104 return self._policy
105
106 @processing_policy.setter
107 def processing_policy(self, policy: ProcessingPolicy) -> None:
108 """!
109 @brief
110 Set the processing policy.
111 @param policy: The desired processing policy.
112
113 @since ids_peak_icv 1.0
114 """
115 self._policy = policy
116
117 @property
118 def enabled(self) -> bool:
119 """!
120 @brief
121 Indicates whether the unpack module is enabled.
122
123 This property always returns True. Setting this property has no effect.
124 @return True
125
126 @since ids_peak_icv 1.0
127 """
128 return True
129
130 @enabled.setter
131 def enabled(self, enabled: bool) -> None:
132 """!
133 @brief
134 This property cannot be changed.
135
136 The module remains enabled to guarantee correct operation of following modules.
137
138 @since ids_peak_icv 1.0
139 """
140 pass
141
142 @property
143 def type(self) -> str:
144 """!
145 @brief
146 Returns the type of the module for serialization purposes.
147
148 @return Module type
149
150 @since ids_peak_icv 1.0
151 """
152 return "Unpack"
153
154 def process(self, input_image: Image) -> Image:
155 """!
156 @brief
157 Converts a packed pixel format image into an unpacked Bayer or monochrome format.
158
159 This function unpacks the input image from a packed format to its corresponding
160 unpacked Bayer or monochrome format.
161
162 @note This operation disregards any specified image regions
163 and processes the entire image.
164
165 @param input_image: The packed input image to unpack.
166 @return The unpacked output image with the downsampled bit depth.
167
168 @since ids_peak_icv 1.0
169 """
170 input_bit_depth = input_image.pixel_format.storage_bits_per_channel
171 output_bit_depth = self._get_output_bit_depth(input_bit_depth)
172 output_pixel_format = self._get_output_pixel_format(input_image.pixel_format, output_bit_depth)
173
174 if input_image.pixel_format == output_pixel_format:
175 return input_image
176
177 output_image_handle = peak_icv_image_handle()
178
179 execute_and_map_return_codes(
180 lib_loader.dll().peak_icv_Preprocessing_ImageConverter_Convert, self._handle, input_image._handle,
181 ctypes.c_int(output_pixel_format.value),
182 ctypes.pointer(output_image_handle))
183
184 return Image(_handle=output_image_handle)
185
186 def reset_to_default(self) -> None:
187 """!
188 @brief
189 Resets all settings to their defaults.
190
191 Default configuration:
192 - target_bit_depth = 8
193 - processing_policy = ProcessingPolicy.FAST
194
195 @note The enabled state does not change when calling this function.
196
197 @since ids_peak_icv 1.0
198 """
199 self._policy = self.DEFAULT_POLICY
201
202 def release_buffers(self) -> None:
203 """!
204 @brief
205 Frees unused internal buffers.
206
207 This module uses pre-allocated internal buffers to accelerate conversions.
208 When the image size or pixel format changes, new buffers are allocated, which may cause
209 the internal buffer pool to grow over time. This function can be used to release
210 unused buffers and reduce memory usage.
211
212 @note Avoid calling this function too frequently (e.g., after every resize or format change),
213 as it introduces some overhead.
214
215 @since ids_peak_icv 1.0
216 """
217
218 execute_and_map_return_codes(
219 lib_loader.dll().peak_icv_Preprocessing_ImageConverter_ReleaseBuffers, self._handle)
220
221 def serialize(self, archive: IArchive) -> None:
222 """!
223 @brief
224 This function has no effect on the Unpack module.
225
226 @since ids_peak_icv 1.0
227 """
228 # Unlike other modules, Unpack behaves differently: it has no version,
229 # is always enabled, and therefore cannot use the base class implementation.
230 pass
231
232 def deserialize(self, archive: IArchive) -> None:
233 """!
234 @brief
235 This function has no effect on the Unpack module.
236
237 @since ids_peak_icv 1.0
238 """
239 # Unpack behaves differently from other modules. Since it is always enabled,
240 # the archive does not need to include an "enabled" entry.
241 # It also does not have a version.
242 pass
243
244 def _get_output_bit_depth(self, input_bit_depth: int) -> int:
245 if self._policy == ProcessingPolicy.FAST:
246 return min(input_bit_depth, self._target_bit_depth)
247 elif self._policy == ProcessingPolicy.ENHANCED:
248 return max(input_bit_depth, self._target_bit_depth)
249 return input_bit_depth
250
251 def _get_output_pixel_format(self, input_pixel_format: PixelFormat, output_bit_depth: int) -> PixelFormat:
252 mapping = {
253 # BayerBG
254 PixelFormat.BAYER_BG_8: self._bayer_bg_format,
255 PixelFormat.BAYER_BG_10: self._bayer_bg_format,
256 PixelFormat.BAYER_BG_10_PACKED: self._bayer_bg_format,
257 PixelFormat.BAYER_BG_10_GROUPED_40_IDS: self._bayer_bg_format,
258 PixelFormat.BAYER_BG_12: self._bayer_bg_format,
259 PixelFormat.BAYER_BG_12_PACKED: self._bayer_bg_format,
260 PixelFormat.BAYER_BG_12_GROUPED_24_IDS: self._bayer_bg_format,
261
262 # BayerGB
263 PixelFormat.BAYER_GB_8: self._bayer_gb_format,
264 PixelFormat.BAYER_GB_10: self._bayer_gb_format,
265 PixelFormat.BAYER_GB_10_PACKED: self._bayer_gb_format,
266 PixelFormat.BAYER_GB_10_GROUPED_40_IDS: self._bayer_gb_format,
267 PixelFormat.BAYER_GB_12: self._bayer_gb_format,
268 PixelFormat.BAYER_GB_12_PACKED: self._bayer_gb_format,
269 PixelFormat.BAYER_GB_12_GROUPED_24_IDS: self._bayer_gb_format,
270
271 # BayerGR
272 PixelFormat.BAYER_GR_8: self._bayer_gr_format,
273 PixelFormat.BAYER_GR_10: self._bayer_gr_format,
274 PixelFormat.BAYER_GR_10_PACKED: self._bayer_gr_format,
275 PixelFormat.BAYER_GR_10_GROUPED_40_IDS: self._bayer_gr_format,
276 PixelFormat.BAYER_GR_12: self._bayer_gr_format,
277 PixelFormat.BAYER_GR_12_PACKED: self._bayer_gr_format,
278 PixelFormat.BAYER_GR_12_GROUPED_24_IDS: self._bayer_gr_format,
279
280 # BayerRG
281 PixelFormat.BAYER_RG_8: self._bayer_rg_format,
282 PixelFormat.BAYER_RG_10: self._bayer_rg_format,
283 PixelFormat.BAYER_RG_10_PACKED: self._bayer_rg_format,
284 PixelFormat.BAYER_RG_10_GROUPED_40_IDS: self._bayer_rg_format,
285 PixelFormat.BAYER_RG_12: self._bayer_rg_format,
286 PixelFormat.BAYER_RG_12_PACKED: self._bayer_rg_format,
287 PixelFormat.BAYER_RG_12_GROUPED_24_IDS: self._bayer_rg_format,
288
289 # Mono
290 PixelFormat.MONO_8: self._mono_format,
291 PixelFormat.MONO_10: self._mono_format,
292 PixelFormat.MONO_10_PACKED: self._mono_format,
293 PixelFormat.MONO_10_GROUPED_40_IDS: self._mono_format,
294 PixelFormat.MONO_12: self._mono_format,
295 PixelFormat.MONO_12_PACKED: self._mono_format,
296 PixelFormat.MONO_12_GROUPED_24_IDS: self._mono_format,
297
298 # Color
299 PixelFormat.RGB_10_PACKED_32: lambda _: PixelFormat.RGB_10,
300 PixelFormat.BGR_10_PACKED_32: lambda _: PixelFormat.BGR_10,
301
302 # YUV
303 PixelFormat.YUV420_8_YY_UV_SEMIPLANAR_IDS: lambda _: PixelFormat.RGB_8,
304 PixelFormat.YUV420_8_YY_VU_SEMIPLANAR_IDS: lambda _: PixelFormat.RGB_8,
305 PixelFormat.YUV422_8_UYVY: lambda _: PixelFormat.RGB_8,
306
307 }
308
309 func = mapping.get(input_pixel_format)
310 if func is None:
311 return input_pixel_format
312 return func(output_bit_depth)
313
314 @staticmethod
315 def _bayer_bg_format(bit_depth: int) -> PixelFormat:
316 if bit_depth >= 12:
317 return PixelFormat.BAYER_BG_12
318 elif bit_depth >= 10:
319 return PixelFormat.BAYER_BG_10
320 return PixelFormat.BAYER_BG_8
321
322 @staticmethod
323 def _bayer_gb_format(bit_depth: int) -> PixelFormat:
324 if bit_depth >= 12:
325 return PixelFormat.BAYER_GB_12
326 elif bit_depth >= 10:
327 return PixelFormat.BAYER_GB_10
328 return PixelFormat.BAYER_GB_8
329
330 @staticmethod
331 def _bayer_gr_format(bit_depth: int) -> PixelFormat:
332 if bit_depth >= 12:
333 return PixelFormat.BAYER_GR_12
334 elif bit_depth >= 10:
335 return PixelFormat.BAYER_GR_10
336 return PixelFormat.BAYER_GR_8
337
338 @staticmethod
339 def _bayer_rg_format(bit_depth: int) -> PixelFormat:
340 if bit_depth >= 12:
341 return PixelFormat.BAYER_RG_12
342 elif bit_depth >= 10:
343 return PixelFormat.BAYER_RG_10
344 return PixelFormat.BAYER_RG_8
345
346 @staticmethod
347 def _mono_format(bit_depth: int) -> PixelFormat:
348 if bit_depth >= 12:
349 return PixelFormat.MONO_12
350 elif bit_depth >= 10:
351 return PixelFormat.MONO_10
352 return PixelFormat.MONO_8
Image Class for working with algorithms.
Definition image.py:40
Base class for all pipeline modules.
Definition module_base.py:8
Unpack is an image pipeline module that converts packed pixel formats into corresponding unpacked Bay...
None reset_to_default(self)
Resets all settings to their defaults.
None release_buffers(self)
Frees unused internal buffers.
None deserialize(self, IArchive archive)
This function has no effect on the Unpack module.
str type(self)
Returns the type of the module for serialization purposes.
PixelFormat _get_output_pixel_format(self, PixelFormat input_pixel_format, int output_bit_depth)
int target_bit_depth(self)
Get the target bit depth used during image processing.
Image process(self, Image input_image)
Converts a packed pixel format image into an unpacked Bayer or monochrome format.
None __init__(self)
Creates an instance of class UnpackModule.
None serialize(self, IArchive archive)
This function has no effect on the Unpack module.