IDS peak ICV v1.2.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 The pipeline type identifier.
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.enabledenabledenabledenabledenabled 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 @ref defaults_feature_binning "default values", except the custom_type, which was set
165 upon creating the object.
166
167 @since ids_peak_icv 1.0
168 """
169 self.xxx = self.DEFAULT_X
170 self.yyy = self.DEFAULT_Y
172
173 @property
174 def x(self) -> int:
175 """!
176 @brief
177 Returns the current number of columns used for downsampling.
178
179 @return The current number of columns to downsample.
180
181 @since ids_peak_icv 1.0
182 """
183 return self._x
184
185 @x.setter
186 def x(self, value: int) -> None:
187 """!
188 @brief
189 Sets the number of columns to downsample.
190
191 @param value: The number of columns to downsample.
192
193 @since ids_peak_icv 1.0
194 """
195 factor = peak_icv_downsampling_factor(value, self.yyy)
196 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_SetFactor,
197 self._handle, factor)
198 self._x = value
199
200 @property
201 def y(self) -> int:
202 """!
203 @brief
204 Returns the current number of rows used for downsampling.
205
206 @return The current number of rows to downsample.
207
208 @since ids_peak_icv 1.0
209 """
210 return self._y
211
212 @y.setter
213 def y(self, value: int) -> None:
214 """!
215 @brief
216 Sets the number of rows to downsample.
217
218 @param value: The number of rows to downsample.
219
220 @since ids_peak_icv 1.0
221 """
222 factor = peak_icv_downsampling_factor(self.xxx, value)
223 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_SetFactor,
224 self._handle, factor)
225 self._y = value
226
227 @property
228 def mode(self) -> DownsamplingMode:
229 """!
230 @brief
231 Returns the current DownsamplingMode.
232
233 @return The current DownsamplingMode.
234
235 @since ids_peak_icv 1.0
236 """
237 return self._mode
238
239 @mode.setter
240 def mode(self, value: DownsamplingMode) -> None:
241 """!
242 @brief
243 Sets the DownsamplingMode.
244
245 @param value: The DownsamplingMode to set.
246
247 @since ids_peak_icv 1.0
248 """
249 mode = value.value
250 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_SetMode,
251 self._handle, mode)
252 self._mode = value
253
254 def serialize(self, archive: IArchive) -> None:
255 """!
256 @brief
257 Serializes the object's internal state into the provided archive.
258
259 This function populates the object into an archive with all parameters required to fully represent the current state of the object.
260 It ensures that the object can be reconstructed or transmitted accurately by saving all relevant data members
261 in a consistent and structured format.
262 @return The serialized module's internal state.
263
264 @since ids_peak_icv 1.0
265 """
266 super().serialize(archive)
267 archive.set("X", self.xxx)
268 archive.set("Y", self.yyy)
269 archive.set("Mode", self.modemodemode.name)
270 archive.set("DefaultMode", self._default_mode.name)
271
272 def deserialize(self, archive: IArchive) -> None:
273 """!
274 @brief
275 Restores the object's state from the provided archive.
276
277 This function reads and applies all necessary parameters from the given archive to reconstruct the internal state of the object.
278 It ensures that the object is restored to a valid and consistent state.
279
280 @param archive: The source archive containing the serialized parameters.
281
282 @exception CorruptedException: If the archive is malformed, misses keys or the values are invalid
283 @exception NotSupportedException: If the 'Version' entry indicates an unsupported version.
284
285 @note This function requires that the archive contains all expected fields as produced by a corresponding serialize() call.
286
287 @since ids_peak_icv 1.0
288 """
289 super().deserialize(archive)
290
291 self.xxx = archive.get("X", int)
292 self.yyy = archive.get("Y", int)
293 mode = archive.get("Mode", str)
294 default_mode = archive.get("DefaultMode", str)
295
296 try:
297 self.modemodemode = DownsamplingMode[mode]
298 self._default_mode = DownsamplingMode[default_mode]
299 except KeyError as e:
300 raise CorruptedException(
301 f"The given archive is corrupted. The value '{e.args[0]}' cannot be mapped to the enum DownsamplingMode.")
302
303 @property
304 def range(self) -> Interval:
305 """!
306 @brief
307 Returns the valid range for downsampling factors x and y.
308
309 @return The valid range.
310
311 @since ids_peak_icv 1.0
312 """
313
314 c_range = peak_common_interval_u()
315 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_GetRange,
316 self._handle, ctypes.pointer(c_range))
317
318 return Interval(c_range.minimum, c_range.maximum)
319
None enabled(self, bool value)
Sets the enabled state of the module.
Definition imodule.py:35
bool enabled(self)
Gets whether the module is currently enabled.
Definition imodule.py:22
Thrown when input data or a file is malformed, unreadable, or otherwise corrupted.
None reset_to_default(self)
Resets all settings to default values, except the custom_type, which was set upon creating the object...
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
None enabled(self, bool value)
Enable or disable the module.
bool enabled(self)
Get whether this module is enabled.