IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
gain_module.py
Go to the documentation of this file.
1import ctypes
2from enum import Enum
3
4from ids_peak_common.datatypes import Interval, PixelFormat
5from ids_peak_common.serialization import IArchive
6from ids_peak_common.pipeline.modules.igain_module import IGain
7from ids_peak_icv import lib_loader
8from ids_peak_icv.backend.datatypes import peak_common_interval_f
9from ids_peak_icv.backend.utils import execute_and_map_return_codes
11from ids_peak_icv.datatypes.image import Image
12
13
14class _GainType(Enum):
15 MASTER = 0
16 RED = 1
17 GREEN = 2
18 BLUE = 3
19
20
21class GainModule(ModuleBase, IGain):
22 """!
23 @brief
24 Gain is an image pipeline module that applies gain correction to an image.
25
26 This module adjusts the intensity or color gain of an image by modifying
27 the master and per-channel (R, G, B) gain factors. It can be used to correct
28 image brightness or color balance after acquisition.
29
30 The gain correction is applied independently to each channel, and is typically
31 used as part of the early preprocessing stages of an image pipeline.
32
33 Default configuration:
34 - Master gain = 1.0
35 - Red gain = 1.0
36 - Green gain = 1.0
37 - Blue gain = 1.0
38
39 @ingroup ids_peak_icv_python_pipeline_modules
40 @since ids_peak_icv 1.0
41 """
42
43 DEFAULT_MASTER: float = 1.0
44 """!
45 @brief
46 Default master gain value.
47
48 @since ids_peak_icv 1.0
49 """
50
51 DEFAULT_RED: float = 1.0
52 """!
53 @brief
54 Default red gain value.
55
56 @since ids_peak_icv 1.0
57 """
58
59 DEFAULT_GREEN: float = 1.0
60 """!
61 @brief
62 Default green gain value.
63
64 @since ids_peak_icv 1.0
65 """
66
67 DEFAULT_BLUE: float = 1.0
68 """!
69 @brief
70 Default blue gain value.
71
72 @since ids_peak_icv 1.0
73 """
74
75 SUPPORTED_PIXEL_FORMATS = [PixelFormat.BAYER_RG_8, PixelFormat.BAYER_RG_10, PixelFormat.BAYER_RG_12,
76 PixelFormat.BAYER_GR_8, PixelFormat.BAYER_GR_10, PixelFormat.BAYER_GR_12,
77 PixelFormat.BAYER_BG_8, PixelFormat.BAYER_BG_10, PixelFormat.BAYER_BG_12,
78 PixelFormat.BAYER_GB_8, PixelFormat.BAYER_GB_10, PixelFormat.BAYER_GB_12,
79 PixelFormat.MONO_8, PixelFormat.MONO_10, PixelFormat.MONO_12, PixelFormat.MONO_16]
80
81 def __init__(self) -> None:
82 """!
83 @brief
84 Creates an instance of the gain module.
85 @since ids_peak_icv 1.0
86 """
87 super().__init__()
88
90 self._red = self.DEFAULT_RED
92 self._blue = self.DEFAULT_BLUE
93
94 self._handle = ctypes.c_void_p()
95 execute_and_map_return_codes(
96 lib_loader.dll().peak_icv_Preprocessing_Gain_Create,
97 ctypes.byref(self._handle),
98 )
99
100 def __del__(self) -> None:
101 if getattr(self, "_handle", None):
102 lib_loader.dll().peak_icv_Preprocessing_Gain_Destroy(self._handle)
103
104 @property
105 def type(self) -> str:
106 """!
107 @brief
108 Returns the type of the module for serialization purposes.
109
110 @return Module type
111
112 @since ids_peak_icv 1.0
113 """
114 return "Gain"
115
116 def reset_to_default(self) -> None:
117 """!
118 @brief
119 Resets all gain values to their default value (1.0).
120
121 @note The enabled state does not change when calling this function.
122
123 @since ids_peak_icv 1.0
124 """
125 self._master = self.DEFAULT_MASTER
126 self._red = self.DEFAULT_RED
127 self._green = self.DEFAULT_GREEN
128 self._blue = self.DEFAULT_BLUE
129
130 self._apply(_GainType.MASTER, self._master)
131 self._apply(_GainType.RED, self._red)
132 self._apply(_GainType.GREEN, self._green)
133 self._apply(_GainType.BLUE, self._blue)
134
135 @property
136 def master(self) -> float:
137 """!
138 @brief
139 Returns the current master gain value.
140 @since ids_peak_icv 1.0
141 """
142 return self._master
143
144 @master.setter
145 def master(self, value: float) -> None:
146 """!
147 @brief
148 Sets the master gain.
149
150 @param value: Gain value within the valid range returned by :py:meth:`range`.
151 @exception OutOfRangeException: If value is outside the valid range.
152 @since ids_peak_icv 1.0
153 """
154 self._master = value
155 self._apply(_GainType.MASTER, value)
156
157 @property
158 def red(self) -> float:
159 """!
160 @brief
161 Returns the current red gain value.
162 @since ids_peak_icv 1.0
163 """
164 return self._red
165
166 @red.setter
167 def red(self, value: float) -> None:
168 """!
169 @brief
170 Sets the red channel gain.
171
172 @param value: Gain value within valid range.
173 @since ids_peak_icv 1.0
174 """
175 self._red = value
176 self._apply(_GainType.RED, value)
177
178 @property
179 def green(self) -> float:
180 """!
181 @brief
182 Returns the current green gain value.
183 @since ids_peak_icv 1.0
184 """
185 return self._green
186
187 @green.setter
188 def green(self, value: float) -> None:
189 """!
190 @brief
191 Sets the green channel gain.
192
193 @param value: Gain value within valid range.
194 @since ids_peak_icv 1.0
195 """
196 self._green = value
197 self._apply(_GainType.GREEN, value)
198
199 @property
200 def blue(self) -> float:
201 """!
202 @brief
203 Returns the current blue gain value.
204 @since ids_peak_icv 1.0
205 """
206 return self._blue
207
208 @blue.setter
209 def blue(self, value: float) -> None:
210 """!
211 @brief
212 Sets the blue channel gain.
213
214 @param value: Gain value within valid range.
215 @since ids_peak_icv 1.0
216 """
217 self._blue = value
218 self._apply(_GainType.BLUE, value)
219
220 @property
221 def range(self) -> Interval:
222 """!
223 @brief
224 Returns the valid range for all gain values.
225
226 The range applies to master and per-channel gains.
227
228 @return Interval representing the valid min/max gain values.
229 @since ids_peak_icv 1.0
230 """
231
232 c_interval = peak_common_interval_f()
233 execute_and_map_return_codes(
234 lib_loader.dll().peak_icv_Preprocessing_Gain_GetRange,
235 self._handle,
236 ctypes.pointer(c_interval)
237 )
238 return Interval(c_interval.minimum, c_interval.maximum)
239
240 def process(self, input_image: Image) -> Image:
241 """!
242 @brief
243 Processes the input image by applying gain correction (if enabled).
244
245 @supportedPixelformats{Gain}
246
247 @param input_image: Input image to be processed.
248 @return Processed output image.
249 @since ids_peak_icv 1.0
250 """
251 if not self._enabled_enabled:
252 return input_image
253
254 if input_image.pixel_format not in self.SUPPORTED_PIXEL_FORMATS:
255 return input_image
256
257 input_handle = input_image._handle
258 execute_and_map_return_codes(
259 lib_loader.dll().peak_icv_Preprocessing_Gain_ProcessInplace,
260 self._handle,
261 input_handle,
262 )
263 return input_image
264
265 def serialize(self, archive: IArchive) -> None:
266 """!
267 @brief
268 Serializes the module’s internal state into an archive.
269
270 @param archive: The target archive that will store the serialized parameters.
271
272 @since ids_peak_icv 1.0
273 """
274 super().serialize(archive)
275 archive.set("MasterGain", self.mastermastermaster)
276 archive.set("RedGain", self.redredred)
277 archive.set("GreenGain", self.greengreengreen)
278 archive.set("BlueGain", self.blueblueblue)
279
280 def deserialize(self, archive: IArchive) -> None:
281 """!
282 @brief
283 Restores the module’s state from an archive.
284
285 @param archive: Archive containing the serialized state.
286 @exception CorruptedException: If the archive is malformed.
287 @exception NotSupportedException: If version is unsupported.
288
289 @since ids_peak_icv 1.0
290 """
291 super().deserialize(archive)
292
293 self.mastermastermaster = archive.get("MasterGain", float)
294 self.redredred = archive.get("RedGain", float)
295 self.greengreengreen = archive.get("GreenGain", float)
296 self.blueblueblue = archive.get("BlueGain", float)
297
298 def _apply(self, gain_type: _GainType, value: float) -> None:
299 execute_and_map_return_codes(
300 lib_loader.dll().peak_icv_Preprocessing_Gain_SetValue,
301 self._handle,
302 ctypes.c_int(gain_type.value),
303 ctypes.c_float(value),
304 )
305
306 def _fetch(self, gain_type: _GainType) -> float:
307 value = ctypes.c_float()
308 execute_and_map_return_codes(
309 lib_loader.dll().peak_icv_Preprocessing_Gain_GetValue,
310 self._handle,
311 ctypes.c_int(gain_type.value),
312 ctypes.pointer(value),
313 )
314 return value.value
Gain is an image pipeline module that applies gain correction to an image.
None reset_to_default(self)
Resets all gain values to their default value (1.0).
float red(self)
Returns the current red gain value.
None deserialize(self, IArchive archive)
Restores the module’s state from an archive.
None master(self, float value)
Sets the master gain.
float green(self)
Returns the current green gain value.
None blue(self, float value)
Sets the blue channel gain.
None _apply(self, _GainType gain_type, float value)
str type(self)
Returns the type of the module for serialization purposes.
float blue(self)
Returns the current blue gain value.
Image process(self, Image input_image)
Processes the input image by applying gain correction (if enabled).
None __init__(self)
Creates an instance of the gain module.
None red(self, float value)
Sets the red channel gain.
Interval range(self)
Returns the valid range for all gain values.
None serialize(self, IArchive archive)
Serializes the module’s internal state into an archive.
float master(self)
Returns the current master gain value.
None green(self, float value)
Sets the green channel gain.
Base class for all pipeline modules.
Definition module_base.py:8