In this OpenCV article we are going to learn about OpenCV Smooth Image with Bilateral Filtering.
so this is the fourth technique for image smoothing or blurring in OpenCV . the first one was
using Averaging, the second was using Gaussian Blurring and the third one was using Median
Blurring you can check the articles in the below links.
Read More Image Smoothing Techniques in OpenCV
1: OpenCV Averaging Image Blurring in Python
2: OpenCV Gaussian Blurring for Images in Python
3: OpenCV Median Blurring for Images in Python
A bilateral filter is non-linear, edge-preserving and noise-reducing smoothing filter. The
intensity value at each pixel in an image is replaced by a weighted average of intensity values
from nearby pixels. This weight can be based on a Gaussian distribution.
So now this is the complete code for OpenCV Smooth Image with Bilateral Filtering
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import cv2 import matplotlib.pyplot as plt image = cv2.imread('imageface.jpg') bil_blur = cv2.bilateralFilter(image,13,75,75) #convert image from bgr to rgb cvt_orig = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) cvt_blur = cv2.cvtColor(bil_blur, cv2.COLOR_BGR2RGB) plt.subplot(121) plt.imshow(cvt_orig) plt.title('Original Image') plt.xticks([]), plt.yticks([]) plt.subplot(122) plt.imshow(cvt_blur) plt.title('Bilateral Image') plt.xticks([]), plt.yticks([]) plt.show() cv2.waitKey(0) cv2.destroyAllWindows() |
OK for this purpose we are going to use this image, and we want to smooth this image
using Bilateral method.
This is the syntax for Bilateral Filtering.
1 |
cv2.bilateralFilter(src, dst, d, sigmaColor, sigmaSpace, borderType) |
dst | It is the destination or output image |
d | A variable of the type integer representing the diameter of the pixel neighborhood. |
sigmacolor | A variable of the type integer representing the filter sigma in the color space. |
sigmaspace | A variable of the type integer representing the filter sigma in the coordinate space. |
Also you can use borderType if you want.
This line of code is used for reading of the image, make sure that you have added an image
in your working directory.
1 |
image = cv2.imread('imageface.jpg') |
And in here we are going to create our Bilateral Filtering.
1 |
bil_blur = cv2.bilateralFilter(image,13,75,75) |
Because we are going to show our image in Matplotlib, so Matplotlib uses RGB (Red, Green, Blue)
color system, and OpenCV uses BGR (Blue, Green, Red) color system, we need to convert the BGR
color to RGB. if we don’t do this there will be messed up in the color.
1 2 |
cvt_orig = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) cvt_blur = cv2.cvtColor(bil_blur, cv2.COLOR_BGR2RGB) |
Note: cv2.waitKey() is a keyboard binding function. Its argument is the time in milliseconds.
the function waits specified milliseconds for any keyboard event. If you press any key in that
time, the program continues. If 0 is passed, it waits indefinitely for a key stroke.
Run the complete code and this is the result.
Subscribe and Get Free Video Courses & Articles in your Email