IDS peak ICV v1.1.0
Loading...
Searching...
No Matches
transformation_module.py
Go to the documentation of this file.
1import ctypes
2
3from ids_peak_common import PixelFormat, NotSupportedException
4from ids_peak_common.serialization import IArchive
5from ids_peak_icv import lib_loader
6from ids_peak_icv.backend.datatypes import peak_icv_preprocessing_transformation_parameters
7from ids_peak_icv.backend.utils import execute_and_map_return_codes
8from ids_peak_icv.datatypes.image import Image
10from ids_peak_icv.pipeline._internal import ModuleBase
11from ids_peak_icv.serialization.archive import Archive
12
13
15 """!
16 @brief
17 Transformation is an image pipeline module that provides mirror and rotation functionality.
18
19 Default configuration:
20 - No Rotation
21 - No Mirroring
22
23 @since ids_peak_icv 1.0
24 @ingroup ids_peak_icv_python_pipeline_modules
25 """
26
27 DEFAULT_MIRROR_LEFT_RIGHT: bool = False
28 DEFAULT_MIRROR_UP_DOWN: bool = False
29 DEFAULT_ROTATION_ANGLE: Rotation = Rotation.NO_ROTATION
30
31 def __init__(self) -> None:
32 """!
33 @brief
34 Creates a transformation module
35
36 @since ids_peak_icv 1.0
37 """
38 super().__init__()
43 self._mirror_enabled = True
44
45 @property
46 def type(self) -> str:
47 """!
48 @brief
49 Returns the type of the module for serialization purposes.
50 @return Module type
51
52 @since ids_peak_icv 1.0
53 """
54 return self.__class__.__name__
55
56 def process(self, input: Image) -> Image:
57 """!
58 @brief
59 Processes the input image and returns a mirrored and/or rotated copy based on current settings.
60
61 @supportedPixelformats{ImageTransformation}
62
63 @note When processing images with a Bayer pattern, the result may have a different pixel format,
64 though the bit depth remains unchanged.
65
66 @note This operation disregards any specified image regions and processes the entire image.
67
68 @param input: The input image to be processed.
69
70 @return A new image object that is the transformed result of the input.
71
72 @exception NotSupportedException: If the image has any other than the supported pixel formats.
73
74 @since ids_peak_icv 1.0
75 """
76 mirror_needs_processing = self._mirror_enabled and (self._mirror_left_right or self._mirror_up_down)
77 rotation_needs_processing = self._rotation_enabled and self._rotation_angle != Rotation.NO_ROTATION
78 if not mirror_needs_processing and not rotation_needs_processing:
79 return input
80
81 output_pixelformat = ctypes.c_uint32()
82 transformation_parameters = peak_icv_preprocessing_transformation_parameters(
84 self.rotation_anglerotation_anglerotation_angle.value if self._rotation_enabled else Rotation.NO_ROTATION.value
85 )
86 execute_and_map_return_codes(
87 lib_loader.dll().peak_icv_Preprocessing_Transformation_GetOutputPixelFormat,
88 input.pixel_format.value,
89 transformation_parameters,
90 ctypes.pointer(output_pixelformat),
91 )
92
93 transpose = self._rotation_enabled and (
94 self.rotation_anglerotation_anglerotation_angle == Rotation.DEGREE_90_CLOCKWISE or self.rotation_anglerotation_anglerotation_angle == Rotation.DEGREE_90_COUNTERCLOCKWISE)
95 output_size = input.size.transposed() if transpose else input.size
96 output_image = Image.create_from_pixel_format_and_size(PixelFormat(output_pixelformat.value), output_size)
97
98 execute_and_map_return_codes(
99 lib_loader.dll().peak_icv_Preprocessing_Transformation_Process,
100 input._handle,
101 transformation_parameters,
102 output_image._handle,
103 )
104
105 return output_image
106
107 def reset_to_default(self) -> None:
108 """!
109 @brief
110 Resets all settings to the default values:
111 - No Rotation
112 - No Mirroring
113
114 @since ids_peak_icv 1.0
115 """
119
120 @property
121 def mirror_left_right(self) -> bool:
122 """!
123 @brief
124 Gets the current left-right mirroring setting.
125
126 @return True if left-right mirroring is enabled so that left and right will be flipped.
127
128 @since ids_peak_icv 1.0
129 """
130 return self._mirror_left_right
131
132 @mirror_left_right.setter
133 def mirror_left_right(self, value: bool) -> None:
134 """!
135 @brief
136 Sets whether the image should be mirrored left-right.
137
138 @param value: If true, the image will be mirrored along the vertical axis.
139
140 @since ids_peak_icv 1.0
141 """
142 self._mirror_left_right = value
143
144 @property
145 def mirror_up_down(self) -> bool:
146 """!
147 @brief
148 Gets the current up-down mirroring setting.
149
150 @return True if up-down mirroring is enabled so that up and down will be flipped.
151
152 @since ids_peak_icv 1.0
153 """
154 return self._mirror_up_down
155
156 @mirror_up_down.setter
157 def mirror_up_down(self, value: bool) -> None:
158 """!
159 @brief
160 Sets whether the image should be mirrored up-down.
161
162 @param value: If true, the image will be mirrored along the horizontal axis.
163
164 @since ids_peak_icv 1.0
165 """
166 self._mirror_up_down = value
167
168 @property
169 def rotation_angle(self) -> Rotation:
170 """!
171 @brief
172 Gets the current rotation angle.
173
174 @return The currently configured rotation angle.
175
176 @since ids_peak_icv 1.0
177 """
178 return self._rotation_angle
179
180 @rotation_angle.setter
181 def rotation_angle(self, value: Rotation) -> None:
182 """!
183 @brief
184 Sets the desired rotation angle.
185
186 @param value: The rotation angle to apply (see RotationAngle enum).
187
188 @since ids_peak_icv 1.0
189 """
190 self._rotation_angle = value
191
192 @property
193 def enabled(self) -> bool:
194 """!
195 @brief
196 Checks if the module is currently enabled.
197
198 @return True if the module is enabled, false otherwise.
199
200 @since ids_peak_icv 1.0
201 """
202 return self._mirror_enabled or self._rotation_enabled
203
204 @enabled.setter
205 def enabled(self, value: bool) -> None:
206 """!
207 @brief
208 Sets the enabled state of this module.
209
210 @param value: If true, the module is enabled; otherwise, it is disabled.
211
212 @since ids_peak_icv 1.0
213 """
214 self._mirror_enabled = value
215 self._rotation_enabled = value
216
217 def serialize(self, archive: IArchive) -> None:
218 """!
219 @brief
220 Serializes the object's internal state into an archive.
221
222 This function populates an archive with all parameters required to fully represent the current state of the object.
223 It ensures that the object can be reconstructed or transmitted accurately by saving all relevant data members
224 in a consistent and structured format.
225
226 @return The serialized module's internal state.
227
228 @since ids_peak_icv 1.0
229 """
230 archive.set("Version", self.versionversionversion)
231
232 mirror_archive = Archive()
233 mirror_archive.set("Enabled", self._mirror_enabled)
234 mirror_archive.set("LeftRight", self._mirror_left_right)
235 mirror_archive.set("UpDown", self._mirror_up_down)
236 archive.set("Mirror", mirror_archive)
237
238 rotation_archive = Archive()
239 rotation_archive.set("Enabled", self._rotation_enabled)
240 rotation_archive.set("Angle", self._rotation_angle.value)
241 archive.set("Rotation", rotation_archive)
242
243 def deserialize(self, archive: IArchive) -> None:
244 """!
245 @brief
246 Restores the object's state from the provided archive.
247
248 This function reads and applies all necessary parameters from the given ``archive`` to reconstruct the internal
249 state of the object.
250 It ensures that the object is restored to a valid and consistent state.
251
252 @note This function requires that the archive contains all expected fields as produced by a
253 corresponding serialize call.
254
255 @param archive: The source archive containing the serialized parameters.
256
257 @exception CorruptedException: If Archive is malformed, misses keys or the values are invalid
258 @exception NotSupportedException: If the 'Version' entry indicates an unsupported version or if the rotation angle
259 is not transformable into RotationAngle.
260
261 @since ids_peak_icv 1.0
262 """
263 super()._validate_version(archive)
264
265 rotation_archive = archive.get_archive("Rotation")
266 self._rotation_enabled = rotation_archive.get("Enabled", bool)
267
268 mirror_archive = archive.get_archive("Mirror")
269 self._mirror_enabled = mirror_archive.get("Enabled", bool)
270 self.mirror_up_downmirror_up_downmirror_up_down = mirror_archive.get("UpDown", bool)
271 self.mirror_left_rightmirror_left_rightmirror_left_right = mirror_archive.get("LeftRight", bool)
272
273 try:
274 self.rotation_anglerotation_anglerotation_angle = Rotation(rotation_archive.get("Angle", int))
275 except ValueError as e:
276 raise NotSupportedException(str(e))
Thrown when an unsupported operation or feature is used.
Represents discrete rotation angles in degrees for use in rotation operations.
Base class for all pipeline modules.
Definition module_base.py:8
int version(self)
Return the module version.
Transformation is an image pipeline module that provides mirror and rotation functionality.
None reset_to_default(self)
Resets all settings to the default values:
None deserialize(self, IArchive archive)
Restores the object's state from the provided archive.
str type(self)
Returns the type of the module for serialization purposes.
bool mirror_left_right(self)
Gets the current left-right mirroring setting.
bool mirror_up_down(self)
Gets the current up-down mirroring setting.
None mirror_up_down(self, bool value)
Sets whether the image should be mirrored up-down.
None mirror_left_right(self, bool value)
Sets whether the image should be mirrored left-right.
None rotation_angle(self, Rotation value)
Sets the desired rotation angle.
Image process(self, Image input)
Processes the input image and returns a mirrored and/or rotated copy based on current settings.
None serialize(self, IArchive archive)
Serializes the object's internal state into an archive.
A simple key-value store with type checking, similar to a dict.
Definition archive.py:11