IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
mono_conversion_module.py
Go to the documentation of this file.
1import ctypes
2
3from ids_peak_common.datatypes.pixelformat import PixelFormat
4from ids_peak_icv import lib_loader
6from ids_peak_icv.datatypes.image import Image
7from ids_peak_icv.backend.datatypes import peak_icv_image_converter_handle, peak_icv_image_handle
8from ids_peak_icv.backend.utils import execute_and_map_return_codes
9
10
12 """!
13 @brief
14 Mono conversion is an image pipeline module that converts images to a monochrome format.
15
16 The mono conversion module performs a mono (grayscale) conversion on input images.
17 When enabled, it converts the input image to a mono format while preserving the input bit depth.
18 If disabled, the input image is passed through unchanged.
19
20 @since ids_peak_icv 1.0
21 """
22
23 def __init__(self) -> None:
24 """!
25 @brief
26 Initializes a MonoConversionModule with default settings.
27
28 @since ids_peak_icv 1.0
29 """
30 super().__init__()
31
32 image_converter_handle = peak_icv_image_converter_handle()
33 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_ImageConverter_Create,
34 ctypes.pointer(image_converter_handle))
35
36 self._handle = image_converter_handle
37
38 def __del__(self) -> None:
39 if hasattr(self, '_handle'):
40 lib_loader.dll().peak_icv_Preprocessing_ImageConverter_Destroy(self._handle)
41
42 @property
43 def type(self) -> str:
44 """!
45 @brief
46 Returns the type of the module for serialization purposes.
47
48 @return Module type
49
50 @since ids_peak_icv 1.0
51 """
52 return "MonoConversion"
53
54 def reset_to_default(self) -> None:
55 """!
56 @brief
57 Calling this method has no effect.
58
59 @since ids_peak_icv 1.0
60 """
61 pass
62
63 def process(self, input_image: Image) -> Image:
64 """!
65 @brief
66 Converts the input image to the configured output pixel format.
67
68 @note This operation disregards any specified image regions
69 and processes the full image.
70
71 @param input_image: The input image to convert.
72
73 @return Converted image in the configured pixel format.
74
75 @since ids_peak_icv 1.0
76 """
77
79 return input_image
80
81 output_pixel_format = self._get_output_format(input_image.pixel_format)
82 if input_image.pixel_format == output_pixel_format:
83 return input_image
84
85 output_image_handle = peak_icv_image_handle()
86 execute_and_map_return_codes(
87 lib_loader.dll().peak_icv_Preprocessing_ImageConverter_Convert, self._handle, input_image._handle,
88 ctypes.c_int(
89 output_pixel_format.value),
90 ctypes.pointer(output_image_handle))
91
92 return Image(_handle=output_image_handle)
93
94 def release_buffers(self) -> None:
95 """!
96 @brief
97 Frees unused internal buffers.
98
99 This module uses pre-allocated internal buffers to accelerate conversions.
100 When the image size or pixel format changes, new buffers are allocated, which may cause
101 the internal buffer pool to grow over time. This function can be used to release
102 unused buffers and reduce memory usage.
103
104 @note Avoid calling this function too frequently (e.g., after every resize or format change),
105 as it introduces some overhead.
106
107 @since ids_peak_icv 1.0
108 """
109
110 execute_and_map_return_codes(
111 lib_loader.dll().peak_icv_Preprocessing_ImageConverter_ReleaseBuffers, self._handle)
112
113 # -------------------------------------------------------------------------
114
115 @staticmethod
116 def _get_output_format(input_pixel_format: PixelFormat) -> PixelFormat:
117
118 if (input_pixel_format.is_single_channel and input_pixel_format.has_intensity_channel):
119 return input_pixel_format
120
121 bit_depth = input_pixel_format.storage_bits_per_channel
122
123 if bit_depth <= 8:
124 return PixelFormat.MONO_8
125 if bit_depth <= 10:
126 return PixelFormat.MONO_10
127 if bit_depth <= 12:
128 return PixelFormat.MONO_12
129 return PixelFormat.MONO_16
Image Class for working with algorithms.
Definition image.py:40
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.
Mono conversion is an image pipeline module that converts images to a monochrome format.
str type(self)
Returns the type of the module for serialization purposes.
Image process(self, Image input_image)
Converts the input image to the configured output pixel format.
None __init__(self)
Initializes a MonoConversionModule with default settings.