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