Bilateral Filter in OpenCV in Python

In this tutorial, we are going to learn about the Bilateral Filter in OpenCV Python. So at first, we are going to learn about what is Bilateral Filter, what package is needed to perform this and how to do this. So let’s go step by step.

What is Bilateral Filter?

A bilateral filter is a kind of filter that reduces the noise for the smoothening images. It’s a type of non-linear filter which replaces an image by the nearby average filter of the image. But the weight of pixels is not only depended only Euclidean distance of pixels but also on the radiometric differences.

We are going to use this using the OpenCV method in python. If you have installed OpenCV in your machine then you can skip this step otherwise you have to install OpenCV in your machine.

Installing OpenCV

For installing OpenCV you have to just download the OpenCV and install it in your PC or Mac like the normal installation you have done before.

Implementing Bilateral Filter in Python with OpenCV

At first, we are importing cv2 as cv in python as we are going to perform all these operations using OpenCV.

import cv2 as cv

Next, our task is to read the image using the cv.imread() function. In the arguments of the function, we are giving the location of the Binary image, if the image is in the same folder then we only give the name of the image as the argument of the imread() function.

img = cv.imread('image.jpg')

Then we are creating bilateral as a variable and here we are applying a bilateral filter with Diameter of each pixel neighborhood =15 and sigmacolor=sigmaspace=75.

bilateral = cv.bilateralFilter(img, 15, 75, 75)

Now its time to write the image and save the output. we are going to perform using cv.imwrite() function.

cv2.imwrite('img_bilateral.jpg', bilateral)

The whole code for the operation is shown below:

import cv2 as cv

img = cv.imread('image.jpg')

bilateral = cv.bilateralFilter(img, 15, 75, 75) 

cv2.imwrite('img_bilateral.jpg', bilateral)

An example output is shown:

Bilateral Filter in OpenCV in Python

Also read: Skin Detection Using OpenCV in Python

Leave a Reply

Your email address will not be published. Required fields are marked *