IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
sharpening_module.py
Go to the documentation of this file.
1import ctypes
2
3from ids_peak_common.serialization import IArchive
4from ids_peak_common.datatypes import Interval, PixelFormat
5from ids_peak_icv import lib_loader, Image
6from ids_peak_icv.backend.datatypes import peak_common_interval_u
7from ids_peak_icv.backend.utils import execute_and_map_return_codes
9from ids_peak_icv.exceptions import OutOfRangeException
10
11
13 """!
14 @brief
15 Sharpening is an image pipeline module that applies a sharpening filter to enhance image detail.
16
17 @since ids_peak_common 1.1
18 @ingroup ids_peak_icv_python_pipeline_modules
19 """
20
21 SUPPORTED_PIXEL_FORMATS = [PixelFormat.MONO_8, PixelFormat.MONO_10, PixelFormat.MONO_12,
22 PixelFormat.RGB_8, PixelFormat.RGB_10, PixelFormat.RGB_12,
23 PixelFormat.RGBA_8, PixelFormat.RGBA_10, PixelFormat.RGBA_12,
24 PixelFormat.BGR_8, PixelFormat.BGR_10, PixelFormat.BGR_12,
25 PixelFormat.BGRA_8, PixelFormat.BGRA_10, PixelFormat.BGRA_12]
26
27 DEFAULT_LEVEL: int = 0
28
29 _level_key = "Level"
30
31 def __init__(self) -> None:
32 """!
33 @brief
34 Creates an instance of the sharpening module.
35
36 @since ids_peak_icv 1.0
37 """
38 super().__init__()
39
41
42 def __del__(self) -> None:
43 pass
44
45 def serialize(self, archive: IArchive) -> None:
46 """!
47 @brief
48 Serializes the object's internal state into the provided archive.
49
50 This function populates the given archive with all parameters required to fully represent the current state of the object.
51 It ensures that the object can be reconstructed or transmitted accurately by saving all relevant data members
52 in a consistent and structured format.
53
54 @param archive: The target archive that will store the serialized parameters.
55
56 @since ids_peak_icv 1.0
57 """
58 super().serialize(archive)
59 archive.set(self._level_key, self.levellevellevel)
60
61 def deserialize(self, archive: IArchive) -> None:
62 """!
63 @brief
64 Restores the object's state from the provided archive.
65
66 This function reads and applies all necessary parameters from the given archive to reconstruct the internal state of the object.
67 It ensures that the object is restored to a valid and consistent state.
68
69 @param archive: The source archive containing the serialized parameters.
70
71 @exception CorruptedException: If Archive is malformed, misses keys or the values are invalid
72 @exception NotSupportedException: If the 'Version' entry indicates an unsupported version.
73
74 @note This function requires that the archive contains all expected fields as produced by a corresponding serialize() call.
75
76 @since ids_peak_icv 1.0
77 """
78
79 super().deserialize(archive)
80 self.levellevellevel = archive.get(self._level_key, int)
81
82 @property
83 def type(self) -> str:
84 """!
85 @brief
86 Returns the type of the module for serialization purposes.
87
88 @return Module type
89
90 @since ids_peak_icv 1.0
91 """
92 return self.__class__.__name__.replace("Module", "")
93
94 def reset_to_default(self) -> None:
95 """!
96 @brief
97 Resets the sharpness level to its default value (0).
98
99 @note The enabled state does not change when calling this function.
100
101 @since ids_peak_icv 1.0
102 """
103 self._level = self.DEFAULT_LEVEL
104
105 @property
106 def level(self) -> int:
107 """!
108 @brief
109 Gets the current sharpness level.
110
111 @return Integer level.
112
113 @since ids_peak_icv 1.0
114 """
115 return self._level
116
117 @level.setter
118 def level(self, level: int) -> None:
119 """!
120 @brief
121 Sets the sharpness level.
122
123 @param level: An integer specifying the sharpening strength.
124 @exception OutOfRangeException: The specified level is outside the valid range.
125
126 @since ids_peak_icv 1.0
127 """
128 range_ = self.level_range
129 if level < range_.minimum or level > range_.maximum:
130 raise OutOfRangeException(f"The given level value {level} is out of range!")
131 self._level = level
132
133 @property
134 def level_range(self) -> Interval:
135 """!
136 @brief
137 Gets the valid range of sharpening level values.
138
139 @return IntervalU with min and max values.
140
141 @since ids_peak_icv 1.0
142 """
143
144 c_interval = peak_common_interval_u()
145
146 execute_and_map_return_codes(
147 lib_loader.dll().peak_icv_ImageFilter_Sharpening_GetRange,
148 ctypes.pointer(c_interval)
149 )
150 return Interval(c_interval.minimum, c_interval.maximum)
151
152 def process(self, input_image: Image) -> Image:
153 """!
154 @brief
155 Apply sharpening to the specified image.
156
157 @supportedPixelformats{Sharpening}
158
159 If sharpening is disabled or the sharpening level is set to 0, the input
160 image is returned unchanged. Otherwise, this method applies an in-place
161 sharpening operation using the configured level.
162
163 @param input_image: Input image to be processed.
164 @return Processed output image.
165
166 @since ids_peak_icv 1.0
167 """
168 if not self._enabled or self.levellevellevel == 0:
169 return input_image
170
171 if input_image.pixel_format not in self.SUPPORTED_PIXEL_FORMATS:
172 return input_image
173
174 input_handle = input_image._handle
175 execute_and_map_return_codes(
176 lib_loader.dll().peak_icv_ImageFilter_Sharpening_ProcessInPlace,
177 input_handle,
178 ctypes.c_int(self.levellevellevel)
179 )
180 return input_image
Occurs when a provided value falls outside the allowed valid range.
Definition exceptions.py:87
Base class for all pipeline modules.
Definition module_base.py:8
Sharpening is an image pipeline module that applies a sharpening filter to enhance image detail.
None reset_to_default(self)
Resets the sharpness level to its default value (0).
None deserialize(self, IArchive archive)
Restores the object's state from the provided archive.
str type(self)
Returns the type of the module for serialization purposes.
Image process(self, Image input_image)
Apply sharpening to the specified image.
None __init__(self)
Creates an instance of the sharpening module.
None serialize(self, IArchive archive)
Serializes the object's internal state into the provided archive.
Interval level_range(self)
Gets the valid range of sharpening level values.