IDS peak ICV v1.1.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 Enable or disable 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):
130 "Parameter points has to be a numpy array of Point objects.")
131
132 if pixel_list.size == 0:
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 all module settings.
219
220 Restores the module configuration to its default values:
221 - hotpixel_list = empty np.ndarray of type Point
222
223 @since ids_peak_icv 1.0
224 """
225 execute_and_map_return_codes(
226 lib_loader.dll().peak_icv_Preprocessing_HotpixelCorrection_ResetList,
227 self._handle)
229
230 def serialize(self, archive: IArchive) -> None:
231 """!
232 @brief
233 Serializes the object's internal state into the provided archive.
234
235 This function populates the given archive with all parameters required to fully represent the current state of the object.
236 It ensures that the object can be reconstructed or transmitted accurately by saving all relevant data members
237 in a consistent and structured format.
238
239 @return The serialized module's internal state.
240
241 @since ids_peak_icv 1.0
242 """
243
244 super().serialize(archive)
245
246 archive.set("Enabled", self._enabled_enabled)
247
248 pixel_list_archives = []
250 pixel_archive = archive.create_archive()
251 pixel_archive.set("X", pixel.x)
252 pixel_archive.set("Y", pixel.y)
253 pixel_list_archives.append(pixel_archive)
254 archive.set("HotpixelList", pixel_list_archives)
255
256 def deserialize(self, archive: IArchive) -> None:
257 """!
258 @brief
259 Restores the object's state from the provided archive.
260
261 This function reads and applies all necessary parameters from the given archive to reconstruct the internal state of the object.
262 It ensures that the object is restored to a valid and consistent state.
263
264 @param archive: The source archive containing the serialized parameters.
265
266 @exception CorruptedException: If Archive is malformed, misses keys or the values are invalid
267 @exception NotSupportedException: If the 'Version' entry indicates an unsupported version.
268
269 @note This function requires that the archive contains all expected fields as produced by a corresponding serialize() call.
270
271 @since ids_peak_icv 1.0
272 """
273
274 super().deserialize(archive)
275
276 pixel_archives = archive.get("HotpixelList", list)
277 pixel_list = []
278 for pixel_archive in pixel_archives:
279 pixel_x = pixel_archive.get("X", int)
280 pixel_y = pixel_archive.get("Y", int)
281 pixel_list.append(Point(pixel_x, pixel_y))
282
283 self._hotpixel_list_hotpixel_list = np.array(pixel_list)
284
285 def _apply_pixel_list(self) -> None:
286 pixel_list = cast(np.ndarray, self._hotpixel_list_hotpixel_list) if self._enabled_enabled else self.DEFAULT_HOTPIXEL_LIST
287
288 if pixel_list.size == 0:
289 self.reset_to_defaultreset_to_default() # reset_to_default set the internal hotpixel list to an empty list!
290 return
291
292 self._call_backend_set_list(pixel_list)
293
294 def _call_backend_set_list(self, pixel_list: npt.NDArray) -> None:
295 size = len(pixel_list)
296 c_points = create_array(peak_icv_point, size)
297
298 for i, pixel in enumerate(pixel_list):
299 c_points[i] = peak_icv_point(pixel.x, pixel.y)
300
301 execute_and_map_return_codes(
302 lib_loader.dll().peak_icv_Preprocessing_HotpixelCorrection_SetList,
303 self._handle, c_points, size
304 )
305
306 def _call_backend_get_list(self) -> None:
307 c_count = ctypes.c_size_t()
308 execute_and_map_return_codes(
309 lib_loader.dll().peak_icv_Preprocessing_HotpixelCorrection_GetList_GetCount,
310 self._handle, ctypes.pointer(c_count)
311 )
312
313 if c_count.value == 0:
315 return
316
317 c_points = create_array(peak_icv_point, c_count.value)
318 execute_and_map_return_codes(lib_loader.dll().peak_icv_Preprocessing_HotpixelCorrection_GetList,
319 self._handle, c_points, c_count)
320
321 self._hotpixel_list_hotpixel_list = np.array([Point(p.x, p.y) for p in c_points], dtype=object)
Thrown when an unsupported operation or feature is used.
None reset_to_default(self)
Resets the module to its default state.
Definition imodule.py:79
None enabled(self, bool value)
Sets the enabled state of the module.
Definition imodule.py:35
bool enabled(self)
Gets whether the module is currently enabled.
Definition imodule.py:22
Hotpixel correction is an image pipeline module for detecting and correcting hot pixels in camera ima...
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
None enabled(self, bool value)
Enable or disable the module.
bool enabled(self)
Get whether this module is enabled.