IDS peak ICV v1.1.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 @return The serialized module's internal state.
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 @return Module type
88
89 @since ids_peak_icv 1.0
90 """
91 return self.__class__.__name__.replace("Module", "")
92
93 def reset_to_default(self) -> None:
94 """!
95 @brief
96 Resets the sharpness level to default (0).
97
98 @since ids_peak_icv 1.0
99 """
100 self._level = self.DEFAULT_LEVEL
101
102 @property
103 def level(self) -> int:
104 """!
105 @brief
106 Gets the current sharpness level.
107
108 @return Integer level.
109
110 @since ids_peak_icv 1.0
111 """
112 return self._level
113
114 @level.setter
115 def level(self, level: int) -> None:
116 """!
117 @brief
118 Sets the sharpness level.
119
120 @param level: An integer specifying the sharpening strength.
121 @exception OutOfRangeException: The specified level is outside the valid range.
122
123 @since ids_peak_icv 1.0
124 """
125 range_ = self.level_range
126 if level < range_.minimum or level > range_.maximum:
127 raise OutOfRangeException(f"The given level value {level} is out of range!")
128 self._level = level
129
130 @property
131 def level_range(self) -> Interval:
132 """!
133 @brief
134 Gets the valid range of sharpening level values.
135
136 @return IntervalU with min and max values.
137
138 @since ids_peak_icv 1.0
139 """
140
141 c_interval = peak_common_interval_u()
142
143 execute_and_map_return_codes(
144 lib_loader.dll().peak_icv_ImageFilter_Sharpening_GetRange,
145 ctypes.pointer(c_interval)
146 )
147 return Interval(c_interval.minimum, c_interval.maximum)
148
149 def process(self, input_image: Image) -> Image:
150 """!
151 @brief
152 Apply sharpening to the specified image.
153
154 @supportedPixelformats{Sharpening}
155
156 If sharpening is disabled or the sharpening level is set to 0, the input
157 image is returned unchanged. Otherwise, this method applies an in-place
158 sharpening operation using the configured level.
159
160 @param input_image: Input image to be processed.
161 @return Processed output image.
162
163 @since ids_peak_icv 1.0
164 """
165 if not self._enabled or self.levellevellevel == 0:
166 return input_image
167
168 if input_image.pixel_format not in self.SUPPORTED_PIXEL_FORMATS:
169 return input_image
170
171 input_handle = input_image._handle
172 execute_and_map_return_codes(
173 lib_loader.dll().peak_icv_ImageFilter_Sharpening_ProcessInPlace,
174 input_handle,
175 ctypes.c_int(self.levellevellevel)
176 )
177 return input_image
Represents an interval with lower and upper boundaries.
Definition interval.py:7
Thrown when a value is outside the allowed range.
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 default (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.