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