4from ids_peak_common
import Size, Interval
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.
120 @return The pipeline type identifier.
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 @ref defaults_feature_binning "default values", except the custom_type, which was set
165 upon creating the object.
167 @since ids_peak_icv 1.0
177 Returns the current number of columns used for downsampling.
179 @return The current number of columns to downsample.
181 @since ids_peak_icv 1.0
186 def x(self, value: int) ->
None:
189 Sets the number of columns to downsample.
191 @param value: The number of columns to downsample.
193 @since ids_peak_icv 1.0
195 factor = peak_icv_downsampling_factor(value, self.
yyy)
196 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_SetFactor,
204 Returns the current number of rows used for downsampling.
206 @return The current number of rows to downsample.
208 @since ids_peak_icv 1.0
213 def y(self, value: int) ->
None:
216 Sets the number of rows to downsample.
218 @param value: The number of rows to downsample.
220 @since ids_peak_icv 1.0
222 factor = peak_icv_downsampling_factor(self.
xxx, value)
223 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_SetFactor,
228 def mode(self) -> DownsamplingMode:
231 Returns the current DownsamplingMode.
233 @return The current DownsamplingMode.
235 @since ids_peak_icv 1.0
240 def mode(self, value: DownsamplingMode) ->
None:
243 Sets the DownsamplingMode.
245 @param value: The DownsamplingMode to set.
247 @since ids_peak_icv 1.0
250 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_SetMode,
257 Serializes the object's internal state into the provided archive.
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.
264 @since ids_peak_icv 1.0
267 archive.set(
"X", self.
xxx)
268 archive.set(
"Y", self.
yyy)
275 Restores the object's state from the provided archive.
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.
280 @param archive: The source archive containing the serialized parameters.
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.
285 @note This function requires that the archive contains all expected fields as produced by a corresponding serialize() call.
287 @since ids_peak_icv 1.0
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)
299 except KeyError
as e:
301 f
"The given archive is corrupted. The value '{e.args[0]}' cannot be mapped to the enum DownsamplingMode.")
307 Returns the valid range for downsampling factors x and y.
309 @return The valid range.
311 @since ids_peak_icv 1.0
315 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_Downsampling_GetRange,
316 self.
_handle, ctypes.pointer(c_range))
318 return Interval(c_range.minimum, c_range.maximum)
None enabled(self, bool value)
Sets the enabled state of the module.
bool enabled(self)
Gets whether the module is currently enabled.
Thrown when input data or a file is malformed, unreadable, or otherwise corrupted.
Mode parameter for the downsampling algorithm.
Downsampling is used to decrease the image size.
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.
None enabled(self, bool value)
Enable or disable the module.
bool enabled(self)
Get whether this module is enabled.