IDS peak ICV v1.1.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
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 MedianFilter class applies a median filter to reduce noise in an image. It replaces each pixel value with the
17 median value of its neighboring pixels, based on a defined kernel size.
18
19 @ingroup ids_peak_icv_python_filters
20 @since ids_peak_icv 1.1
21 """
22
23 def __init__(self) -> None:
24 """!
25 @brief
26 Creates an instance of the MedianFilter class with a default kernel size of 3x3.
27
28 @since ids_peak_icv 1.1
29 """
30 self._kernel_size = 3
31
32 @property
33 def kernel_size(self) -> int:
34 """!
35 @brief
36 Gets the current kernel size used for the median filter.
37
38 @return int: The size of the kernel.
39
40 @since ids_peak_icv 1.1
41 """
42 return self._kernel_size
43
44 @kernel_size.setter
45 def kernel_size(self, kernel_size: int) -> None:
46 """!
47 @brief
48 Sets the size of the kernel used for the median filter.
49
50 @param kernel_size: Size of the kernel. Must be an odd number greater than 1.
51 Also, it has to be smaller than the image size.
52
53 @since ids_peak_icv 1.1
54 """
55 self._kernel_size = kernel_size
56
57 def process(self, image: Union[Image, ids_peak_ipl.Image]) -> Image:
58 """!
59 @brief
60 Applies the median filter to the given image and returns the filtered result.
61
62 This function processes the entire image and reduces noise using the specified kernel size.
63 The output image will have the same pixel format and dimensions as the input image.
64
65 @note This operation disregards any specified image regions. It processes the entire image.
66
67 @param image: The input image to which the median filter will be applied.
68 It can be of type Image or ids_peak_ipl.Image.
69 @return Image: The filtered image with reduced noise.
70 @exception NotSupportedException: If the input image is of an unsupported format.
71 @exception NotPossibleException: If the KernelSize is not a valid odd number greater than 1.
72 @exception OutOfRangeException: If the kernel size is bigger than the given image.
73
74 @since ids_peak_icv 1.1
75 """
76 _image = _check_and_get_icv_image(image)
77
78 output_image = Image.create_from_pixel_format_and_size(_image._pixel_format,
79 Size(_image.width, _image.height))
80
81 execute_and_map_return_codes(lib_loader.dll().peak_icv_Filter_Image_Median,
82 _image._handle,
83 ctypes.c_size_t(self._kernel_size),
84 output_image._handle)
85
86 return output_image
A size consists of width and height where width and height have to be greater than or equal to zero.
Definition size.py:9
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 given image and returns the filtered result.
int kernel_size(self)
Gets the current kernel size used for the median filter.
None __init__(self)
Creates an instance of the MedianFilter class with a default kernel size of 3x3.