IDS peak ICV v1.2.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
7from ids_peak_icv import lib_loader
8from ids_peak_icv.backend.datatypes import peak_icv_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
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 @return Module type.
110 @since ids_peak_icv 1.0
111 """
112 return "Gain"
113
114 def reset_to_default(self) -> None:
115 """!
116 @brief
117 Resets all gain values to defaults.
118
119 Master, Red, Green, and Blue gains are all reset to 1.0.
120 @since ids_peak_icv 1.0
121 """
122 self._master = self.DEFAULT_MASTER
123 self._red = self.DEFAULT_RED
124 self._green = self.DEFAULT_GREEN
125 self._blue = self.DEFAULT_BLUE
126
127 self._apply(_GainType.MASTER, self._master)
128 self._apply(_GainType.RED, self._red)
129 self._apply(_GainType.GREEN, self._green)
130 self._apply(_GainType.BLUE, self._blue)
131
132 @property
133 def master(self) -> float:
134 """!
135 @brief
136 Returns the current master gain value.
137 @since ids_peak_icv 1.0
138 """
139 return self._master
140
141 @master.setter
142 def master(self, value: float) -> None:
143 """!
144 @brief
145 Sets the master gain.
146
147 @param value: Gain value within the valid range returned by :py:meth:`range`.
148 @exception OutOfRangeException: If value is outside the valid range.
149 @since ids_peak_icv 1.0
150 """
151 self._master = value
152 self._apply(_GainType.MASTER, value)
153
154 @property
155 def red(self) -> float:
156 """!
157 @brief
158 Returns the current red gain value.
159 @since ids_peak_icv 1.0
160 """
161 return self._red
162
163 @red.setter
164 def red(self, value: float) -> None:
165 """!
166 @brief
167 Sets the red channel gain.
168
169 @param value: Gain value within valid range.
170 @since ids_peak_icv 1.0
171 """
172 self._red = value
173 self._apply(_GainType.RED, value)
174
175 @property
176 def green(self) -> float:
177 """!
178 @brief
179 Returns the current green gain value.
180 @since ids_peak_icv 1.0
181 """
182 return self._green
183
184 @green.setter
185 def green(self, value: float) -> None:
186 """!
187 @brief
188 Sets the green channel gain.
189
190 @param value: Gain value within valid range.
191 @since ids_peak_icv 1.0
192 """
193 self._green = value
194 self._apply(_GainType.GREEN, value)
195
196 @property
197 def blue(self) -> float:
198 """!
199 @brief
200 Returns the current blue gain value.
201 @since ids_peak_icv 1.0
202 """
203 return self._blue
204
205 @blue.setter
206 def blue(self, value: float) -> None:
207 """!
208 @brief
209 Sets the blue channel gain.
210
211 @param value: Gain value within valid range.
212 @since ids_peak_icv 1.0
213 """
214 self._blue = value
215 self._apply(_GainType.BLUE, value)
216
217 @property
218 def range(self) -> Interval:
219 """!
220 @brief
221 Returns the valid range for all gain values.
222
223 The range applies to master and per-channel gains.
224
225 @return Interval representing the valid min/max gain values.
226 @since ids_peak_icv 1.0
227 """
228
229 c_interval = peak_icv_interval_f()
230 execute_and_map_return_codes(
231 lib_loader.dll().peak_icv_Preprocessing_Gain_GetRange,
232 self._handle,
233 ctypes.pointer(c_interval)
234 )
235 return Interval(c_interval.minimum, c_interval.maximum)
236
237 def process(self, input_image: Image) -> Image:
238 """!
239 @brief
240 Processes the input image by applying gain correction (if enabled).
241
242 @supportedPixelformats{Gain}
243
244 @param input_image: Input image to be processed.
245 @return Processed output image.
246 @since ids_peak_icv 1.0
247 """
248 if not self._enabled_enabled:
249 return input_image
250
251 if input_image.pixel_format not in self.SUPPORTED_PIXEL_FORMATS:
252 return input_image
253
254 input_handle = input_image._handle
255 execute_and_map_return_codes(
256 lib_loader.dll().peak_icv_Preprocessing_Gain_ProcessInplace,
257 self._handle,
258 input_handle,
259 )
260 return input_image
261
262 def serialize(self, archive: IArchive) -> None:
263 """!
264 @brief
265 Serializes the module’s internal state into an archive.
266
267 @return Serialized archive containing all parameters.
268 @since ids_peak_icv 1.0
269 """
270 super().serialize(archive)
271 archive.set("MasterGain", self.mastermastermastermastermaster)
272 archive.set("RedGain", self.redredredredred)
273 archive.set("GreenGain", self.greengreengreengreengreen)
274 archive.set("BlueGain", self.blueblueblueblueblue)
275
276 def deserialize(self, archive: IArchive) -> None:
277 """!
278 @brief
279 Restores the module’s state from an archive.
280
281 @param archive: Archive containing the serialized state.
282 @exception CorruptedException: If the archive is malformed.
283 @exception NotSupportedException: If version is unsupported.
284
285 @since ids_peak_icv 1.0
286 """
287 super().deserialize(archive)
288
289 self.mastermastermastermastermaster = archive.get("MasterGain", float)
290 self.redredredredred = archive.get("RedGain", float)
291 self.greengreengreengreengreen = archive.get("GreenGain", float)
292 self.blueblueblueblueblue = archive.get("BlueGain", float)
293
294 def _apply(self, gain_type: _GainType, value: float) -> None:
295 execute_and_map_return_codes(
296 lib_loader.dll().peak_icv_Preprocessing_Gain_SetValue,
297 self._handle,
298 ctypes.c_int(gain_type.value),
299 ctypes.c_float(value),
300 )
301
302 def _fetch(self, gain_type: _GainType) -> float:
303 value = ctypes.c_float()
304 execute_and_map_return_codes(
305 lib_loader.dll().peak_icv_Preprocessing_Gain_GetValue,
306 self._handle,
307 ctypes.c_int(gain_type.value),
308 ctypes.pointer(value),
309 )
310 return value.value
Represents an interval with lower and upper boundaries.
Definition interval.py:7
Interface for modules that provide gain control functionality.
float red(self)
Retrieves the gain value for the red channel.
None master(self, float value)
Sets the master gain value.
float green(self)
Retrieves the gain value for the green channel.
None blue(self, float value)
Sets the gain value for the blue channel.
float blue(self)
Retrieves the gain value for the blue channel.
None red(self, float value)
Sets the gain value for the red channel.
float master(self)
Retrieves the current master gain value.
None green(self, float value)
Sets the gain value for the green channel.
Gain is an image pipeline module that applies gain correction to an image.
None reset_to_default(self)
Resets all gain values to defaults.
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