IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
hotpixel_correction_module.py
Go to the documentation of this file.
1from __future__ import annotations
2
3import ctypes
4from typing import TypeAlias, cast
5
6import numpy as np
7import numpy.typing as npt
8
9from ids_peak_common import Interval, Point
10from ids_peak_common.exceptions import NotSupportedException
11from ids_peak_common.serialization import IArchive
12from ids_peak_icv import lib_loader
13from ids_peak_icv.backend.datatypes import (peak_icv_hotpixel_correction_handle, peak_common_interval_u,
14 peak_icv_point)
15from ids_peak_icv.backend.utils import execute_and_map_return_codes, create_array
16from ids_peak_icv.datatypes.image import Image
17from ids_peak_icv.pipeline._internal import ModuleBase
18
19# Note: We loose type info here, as it is stored as a pointer to a python object
20PointArray: TypeAlias = npt.NDArray[np.object_]
21
22
24 """!
25 @brief
26 Hotpixel correction is an image pipeline module for detecting and correcting hot pixels in camera images.
27
28 Provides the feature HotpixelCorrectionFeature.
29
30 @since ids_peak_icv 1.0
31 """
32
33 DEFAULT_HOTPIXEL_LIST: np.ndarray = np.empty(0, dtype=Point)
34 """!
35 @brief
36 Default hotpixel list (empty list).
37
38 @since ids_peak_icv 1.0
39 """
40
41 def __init__(self) -> None:
42 """!
43 @brief
44 Creates a HotpixelCorrectionModule module with default settings (no correction).
45
46 @since ids_peak_icv 1.0
47 """
48 super().__init__()
49
52
53 _hotpixel_correction_handle = peak_icv_hotpixel_correction_handle()
54 execute_and_map_return_codes(
55 lib_loader.dll().peak_icv_Preprocessing_HotpixelCorrection_Create,
56 ctypes.pointer(_hotpixel_correction_handle)
57 )
58 self._handle = _hotpixel_correction_handle
59
60 @property
61 def type(self) -> str:
62 """!
63 @brief
64 Returns the type of the module for serialization purposes.
65
66 @return Module type
67
68 @since ids_peak_icv 1.0
69 """
70 return "HotpixelCorrection"
71
72 @property
73 def enabled(self) -> bool:
74 """!
75 @brief
76 Gets whether this module is enabled.
77
78 When disabled, :func:`process` returns the input image unchanged.
79
80 @return True if the module is enabled, False otherwise.
81
82 @since ids_peak_icv 1.0
83 """
85
86 @enabled.setter
87 def enabled(self, enabled: bool) -> None:
88 """!
89 @brief
90 Enables or disables the module.
91
92 When disabled, :func:`process` returns the input image unchanged.
93
94 @param enabled: True to enable, False to disable.
95
96 @since ids_peak_icv 1.0
97 """
98 self._hotpixel_correction_enabled = enabled
100
101 @property
102 def hotpixel_list(self) -> PointArray:
103 """!
104 @brief
105 Gets the current array of known hot pixels.
106
107 @return An array containing the positions of all configured hot pixels.
108
109 @since ids_peak_icv 1.0
110 """
111 if self._hotpixel_list_hotpixel_list is not None:
113
115
116 return cast(np.ndarray, self._hotpixel_list_hotpixel_list)
117
118 @hotpixel_list.setter
119 def hotpixel_list(self, pixel_list: PointArray) -> None:
120 """!
121 @brief
122 Sets the array of known hot pixels.
123
124 @param pixel_list: An array of hot pixel positions to use.
125
126 @since ids_peak_icv 1.0
127 """
128 if not isinstance(pixel_list, np.ndarray):
129 raise NotSupportedException(
130 "Parameter points has to be a numpy array of Point objects.")
131
132 if pixel_list.size == 0:
133 self.reset_to_default()
134 self._hotpixel_list_hotpixel_list = pixel_list
135 self._apply_pixel_list()
136
137 @property
138 def sensitivity_range(self) -> Interval:
139 """!
140 @brief
141 Gets the valid range for sensitivity.
142
143 @return The valid sensitivity range.
144
145 @since ids_peak_icv 1.0
146 """
147 c_interval = peak_common_interval_u()
148 execute_and_map_return_codes(
149 lib_loader.dll().peak_icv_Preprocessing_HotpixelCorrection_GetSensitivityRange,
150 self._handle, ctypes.pointer(c_interval))
151 return Interval(c_interval.minimum, c_interval.maximum)
152
153 def detect(self, image: Image, sensitivity: int = 3, gain_factor: float = 1.0) -> None:
154 """!
155 @brief
156 Performs immediate hot pixel detection on the given image.
157
158 This method analyzes the provided image and detects hot pixels based on the
159 sensitivity setting. The results are stored internally and can be accessed using pixel_list.
160
161 Note: Calling this function will overwrite any existing hot pixel list previously set via pixel_list.
162
163 @param image: The input image to analyze for hot pixel detection.
164 @param sensitivity: Sensitivity for hot pixel detection. A higher sensitivity
165 may result in detecting more hot pixels, including possible
166 false positives.
167 @param gain_factor: Gain factor applied to the image. Used to account for
168 increased noise levels when detecting hot pixels.
169
170 @exception NotSupportedException: If image has any other than the supported pixel formats.
171
172 @since ids_peak_icv 1.0
173 """
174 execute_and_map_return_codes(
175 lib_loader.dll().peak_icv_Preprocessing_HotpixelCorrection_Detect,
176 self._handle, image._handle, ctypes.c_size_t(sensitivity), ctypes.c_float(gain_factor))
178
179 def process(self, input_image: Image) -> Image:
180 """!
181 @brief
182 Applies hot pixel correction to the input image in place and returns a reference to the modified image.
183
184 @supportedPixelformats{HotpixelCorrection}
185
186 The correction is performed based on the current configuration of the module. The operation modifies the
187 input image directly and does not create a copy.
188
189 @supportedPixelformats{HotpixelCorrection}
190
191 Images with other formats are passed through unmodified.
192
193 This operation disregards any specified image regions and processes the entire image.
194
195 @param input_image: The input image to be processed and corrected.
196
197 @exception NotSupportedException: If image has any other than the supported pixel formats.
198
199 @return A new image with hotpixel correction applied.
200
201 @since ids_peak_icv 1.0
202 """
204 return input_image
205
207 return input_image
208
209 execute_and_map_return_codes(
210 lib_loader.dll().peak_icv_Preprocessing_HotpixelCorrection_ProcessInplace,
211 self._handle, input_image._handle)
212
213 return input_image
214
215 def reset_to_default(self) -> None:
216 """!
217 @brief
218 Resets the hotpixel list to its default value (empty list).
219
220 @note The enabled state does not change when calling this function.
221
222 @since ids_peak_icv 1.0
223 """
224 execute_and_map_return_codes(
225 lib_loader.dll().peak_icv_Preprocessing_HotpixelCorrection_ResetList,
226 self._handle)
228
229 def serialize(self, archive: IArchive) -> None:
230 """!
231 @brief
232 Serializes the object's internal state into the provided archive.
233
234 This function populates the given archive with all parameters required to fully represent the current state of the object.
235 It ensures that the object can be reconstructed or transmitted accurately by saving all relevant data members
236 in a consistent and structured format.
237
238 @param archive: The target archive that will store the serialized parameters.
239
240 @since ids_peak_icv 1.0
241 """
242
243 super().serialize(archive)
244
245 archive.set("Enabled", self._enabled_enabled)
246
247 pixel_list_archives = []
249 pixel_archive = archive.create_archive()
250 pixel_archive.set("X", pixel.x)
251 pixel_archive.set("Y", pixel.y)
252 pixel_list_archives.append(pixel_archive)
253 archive.set("HotpixelList", pixel_list_archives)
254
255 def deserialize(self, archive: IArchive) -> None:
256 """!
257 @brief
258 Restores the object's state from the provided archive.
259
260 This function reads and applies all necessary parameters from the given archive to reconstruct the internal state of the object.
261 It ensures that the object is restored to a valid and consistent state.
262
263 @param archive: The source archive containing the serialized parameters.
264
265 @exception CorruptedException: If Archive is malformed, misses keys or the values are invalid
266 @exception NotSupportedException: If the 'Version' entry indicates an unsupported version.
267
268 @note This function requires that the archive contains all expected fields as produced by a corresponding serialize() call.
269
270 @since ids_peak_icv 1.0
271 """
272
273 super().deserialize(archive)
274
275 pixel_archives = archive.get("HotpixelList", list)
276 pixel_list = []
277 for pixel_archive in pixel_archives:
278 pixel_x = pixel_archive.get("X", int)
279 pixel_y = pixel_archive.get("Y", int)
280 pixel_list.append(Point(pixel_x, pixel_y))
281
282 self._hotpixel_list_hotpixel_list = np.array(pixel_list)
283
284 def _apply_pixel_list(self) -> None:
285 pixel_list = cast(np.ndarray, self._hotpixel_list_hotpixel_list) if self._enabled_enabled else self.DEFAULT_HOTPIXEL_LIST
286
287 if pixel_list.size == 0:
288 self.reset_to_default() # reset_to_default set the internal hotpixel list to an empty list!
289 return
290
291 self._call_backend_set_list(pixel_list)
292
293 def _call_backend_set_list(self, pixel_list: npt.NDArray) -> None:
294 size = len(pixel_list)
295 c_points = create_array(peak_icv_point, size)
296
297 for i, pixel in enumerate(pixel_list):
298 c_points[i] = peak_icv_point(pixel.x, pixel.y)
299
300 execute_and_map_return_codes(
301 lib_loader.dll().peak_icv_Preprocessing_HotpixelCorrection_SetList,
302 self._handle, c_points, size
303 )
304
305 def _call_backend_get_list(self) -> None:
306 c_count = ctypes.c_size_t()
307 execute_and_map_return_codes(
308 lib_loader.dll().peak_icv_Preprocessing_HotpixelCorrection_GetList_GetCount,
309 self._handle, ctypes.pointer(c_count)
310 )
311
312 if c_count.value == 0:
314 return
315
316 c_points = create_array(peak_icv_point, c_count.value)
317 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_HotpixelCorrection_GetList,
318 self._handle, c_points, c_count)
319
320 self._hotpixel_list_hotpixel_list = np.array([Point(p.x, p.y) for p in c_points], dtype=object)
Hotpixel correction is an image pipeline module for detecting and correcting hot pixels in camera ima...
None reset_to_default(self)
Resets the hotpixel list to its default value (empty list).
None deserialize(self, IArchive archive)
Restores the object's state from the provided archive.
None hotpixel_list(self, PointArray pixel_list)
Sets the array of known hot pixels.
str type(self)
Returns the type of the module for serialization purposes.
None detect(self, Image image, int sensitivity=3, float gain_factor=1.0)
Performs immediate hot pixel detection on the given image.
Image process(self, Image input_image)
Applies hot pixel correction to the input image in place and returns a reference to the modified imag...
None __init__(self)
Creates a HotpixelCorrectionModule module with default settings (no correction).
None serialize(self, IArchive archive)
Serializes the object's internal state into the provided archive.
Base class for all pipeline modules.
Definition module_base.py:8
bool enabled(self)
Gets whether this module is enabled.
None enabled(self, bool enabled)
Enables or disables the module.