IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
median_filter.py
Go to the documentation of this file.
1import ctypes
2from typing import Union
3
4from ids_peak_icv import lib_loader
5from ids_peak_icv.backend.utils import execute_and_map_return_codes
6from ids_peak_icv.datatypes.image import Image, _check_and_get_icv_image
7from ids_peak_common.datatypes.geometry.size import Size
8from ids_peak_ipl import ids_peak_ipl
9
10
12 """!
13 @brief
14 Class for applying a median filter to an image.
15
16 The median filter is a non-linear, spatial filter
17 used to reduce impulsive noise in images.
18 It replaces each pixel
19 with the median value
20 of the pixels in its local neighborhood,
21 defined by the specified kernel size.
22 The kernel size corresponds to a square neighborhood
23 (size x size grid) around each pixel,
24 where the median is calculated.
25
26 This process preserves edges better than linear smoothing filters
27 while effectively removing salt-and-pepper noise
28 and small image artifacts.
29 A larger kernel removes more noise
30 but also reduces image detail.
31
32 @since ids_peak_icv 1.1
33
34 @ingroup ids_peak_icv_python_filters
35 """
36
37 def __init__(self) -> None:
38 """!
39 @brief
40 Initializes a MedianFilter instance
41 with a default kernel size of 3 (3 × 3 neighborhood).
42
43 @since ids_peak_icv 1.1
44 """
45 self._kernel_size = 3
46
47 @property
48 def kernel_size(self) -> int:
49 """!
50 @brief
51 The size of the kernel.
52
53 @since ids_peak_icv 1.1
54 """
55 return self._kernel_size
56
57 @kernel_size.setter
58 def kernel_size(self, kernel_size: int) -> None:
59 """!
60 @brief
61 Sets the size of the kernel used for the median filter.
62
63 The size of the kernel
64 must be an odd number
65 greater than 1
66 and smaller than the image dimensions.
67 The kernel size defines the size of the neighborhood
68 (in a size × size grid)
69 used to calculate the median for each pixel.
70
71 A larger kernel removes more noise
72 but also reduces image detail.
73
74 @param kernel_size:
75 Size of the kernel used for the median filter.
76
77 @since ids_peak_icv 1.1
78 """
79 self._kernel_size = kernel_size
80
81 def process(self, image: Union[Image, ids_peak_ipl.Image]) -> Image:
82 """!
83 @brief
84 Applies the median filter to the specified image.
85
86 @supportedPixelformats{MedianFilter}
87
88 @note
89 Processes the entire image,
90 ignoring any specified image region.
91
92 @param image:
93 The image to process.
94
95 @return
96 The filtered image.
97
98 @exception NotSupportedException:
99 The pixel format is unsupported.
100 @exception NotPossibleException:
101 The kernel size must be an odd number greater than 1.
102 @exception OutOfRangeException:
103 The kernel size exceeds the image dimensions.
104
105 @since ids_peak_icv 1.1
106 """
107 _image = _check_and_get_icv_image(image)
108
109 output_image = Image.create_from_pixel_format_and_size(_image._pixel_format,
110 Size(_image.width, _image.height))
111
112 execute_and_map_return_codes(lib_loader.dll().peak_icv_Filter_Image_Median,
113 _image._handle,
114 ctypes.c_size_t(self._kernel_size),
115 output_image._handle)
116
117 return output_image
Class for applying a median filter to an image.
Image process(self, Union[Image, ids_peak_ipl.Image] image)
Applies the median filter to the specified image.
int kernel_size(self)
The size of the kernel.
None __init__(self)
Initializes a MedianFilter instance with a default kernel size of 3 (3 × 3 neighborhood).