IDS peak ICV v1.1.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 This module allows explicit control over the output pixel format and provides
19 serialization capabilities for saving/restoring state.
20
21 Default pixel format: PixelFormat.RGB8
22
23 @since ids_peak_icv 1.0
24 """
25
26 DEFAULT_OUTPUT_PIXEL_FORMAT: PixelFormat = PixelFormat.RGB_8
27
28 def __init__(self) -> None:
29 super().__init__()
30 """!
31 @brief
32 Initializes a pixel format conversion module with default settings.
33
34 @since ids_peak_icv 1.0
35 """
36 image_converter_handle = peak_icv_image_converter_handle()
37 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_ImageConverter_Create,
38 ctypes.pointer(image_converter_handle))
39
40 self._handle = image_converter_handle
43
44 def __del__(self) -> None:
45 if hasattr(self, '_handle'):
46 lib_loader.dll().peak_icv_Preprocessing_ImageConverter_Destroy(self._handle)
47
48 @property
49 def type(self) -> str:
50 """!
51 @brief
52 Returns the type of the module (used for serialization or UI).
53
54 @return Module type.
55
56 @since ids_peak_icv 1.0
57 """
58 return "PixelFormatConversion"
59
60 def reset_to_default(self) -> None:
61 """!
62 @brief
63 Resets the output pixel format to its default value (RGB8).
64
65 @since ids_peak_icv 1.0
66 """
68
69 @property
70 def output_pixel_format(self) -> PixelFormat:
71 """!
72 @brief
73 Returns the currently set output pixel format.
74
75 @return Output pixel format.
76
77 @since ids_peak_icv 1.0
78 """
79 return self._output_pixel_format
80
81 @output_pixel_format.setter
82 def output_pixel_format(self, output_pixel_format: PixelFormat) -> None:
83 """!
84 @brief
85 Sets the desired output pixel format.
86
87 @param output_pixel_format: PixelFormat to be set as output.
88
89 @since ids_peak_icv 1.0
90 """
91 self._output_pixel_format = output_pixel_format
92
93 def process(self, input_image: Image) -> Image:
94 """!
95 @brief
96 Processes the input image, performing mono conversion if enabled.
97 This function converts the input image to a monochrome format if the module is enabled.
98 The output image preserves the bit depth of the input. If the module is disabled,
99 the input image is returned unchanged.
100
101 @param input_image: The input image to convert.
102 @return The processed image in mono format if enabled; otherwise, the original image.
103
104 @since ids_peak_icv 1.0
105 """
106
108 return input_image
109
110 if input_image.pixel_format == self._output_pixel_format:
111 return input_image
112
113 output_image_handle = peak_icv_image_handle()
114 execute_and_map_return_codes(
115 lib_loader.dll().peak_icv_Preprocessing_ImageConverter_Convert, self._handle, input_image._handle,
116 ctypes.c_int(
117 self._output_pixel_format.value),
118 ctypes.pointer(output_image_handle))
119
120 return Image(_handle=output_image_handle)
121
122 def release_buffers(self) -> None:
123 """!
124 @brief
125 Frees unused internal buffers.
126
127 This module uses pre-allocated internal buffers to accelerate conversions.
128 When the image size or pixel format changes, new buffers are allocated, which may cause
129 the internal buffer pool to grow over time. This function can be used to release
130 unused buffers and reduce memory usage.
131
132 Avoid calling this function too frequently (e.g., after every resize or format change),
133 as it introduces some overhead.
134
135 @since ids_peak_icv 1.0
136 """
137
138 execute_and_map_return_codes(
139 lib_loader.dll().peak_icv_Preprocessing_ImageConverter_ReleaseBuffers, self._handle)
140
141 # -------------------------------------------------------------------------
142
143 def serialize(self, archive: IArchive) -> None:
144 """!
145 @brief
146 Serializes the object's internal state into the provided archive.
147
148 This function populates the given archive with all parameters required to fully represent the current state of the object.
149 It ensures that the object can be reconstructed or transmitted accurately by saving all relevant data members
150 in a consistent and structured format.
151
152 @return The serialized module's internal state.
153
154 @since ids_peak_icv 1.0
155 """
156 super().serialize(archive)
157 archive.set("OutputPixelFormat", self._output_pixel_format.string_value)
158
159 def deserialize(self, archive: IArchive) -> None:
160 """!
161 @brief
162 Restores the object's state from the provided archive.
163
164 This function reads and applies all necessary parameters from the given archive to reconstruct the internal state of the object.
165 It ensures that the object is restored to a valid and consistent state.
166
167 @param archive: The source archive containing the serialized parameters.
168
169 @exception CorruptedException: If Archive is malformed, misses keys or the values are invalid
170 @exception NotSupportedException: If the 'Version' entry indicates an unsupported version.
171
172 @note This function requires that the archive contains all expected fields as produced by a corresponding serialize() call.
173
174 @since ids_peak_icv 1.0
175 """
176 super().deserialize(archive)
177
178 self._output_pixel_format = PixelFormat.create_from_string_value(archive.get("OutputPixelFormat", str))
179
180 # -------------------------------------------------------------------------
Image Class for working with algorithms.
Definition image.py:39
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 reset_to_default(self)
Resets the output pixel format to its default value (RGB8).
None deserialize(self, IArchive archive)
Restores the object's state from the provided archive.
str type(self)
Returns the type of the module (used for serialization or UI).
Image process(self, Image input_image)
Processes the input image, performing mono conversion if enabled.
None serialize(self, IArchive archive)
Serializes the object's internal state into the provided archive.