Bilateral Smoothing

"Bilateral Smoothing" is also called as "Bilateral Blurring" or "Bilateral Filtering". This is the most advanced filter to smooth an image and reduce noise. All of the above filters will smooth away the edges while removing noises. But this filter is able to reduce noise of the image while preserving the edges. The drawback of this type of filter is that it takes longer time to process. For more information about bilateral smoothing, please refer to here.

OpenCV Code

In the following OpenCV/C++  example code is same as the above examples except for one function.

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace cv;

int main( int argc, char** argv )
//create 2 empty windows
namedWindow( "Original Image" , CV_WINDOW_AUTOSIZE );
namedWindow( "Smoothed Image" , CV_WINDOW_AUTOSIZE );

// Load an image from file
Mat src = imread( "MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED );

//show the loaded image
imshow( "Original Image", src );

Mat dst;
char zBuffer[35];

for int i = 1; i  <  31; i = i + 2 )

//copy the text to the "zBuffer"
_snprintf_s(zBuffer, 35,"Kernel Size : %d x %d", i, i);

//smooth the image using Bilateral filter in the "src" and save it to "dst" bilateralFilter( src, dst, i, i, i);

//put the text in the "zBuffer" to the "dst" image
putText( dst, zBuffer, Point( src.cols/4, src.rows/8), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255), 2 );

//show the blurred image with the text
imshow( "Smoothed Image", dst );

//wait for 2 seconds
int c = waitKey(2000);

//if the "esc" key is pressed during the wait, return
if (c == 27)
return 0;

//make the "dst" image, black
dst = Mat::zeros( src.size(), src.type() );

//copy the text to the "zBuffer"
_snprintf_s(zBuffer, 35,"Press Any Key to Exit");

//put the text in the "zBuffer" to the "dst" image
putText( dst, zBuffer, Point( src.cols/4,  src.rows / 2), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255) );

//show the black image with the text
imshow( "Smoothed Image", dst );

//wait for a key press infinitely
return 0;
You can download this OpenCV visual C++ project from here

Smooth Image using Bilateral Filter ( 9 x 9 )
Smooth Image using Bilateral Filter ( 9 x 9 )

New OpenCV Functions
  • void bilateralFilter( InputArray src, OutputArray dst, int d, double sigmaColor, double sigmaSpace, int borderType=BORDER_DEFAULT )
This OpenCV function filters an image using a Bilateral kernel. Each channel is processed independently. This method does not work in place. For real time applications, it is advised to use a value smaller than 5 for "d".

  • src - Input image (Image with 1 or 3 channels)
  • dst - Output image (It will have the same size and depth as the input image)
  • - Diameter of each pixel neighbourhood
  • sigmaColor - sigma in the color space
  • sigmaSpace - sigma in the coordinate space
  • borderType - You can define various border interpolation methods. This value only affects the pixels at the border. ( You can use one of the following; BORDER_DEFAULT, BORDER_REFLECT, BORDER_REPLICATE, BORDER_TRANSPARENT, BORDER_REFLECT_101 )
I have discussed with you about most commonly used smoothing methods with OpenCV/C++ examples. Try all of the above smoothing techniques using different filter sizes and observe the output image and the processing time.

No comments:

Post a Comment