Bilateral Filtering
Bilateral filter is another powerful method for noise removal. The main problem with Gaussian filter is that is it does not considers presence of edges which may not be desireable. Bilateral filter not only consider the neighborhood but also considers the pixel value similarity. This will help preserve the edges in the image. It can be computational expensive if higher number of neighborhood are considered.
In OpenCV the bilateral filter is implemented using bilateralFilter(). The signature of the bilateralFilter() is shown below:
cv2.bilateralFilter(src, d, sigmaColor, sigmaSpace [, borderType]])
The parameters of the above function are described as below:
Parameter |
Description |
src
|
The input image which can be 8-bit or floating-point, grayscale or color image.
|
d
|
Diameter of each pixel neighborhood that is used during filtering. If it is non-positive, it is computed from sigmaSpace.
|
sigmaColor
|
Filter sigma in the color space. A larger value of the parameter means that farther colors within the pixel neighborhood will be mixed together, resulting in larger areas of semi-equal color.
|
sigmaSpace
|
Filter sigma in the coordinate space. A larger value of the parameter means that farther pixels will influence each other as long as their colors are close enough. When d>0, it specifies the neighborhood size regardless of sigmaSpace. Otherwise, d is proportional to sigmaSpace
|
borderType
|
Border mode used to extrapolate pixels outside of the image.
|
Let us look at an example of how to apply bilateral filter on an image. Code 1-1 shows an example of application of bilateral filter.
Code 1-1 Bilateral Filtering
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | import cv2
import numpy as np
try:
path = r'F:\img\einstein.jpg'
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
bilateral = cv2.bilateralFilter(img,9,20,20)
gauss = cv2.GaussianBlur(img,(9,9),1)
cv2.imshow('Original Image',img)
cv2.imshow('Bilateral Filtering',bilateral)
cv2.imshow('Gaussian Filtering',gauss)
cv2.waitKey(0)
cv2.destroyAllWindows()
except Exception as e:
print(str(e))
|
Line 1 and 2: We start by importing OpenCV and numpy libraries.
Line 3: This line defines a try block. The exception block (Line 11-12) is associated with this block. If any exception occurs, it throws it and code in exception block is executed.
Line 4: Define path of the image to read.
Line 5: The image is read. Note that we are reading a grayscale image (see 2nd parameter).
Line 6: Application of bilateral filter with neighborhood diameter 9 and both sigma for color and space are set to 20.
Line 7: Gaussian blur technique with neighborhood size (9,9) and sigma set to 1.
Line 8-10: It displays original image, and results for both bilateral and Gaussian blurring.
Line 11: waitKey(0) function makes the window wait until user presses any keyboard button.
Line 12: Object of all the windows opened will be destroyed and closed.
The output obtained for the code shown above is shown in Figure 1-1.
Figure 1-1: Original image (left), Bilaterial filter (middle) and Gaussian filter (right).