IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
downsampling_module.py
Go to the documentation of this file.
1import ctypes
2import enum
3
4from ids_peak_common import Size, Interval
5from ids_peak_common.serialization import IArchive
6
7from ids_peak_icv.backend.datatypes import (peak_common_size, peak_icv_downsampling_factor,
8 peak_icv_downsampling_handle, peak_common_interval_u)
9from ids_peak_icv.backend.utils import execute_and_map_return_codes
10from ids_peak_icv.exceptions import CorruptedException
11from ids_peak_icv import Image, lib_loader
12from ids_peak_icv.pipeline._internal import ModuleBase
13
14
15class DownsamplingMode(enum.Enum):
16 """!
17 @brief
18 Mode parameter for the downsampling algorithm.
19
20 The enum holding the possible modes.
21
22 The enum was specifically chosen to be PascalCase as the serialization across languages
23 needs it to be in this specific case.
24
25 @ingroup ids_peak_icv_cpp_pipeline_modules
26 @since ids_peak_icv 1.0
27 """
28
29 BinningAverage = 0
30 """!
31 @brief
32 The averaged pixel values of neighboring rows and/or columns are computed during binning.
33
34 @since ids_peak_icv 1.0
35 """
36
37 BinningSum = 1
38 """!
39 @brief
40 The pixel values of neighboring rows and/or columns are summed during binning.
41
42 @since ids_peak_icv 1.0
43 """
44
45 Decimation = 2
46 """!
47 @brief
48 The additional pixel values of neighboring rows and/or columns are skipped during decimation.
49
50 @since ids_peak_icv 1.0
51 """
52
53
55 """!
56 @brief
57 Downsampling is used to decrease the image size.
58
59 This module provides comprehensive functionality for image downsampling,
60 which is the process of reducing image resolution and dimensions.
61 It supports multiple algorithms optimized for various use cases.
62
63 There are two basic techniques for downsampling:
64
65 - _Binning_
66 This method reduces resolution by summarizing or averaging groups of pixels
67 in the horizontal (x) and/or vertical (y) directions.
68 It helps preserve image quality by minimizing aliasing artifacts.
69
70 - _Decimation_
71 This method reduces resolution by selecting (i.e., skipping) pixels at regular intervals
72 along columns (x), rows (y) or both.
73 It is computationally efficient but may introduce aliasing.
74
75 The reduction factors are specified separately for the x (columns) and y (rows) directions.
76
77 @ingroup ids_peak_icv_python_pipeline_modules
78 @since ids_peak_icv 1.0
79 """
80
81 DEFAULT_X: int = 1
82 DEFAULT_Y: int = 1
83
84 def __init__(self, default_mode: DownsamplingMode, custom_type: str) -> None:
85 """!
86 @brief
87 Creates a DownsamplingModule with @ref defaults_feature_binning "default values".
88
89 @param default_mode: Override default mode, used when resetting the module to default
90 @param custom_type: Override custom mode, used when serialized to disk
91
92 @since ids_peak_icv 1.0
93 """
94 super().__init__()
95 self._x = self.DEFAULT_X
96 self._y = self.DEFAULT_Y
97 self._mode = default_mode
98 self._default_mode = default_mode
99 self._type = custom_type
100
103
104 c_downsampling_factor = peak_icv_downsampling_factor(self._x, self._y)
105 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_Create,
106 ctypes.pointer(handle), c_downsampling_factor, self._default_mode.value)
107 self._handle = handle
108
109 def __del__(self) -> None:
110 if self._handle is not None:
111 lib_loader.dll().peak_icv_Preprocessing_Downsampling_Destroy(self._handle)
112
113
114 @property
115 def type(self) -> str:
116 """!
117 @brief
118 Returns the type of the pipeline for serialization purposes.
119
120 @return Module type
121
122 @since ids_peak_icv 1.0
123 """
124 return self._type
125
126 def process(self, input_image: Image) -> Image:
127 """!
128 @brief
129 Applies the downsampling factors and mode to the input image and returns a downsampled copy based on current settings.
130
131 @supportedPixelformats{Downsampling}
132
133 @note Processes the entire image, ignoring any specified image region.
134 @note The alpha channel of the input image (if present) is also ignored.
135 This is because the alpha channel can have different interpretations
136 — such as transparency, segmentation labels, or masks —
137 each of which would require different handling during binning.
138 Consequently, the alpha channel of the output image is always set to the maximum possible pixel value.
139
140 @param input_image: The input image to be processed.
141
142 @return A new image object that is the transformed result of the input.
143
144 @exception NotSupportedException: If input has any other than the supported pixel formats.
145
146 @since ids_peak_icv 1.0
147 """
148 if not self.enabledenabledenabled or (self.xxx == 1 and self.yyy == 1):
149 return input_image
150
151 output_size = peak_common_size()
152 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_GetOutputImageSize,
153 self._handle, input_image._handle, ctypes.pointer(output_size))
154
155 output_image = Image.create_from_pixel_format_and_size(input_image.pixel_format,
156 Size(output_size.width, output_size.height))
157 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_Process,
158 self._handle, input_image._handle, output_image._handle)
159 return output_image
160
161 def reset_to_default(self) -> None:
162 """!
163 @brief
164 Resets all settings to their default values defined at initialization,
165 i.e. x = 1, y = 1, and the initial downsampling mode.
166
167 @note The enabled state and custom_type do not change when calling this function.
168
169 @since ids_peak_icv 1.0
170 """
171 self.xxx = self.DEFAULT_X
172 self.yyy = self.DEFAULT_Y
174
175 @property
176 def x(self) -> int:
177 """!
178 @brief
179 Returns the current number of columns used for downsampling.
180
181 @return The current number of columns to downsample.
182
183 @since ids_peak_icv 1.0
184 """
185 return self._x
186
187 @x.setter
188 def x(self, value: int) -> None:
189 """!
190 @brief
191 Sets the number of columns to downsample.
192
193 @param value: The number of columns to downsample.
194
195 @since ids_peak_icv 1.0
196 """
197 factor = peak_icv_downsampling_factor(value, self.yyy)
198 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_SetFactor,
199 self._handle, factor)
200 self._x = value
201
202 @property
203 def y(self) -> int:
204 """!
205 @brief
206 Returns the current number of rows used for downsampling.
207
208 @return The current number of rows to downsample.
209
210 @since ids_peak_icv 1.0
211 """
212 return self._y
213
214 @y.setter
215 def y(self, value: int) -> None:
216 """!
217 @brief
218 Sets the number of rows to downsample.
219
220 @param value: The number of rows to downsample.
221
222 @since ids_peak_icv 1.0
223 """
224 factor = peak_icv_downsampling_factor(self.xxx, value)
225 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_SetFactor,
226 self._handle, factor)
227 self._y = value
228
229 @property
230 def mode(self) -> DownsamplingMode:
231 """!
232 @brief
233 Returns the current DownsamplingMode.
234
235 @return The current DownsamplingMode.
236
237 @since ids_peak_icv 1.0
238 """
239 return self._mode
240
241 @mode.setter
242 def mode(self, value: DownsamplingMode) -> None:
243 """!
244 @brief
245 Sets the DownsamplingMode.
246
247 @param value: The DownsamplingMode to set.
248
249 @since ids_peak_icv 1.0
250 """
251 mode = value.value
252 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_SetMode,
253 self._handle, mode)
254 self._mode = value
255
256 def serialize(self, archive: IArchive) -> None:
257 """!
258 @brief
259 Serializes the object's internal state into the provided archive.
260
261 This function populates the object into an archive with all parameters required to fully represent the current state of the object.
262 It ensures that the object can be reconstructed or transmitted accurately by saving all relevant data members
263 in a consistent and structured format.
264
265 @param archive: The target archive that will store the serialized parameters.
266
267 @since ids_peak_icv 1.0
268 """
269 super().serialize(archive)
270 archive.set("X", self.xxx)
271 archive.set("Y", self.yyy)
272 archive.set("Mode", self.modemodemode.name)
273 archive.set("DefaultMode", self._default_mode.name)
274
275 def deserialize(self, archive: IArchive) -> None:
276 """!
277 @brief
278 Restores the object's state from the provided archive.
279
280 This function reads and applies all necessary parameters from the given archive to reconstruct the internal state of the object.
281 It ensures that the object is restored to a valid and consistent state.
282
283 @param archive: The source archive containing the serialized parameters.
284
285 @exception CorruptedException: If the archive is malformed, misses keys or the values are invalid
286 @exception NotSupportedException: If the 'Version' entry indicates an unsupported version.
287
288 @note This function requires that the archive contains all expected fields as produced by a corresponding serialize() call.
289
290 @since ids_peak_icv 1.0
291 """
292 super().deserialize(archive)
293
294 self.xxx = archive.get("X", int)
295 self.yyy = archive.get("Y", int)
296 mode = archive.get("Mode", str)
297 default_mode = archive.get("DefaultMode", str)
298
299 try:
300 self.modemodemode = DownsamplingMode[mode]
301 self._default_mode = DownsamplingMode[default_mode]
302 except KeyError as e:
303 raise CorruptedException(
304 f"The given archive is corrupted. The value '{e.args[0]}' cannot be mapped to the enum DownsamplingMode.")
305
306 @property
307 def range(self) -> Interval:
308 """!
309 @brief
310 Returns the valid range for downsampling factors x and y.
311
312 @return The valid range.
313
314 @since ids_peak_icv 1.0
315 """
316
317 c_range = peak_common_interval_u()
318 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_GetRange,
319 self._handle, ctypes.pointer(c_range))
320
321 return Interval(c_range.minimum, c_range.maximum)
322
Occurs when reading from a file or stream that is incomplete, damaged, or in an unexpected format.
None reset_to_default(self)
Resets all settings to their default values defined at initialization, i.e.
None deserialize(self, IArchive archive)
Restores the object's state from the provided archive.
None mode(self, DownsamplingMode value)
Sets the DownsamplingMode.
None x(self, int value)
Sets the number of columns to downsample.
None __init__(self, DownsamplingMode default_mode, str custom_type)
Creates a DownsamplingModule with default values.
str type(self)
Returns the type of the pipeline for serialization purposes.
int y(self)
Returns the current number of rows used for downsampling.
DownsamplingMode mode(self)
Returns the current DownsamplingMode.
Image process(self, Image input_image)
Applies the downsampling factors and mode to the input image and returns a downsampled copy based on ...
int x(self)
Returns the current number of columns used for downsampling.
Interval range(self)
Returns the valid range for downsampling factors x and y.
None serialize(self, IArchive archive)
Serializes the object's internal state into the provided archive.
None y(self, int value)
Sets the number of rows to downsample.
Base class for all pipeline modules.
Definition module_base.py:8
bool enabled(self)
Gets whether this module is enabled.
None enabled(self, bool enabled)
Enables or disables the module.