A bilateral filter is used for smoothening images and reducing noise, while preserving edges. This article explains an approach using the averaging filter, while this article provides one using a median filter. However, these convolutions often result in a loss of important edge information, since they blur out everything, irrespective of it being noise or an edge. To counter this problem, the non-linear bilateral filter was introduced.
Gaussian blurring can be formulated as follows:
Here, is the result at pixel p, and the RHS is essentially a sum over all pixels q weighted by the Gaussian function. is the intensity at pixel q.
Bilateral Filter: an Additional Edge Term
The bilateral filter can be formulated as follows:
Here, the normalization factor and the range weight are new terms added to the previous equation. denotes the spatial extent of the kernel, i.e. the size of the neighbourhood, and denotes the minimum amplitude of an edge. It ensures that only those pixels with intensity values similar to that of the central pixel are considered for blurring, while sharp intensity changes are maintained. The smaller the value of , the sharper the edge. As tends to infinity, the equation tends to a Gaussian blur.
OpenCV has a function called bilateralFilter() with the following arguments:
- d: Diameter of each pixel neighborhood.
- sigmaColor: Value of in the color space. The greater the value, the colors farther to each other will start to get mixed.
- sigmaColor: Value of in the coordinate space. The greater its value, the more further pixels will mix together, given that their colors lie within the sigmaColor range.
Input : Noisy Image.
Code : Implementing Bilateral Filtering
Output of Bilateral Filter
Comparison with Average and Median filters
Below is the output of the average filter (
cv2.blur(img, (5, 5))).
Below is the output of the median filter (
Below is the output of the Gaussian filter (
cv2.GaussianBlur(img, (5, 5), 0)).
It is easy to note that all these denoising filters smudge the edges, while Bilateral Filtering retains them.
- Filtering Images based on size attributes in Python
- Python | Filtering data with Pandas .query() method
- NLP | Filtering Insignificant Words
- NLP | Training a tokenizer and filtering stopwords in a sentence
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Important differences between Python 2.x and Python 3.x with examples
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Visualizing O(n) using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.