In image processing applications, the bilateral filters are a special type of non-linear filters.
There is a trade off between loosing structure and noise removal, because the most popular method to remove noise is Gaussian blurring which is not aware of structure of image; therefore, it also removes the edges. Most of the time, edges contain valuable information about the scene and we don't want to loose it. The bilateral filter is aware of structure of the scene and it tends to act like a classical blurring filter when it is on a area without edges; however, when it sees an edge, it changes its behavior; so that, blurring does not work across the edges, but it works along the edges meaning that they are edge-preserving filters.
#include <opencv2/opencv.hpp>
#include <iostream>
void main(int argc, char* argv[]) {
if(argc==1) {
std::cout << argv[0] << " <image>" << endl;
return;
}
cv::Mat image, output;
image = cv::imread(argv[1]);
if(image.empty()) {
std::cout << "Unable to load the image: " << argv[1] << endl;
return;
}
cv::bilateralFilter(image, output, 3, 5, 3);
}