摘要:
双边滤波就是一种非线性的滤波方法,他在保持图像边缘信息的同时去除噪声。所谓非线性,是因为他的基本原理是对像素进行加权平均,其中权值取决于空间距离和像素值之间的差异。
双边滤波的原理:
在实现双边滤波时,我们需要定义一个整数半径radius,遍历每个像素点,对每个像素点radius范围内的像素点,求出它们的高斯权重和一个相似性权重,高斯权重表示距离当前中心像素越远对中心的影响越小,相似性权重表示像素值之间的差距越小,则权重越大。
求出两个权重后把他们相乘,作为对每个中心像素求加权平均值的权重,对每个中心像素都进行这样就实现了双边滤波。
总的来说,双边滤波能够对图像进行平滑处理,并保留图像的细节和边缘信息。
涉及的数学公式:
高斯函数:
![](https://i-blog.csdnimg.cn/blog_migrate/6f430a0cb32b947c4b677e3e76a5b60c.png)
double gaussian(double x, double sigma)
{
return exp(-(x * x) / (2 * sigma * sigma));
}
在双边滤波中,高斯函数的系数可以被省略
高斯权重函数:
![](https://i-blog.csdnimg.cn/blog_migrate/3028d83991f2d078c00117ab2aa37200.png)
∥pi,j−px,y∥ 表示中心像素和邻域内某一个像素的欧几里得距离,也就是坐标系距离
σ表示高斯函数的标准差
相似性权重函数:
![](https://i-blog.csdnimg.cn/blog_migrate/21e9ff2e9f5fb89283cb791df225103f.png)
他其实就是Gaussian(color_distance,sigmar)
color_distance=∥Ii,j−Ix,y∥ ,也就是中心像素值与他邻域内某一像素之间的差值
双边滤波器的定义:
![](https://i-blog.csdnimg.cn/blog_migrate/2db18e53f9e6a96a22c4863116f4e147.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e68071c1997952b7c37d9dce2b8636ee.png)
对每个像素点都进行这样的计算,就得到了双边滤波的结果。
注:
当然,如果是对彩色图像进行双边滤波,那么则需要访问一个像素点的BGR三个颜色通道,分别对他的颜色通道求出Wi,j(x,y)Si,j(x,y)Ix,y,再进行计算,并且最后计算出三个通道的加权平均值赋予给中心像素。
下面是对彩色图像的双边滤波的C++具体实现
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <cmath>
#include <vector>
using namespace cv;
using namespace std;
double gaussian(double x, double sigma)
{
return exp(-(x * x) / (2 * sigma * sigma));
}
double bilateralWeight(double x, double y, double sigma_s, double sigma_r, const Mat& src, int center_x, int center_y)
{
double spatial_distance = sqrt((x - center_x) * (x - center_x) + (y - center_y) * (y - center_y));
double color_distance = sqrt(pow((src.at<Vec3b>(center_x, center_y)[0] - src.at<Vec3b>(x, y)[0]), 2) +
pow((src.at<Vec3b>(center_x, center_y)[1] - src.at<Vec3b>(x, y)[1]), 2) +
pow((src.at<Vec3b>(center_x, center_y)[2] - src.at<Vec3b>(x, y)[2]), 2)); //计算color_distance
double spatial_weight = gaussian(spatial_distance, sigma_s);
double color_weight = gaussian(color_distance, sigma_r);
return spatial_weight * color_weight; //返回权重乘积
}
Mat bilateralFilter(const Mat& src, int d, double sigma_s, double sigma_r)
{
int h = src.rows, w = src.cols;
Mat dst = src.clone();
int radius = (d-1)/2;
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
double pixel_wsumR = 0.0;
double pixel_wsumB = 0.0;
double pixel_wsumG = 0.0;
double wsum = 0.0;
for (int k = i - radius; k <= i + radius; k++)
{
for (int l = j - radius; l <= j + radius; l++)
{
if (k >= 0 && k < h && l >= 0 && l < w)
{
double weight = bilateralWeight(k, l, sigma_s, sigma_r, src, i, j);
pixel_wsumB += weight * src.at<Vec3b>(k, l)[0];
pixel_wsumG += weight * src.at<Vec3b>(k, l)[1];
pixel_wsumR += weight * src.at<Vec3b>(k, l)[2]; //对pixel邻域内的所有pixel进行权重值累加
wsum += weight;
}
}
}
dst.at<Vec3b>(i, j)[0] = pixel_wsumB / wsum;
dst.at<Vec3b>(i, j)[1] = pixel_wsumG / wsum;
dst.at<Vec3b>(i, j)[2] = pixel_wsumR / wsum;
}
}
return dst;
}
int main()
{
Mat img = imread("test.png", 1);
if (img.empty())
{
cout << "Failed to read image!" << endl;
return -1;
}
Mat result = bilateralFilter(img, 7, 50, 50);
namedWindow("Original", WINDOW_AUTOSIZE);
imshow("Original", img);
namedWindow("Bilateral Filter", WINDOW_AUTOSIZE);
imshow("Bilateral Filter", result);
waitKey(0);
return 0;
}