COMP 6771 - Bilateral Filtering
COMP 6771 - Bilateral Filtering
COMP 6771 - Bilateral Filtering
Submitted By –
Bilateral Filtering
Motivation - With contrast, in gaussian or regular filters, edges are lost after transformation and we acquire a new blend of foreground and
background pixels, which can often result in the loss of information. Typically, gaussian filtering computes the weighted average of the nearby
pixel values by calculating the spatial distance from the central pixel, where the weight decreases as we move away from the centre pixel. But what
if there are two domains in the image, and after transformation, a pixel in one of them has the same weight as a pixel in the other? As a result, edge
information may be lost, and pixels are mixed. Therefore, bilateral filters are introduced to smooth the images while maintaining the edges. They
measure the spatial distance as well as the photometric distance to assign the weights to their neighbouring pixels to get around this limitation.
We have also referred another paper on the same topic but having different approach and have compared both the approaches. The motivation
behind both the papers remains same that is to smooth the images and removing the noise. Other paper we have referred is Fast-adaptive Bilateral
filtering
Contribution – Bilateral Filters is often used in computational photography processes including tone mapping, style transfer, relighting, and
denoising because of its capacity to divide a picture into several scales without producing phantom colors after alteration. The main contribution of
the other paper (Fast-adaptive Bilateral Filtering) which we have refereed is to build a fast and effective algorithm which can help smoothing the
images by using less computational resources. This algorithm used in fast adaptive bilateral filters is 20x times faster than the brute-force
implementation. Basically, this approach is used for removing noise and to sharpen the images, but this can also be used for texture filtering, JPEG
deblocking and artifact removal.
Approaches - The main approach used in the study of bilateral filtering is combining both spatial and range filtering, which measures spatial
distance by a geometric closeness function and photometric distance by a similarity function, and then multiplies both to produce the altered image
of the input. Below is the given equation for bilateral filtering where c is geometric closeness function and s is similarity function.
Fast-adaptive Bilateral Filtering – In the above paper, they have used fixed gaussian range filter along with spatial filter but here they have used
adaptive bilateral filter where centre and width of the kernel is allowed to change from pixel to pixel. The algorithm proposed in this paper based
on local histogram approximation using polynomials whose complexity does not scale with spatial filter width and based on that filtering is
performed purely in range space using local histogram. The amount of sharpening and noise removal at a particular pixel is controlled by adapting
the centre and width of the kernel. This algorithm does not involve any form of kernel approximation rather than that approximation is purely based
on histogram.
The method proposed in the bilateral filtering paper is very time consuming as it is using nonlinear combinations of nearby pixel values and
moreover depends on the range and domain filtering constant. Further, it requires twice computing cost to convolve as much as a domain filter of
the same size, therefore, to tackle this issue, they precomputed all the similarity function's values and used them to alter the image. Another way to
keep the edges is to compute the median in the filter rather than the mean, while K-means is frequently used in place of the mean in many
applications.
1
BILATERAL FILTERING | NISHA BHATIA, VAISHAKHI PATEL |40220958, 40233201
We have implemented the bilateral paper using OpenCV library of the python and implemented the concept as shown below.
import os
import cv2
import math
import numpy as np
sigma = math.sqrt(variance)
half = kernel_size // 2
matrix[i, j] = math.sqrt(
def bilateral_filter(
img: np.ndarray,
spatial_variance: float,
intensity_variance: float,
kernel_size: int,
) -> np.ndarray:
"""
2
BILATERAL FILTERING | NISHA BHATIA, VAISHAKHI PATEL |40220958, 40233201
Input:
Output:
"""
filtered_image = np.zeros(img.shape)
if len(img.shape) == 3:
else:
filtered_image[i, j] = vals
return filtered_image
def bilateral_filter_image(
img,
3
BILATERAL FILTERING | NISHA BHATIA, VAISHAKHI PATEL |40220958, 40233201
spatial_variance,
intensity_variance,
kernel_size
):
out = out.astype("float32")
out = np.uint8(out)
return out
if __name__ == "__main__":
filename = os.path.abspath("lena_noise.jpg")
img = cv2.imread(filename, 0)
out = bilateral_filter_image(
img,
spatial_variance=15.0,
intensity_variance=75.0,
kernel_size=75
f = plt.figure()
f.add_subplot(1,2, 1)
plt.imshow(img)
plt.show()
f.add_subplot(1,2, 2)
plt.imshow(out)
plt.show()
4
BILATERAL FILTERING | NISHA BHATIA, VAISHAKHI PATEL |40220958, 40233201
This is the output of the above implemented code before and after applying bilateral filter and we have observed that image got blurred after
smoothing and the noise is also removed while preserving the edges as well. We have performed this algorithm on black and white as well as on
RGB bands as well.
if __name__ == "__main__":
filename = os.path.abspath("taj.jpg")
img = cv2.imread(filename)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
f = plt.figure(figsize=(10, 7))
f.add_subplot(1,2,1)
plt.title("Before")
plt.imshow(img)
plt.axis('off')
plt.show()
f.add_subplot(1,2,2)
plt.imshow(out, cmap='gray')
plt.axis('off')
plt.show()
5
BILATERAL FILTERING | NISHA BHATIA, VAISHAKHI PATEL |40220958, 40233201
Multiple iterations of bilateral filtering – Doing multiple iterations result in flattening the colors in an image but without blurring the edges. All
shadows and edges are preserved, but most of the shading is gone, and no “new” colors are introduced by filtering
filename = os.path.abspath("bunny.jpg")
img = cv2.imread(filename)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
f = plt.figure(figsize=(10, 7))
f.add_subplot(1,2,1)
plt.title("Original img")
plt.imshow(img)
plt.axis('off')
plt.show()
for i in range(4):
f.add_subplot(1,2,2)
plt.title("Iter = 4")
plt.imshow(out)
6
BILATERAL FILTERING | NISHA BHATIA, VAISHAKHI PATEL |40220958, 40233201
plt.axis('off')
Output –
For getting difference in images, we have change value of sigma such as spatial domain variance and intensity variance.
filename = os.path.abspath("lena_noise.jpg")
img = cv2.imread(filename)
results = []
sigma_ds = [1,3,10]
sigma_rs = [10,30,100,300]
results.append(result)
rows,cols = 3,4
a.set_title(f"sig_r = {sigma_r}")
a.set_ylabel(f"sig_d = {sigma_d}")
for i in range(rows):
for j in range(cols):
ax[i][j].imshow(results[i*cols+j],cmap='gray')
ax[i][j].axis('off')
7
BILATERAL FILTERING | NISHA BHATIA, VAISHAKHI PATEL |40220958, 40233201
plt.show()
𝜎𝑑 = 1
𝜎𝑑 = 3
𝜎𝑑 = 10
Bilateral filters perform operation on three bands at once and then measure the distance between pixels in combined space so this helps in preventing
the edges to be blurred and reduces phantom colors appear in original image. But as they perform nonlinear operation it is very time consuming
and very expensive in terms of computational cost.
Difficulties in Implementation
8
BILATERAL FILTERING | NISHA BHATIA, VAISHAKHI PATEL |40220958, 40233201
References
1. R. G. Gavaskar and K. N. Chaudhury, "Fast Adaptive Bilateral Filtering," in IEEE Transactions on Image Processing, vol. 28, no. 2,
pp. 779-790, Feb. 2019, doi: 10.1109/TIP.2018.2871597.
2. C. Tomasi and R. Manduchi, "Bilateral filtering for gray and color images," Sixth International Conference on Computer Vision
(IEEE Cat. No.98CH36271), 1998, pp. 839-846, doi: 10.1109/ICCV.1998.710815.
3. Sylvain Paris, Pierre Kornprobst, Jack Tumblin and Frédo Durand (2009), "Bilateral Filtering: Theory and Applications", Foundations and
Trends® in Computer Graphics and Vision: Vol. 4: No. 1, pp 1-73. http://dx.doi.org/10.1561/0600000020