4from ids_peak_common
import Size, Interval
5from ids_peak_common.serialization
import IArchive
8 peak_icv_downsampling_handle, peak_common_interval_u)
11from ids_peak_icv
import Image, lib_loader
18 Mode parameter for the downsampling algorithm.
20 The enum holding the possible modes.
22 The enum was specifically chosen to be PascalCase as the serialization across languages
23 needs it to be in this specific case.
25 @ingroup ids_peak_icv_cpp_pipeline_modules
26 @since ids_peak_icv 1.0
32 The averaged pixel values of neighboring rows and/or columns are computed during binning.
34 @since ids_peak_icv 1.0
40 The pixel values of neighboring rows and/or columns are summed during binning.
42 @since ids_peak_icv 1.0
48 The additional pixel values of neighboring rows and/or columns are skipped during decimation.
50 @since ids_peak_icv 1.0
57 Downsampling is used to decrease the image size.
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.
63 There are two basic techniques for downsampling:
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.
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.
75 The reduction factors are specified separately for the x (columns) and y (rows) directions.
77 @ingroup ids_peak_icv_python_pipeline_modules
78 @since ids_peak_icv 1.0
84 def __init__(self, default_mode: DownsamplingMode, custom_type: str) ->
None:
87 Creates a DownsamplingModule with @ref defaults_feature_binning "default values".
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
92 @since ids_peak_icv 1.0
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)
111 lib_loader.dll().peak_icv_Preprocessing_Downsampling_Destroy(self.
_handle)
118 Returns the type of the pipeline for serialization purposes.
122 @since ids_peak_icv 1.0
126 def process(self, input_image: Image) -> Image:
129 Applies the downsampling factors and mode to the input image and returns a downsampled copy based on current settings.
131 @supportedPixelformats{Downsampling}
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.
140 @param input_image: The input image to be processed.
142 @return A new image object that is the transformed result of the input.
144 @exception NotSupportedException: If input has any other than the supported pixel formats.
146 @since ids_peak_icv 1.0
152 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_GetOutputImageSize,
153 self.
_handle, input_image._handle, ctypes.pointer(output_size))
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)
164 Resets all settings to their default values defined at initialization,
165 i.e. x = 1, y = 1, and the initial downsampling mode.
167 @note The enabled state and custom_type do not change when calling this function.
169 @since ids_peak_icv 1.0
179 Returns the current number of columns used for downsampling.
181 @return The current number of columns to downsample.
183 @since ids_peak_icv 1.0
188 def x(self, value: int) ->
None:
191 Sets the number of columns to downsample.
193 @param value: The number of columns to downsample.
195 @since ids_peak_icv 1.0
197 factor = peak_icv_downsampling_factor(value, self.
yyy)
198 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_SetFactor,
206 Returns the current number of rows used for downsampling.
208 @return The current number of rows to downsample.
210 @since ids_peak_icv 1.0
215 def y(self, value: int) ->
None:
218 Sets the number of rows to downsample.
220 @param value: The number of rows to downsample.
222 @since ids_peak_icv 1.0
224 factor = peak_icv_downsampling_factor(self.
xxx, value)
225 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_SetFactor,
230 def mode(self) -> DownsamplingMode:
233 Returns the current DownsamplingMode.
235 @return The current DownsamplingMode.
237 @since ids_peak_icv 1.0
242 def mode(self, value: DownsamplingMode) ->
None:
245 Sets the DownsamplingMode.
247 @param value: The DownsamplingMode to set.
249 @since ids_peak_icv 1.0
252 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_SetMode,
259 Serializes the object's internal state into the provided archive.
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.
265 @param archive: The target archive that will store the serialized parameters.
267 @since ids_peak_icv 1.0
270 archive.set(
"X", self.
xxx)
271 archive.set(
"Y", self.
yyy)
278 Restores the object's state from the provided archive.
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.
283 @param archive: The source archive containing the serialized parameters.
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.
288 @note This function requires that the archive contains all expected fields as produced by a corresponding serialize() call.
290 @since ids_peak_icv 1.0
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)
302 except KeyError
as e:
304 f
"The given archive is corrupted. The value '{e.args[0]}' cannot be mapped to the enum DownsamplingMode.")
310 Returns the valid range for downsampling factors x and y.
312 @return The valid range.
314 @since ids_peak_icv 1.0
318 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_GetRange,
319 self.
_handle, ctypes.pointer(c_range))
321 return Interval(c_range.minimum, c_range.maximum)
Occurs when reading from a file or stream that is incomplete, damaged, or in an unexpected format.
Mode parameter for the downsampling algorithm.
Downsampling is used to decrease the image size.
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.
bool enabled(self)
Gets whether this module is enabled.
None enabled(self, bool enabled)
Enables or disables the module.