雙邊濾波(Bilateral filter)是一種非線性的濾波方法,是結合圖像的空間鄰近度和像素值相似度的一種折衷處理,同時考慮空域信息和灰度相似性,達到保邊去噪的目的。
雙邊濾波器之所以能夠做到在平滑去噪的同時還能夠很好的保存邊緣(Edge Preserve),是由於其濾波器的核由兩個函數生成:
一個函數由像素歐式距離決定濾波器模板的係數
另一個函數由像素的灰度差值決定濾波器的係數
其綜合了高斯濾波器(Gaussian Filter)和
αα-截尾均值濾波器(Alpha-Trimmed mean Filter)的特點。高斯濾波器只考慮像素間的歐式距離,其使用的模板係數隨著和窗口中心的距離增大而減小;Alpha截尾均值濾波器則只考慮了像素灰度值之間的差值,去掉 α%α%的最小值和最大值後再計算均值。雙邊濾波器使用二維高斯函數生成距離模板,使用一維高斯函數生成值域模板。
距離模板係數的生成公式如下:
其中,
(k,l)(k,l)為模板窗口的中心坐標; (i,j)(i,j)為模板窗口的其他係數的坐標; σdσd為高斯函數的標準差。使用該公式生成的濾波器模板和高斯濾波器使用的模板是沒有區別的。值域模板係數的生成公式如下:
其中,函數
f(x,y)f(x,y)表示要處理的圖像, f(x,y)f(x,y)表示圖像在點 (x,y)(x,y)處的像素值; (k,l)(k,l)為模板窗口的中心坐標; (i,j)(i,j)為模板窗口的其他係數的坐標; σrσr為高斯函數的標準差。將上述兩個模板相乘就得到了雙邊濾波器的模板
這裡的實現主要參考OpenCV中的bilateralFilter實現,其實現主要有兩個優化:
使用查表的方式計算灰度值模板係數
將二維的模板轉換為一維,降低算法複雜度。
在濾波之前,首先將灰度值模板係數計算出來。
double color_coeff = -0.5 / (color_sigma * color_sigma);vector<double> _color_weight(channels * 256); double *color_weight = &_color_weight[0];for (int i = 0; i < channels * 256; i++) color_weight[i] = exp(i * i * color_coeff);灰度值的模板係數計算公式參見上面的公式,是兩個灰度值的差值的平方。這裡表的長度是channels * 256沒有想通,應該255的長度就足夠了。在使用的時候,首先取出模板中心的灰度值val0,然後依次取出模板其他位置的灰度值val,使用abs(val - val0)的差值從color_weight查表得到灰度值模板的係數。
距離的模板是二維的,這裡使用的方法就i比較巧妙,將其化為了一維。
vector<double> _space_weight(ksize * ksize); vector<int> _space_ofs(ksize * ksize);
int maxk = 0; for (int i = -radius; i <= radius; i++) { for (int j = -radius; j <= radius; j++) { double r = sqrt(i*i + j * j); if (r > radius) continue; space_weight[maxk] = exp(r * r * space_coeff); space_ofs[maxk++] = i * temp.step + j * channels; } }使用一維數組存放空間模板係數,同時使用另一個一維數組存放模板位置,和係數相對應。
整個代碼的實現如下:void myBilateralFilter(const Mat &src, Mat &dst, int ksize, double space_sigma, double color_sigma){ int channels = src.channels(); CV_Assert(channels == 1 || channels == 3); double space_coeff = -0.5 / (space_sigma * space_sigma); double color_coeff = -0.5 / (color_sigma * color_sigma); int radius = ksize / 2; Mat temp; copyMakeBorder(src, temp, radius, radius, radius, radius, BorderTypes::BORDER_REFLECT); vector<double> _color_weight(channels * 256); vector<double> _space_weight(ksize * ksize); vector<int> _space_ofs(ksize * ksize); double *color_weight = &_color_weight[0]; double *space_weight = &_space_weight[0]; int *space_ofs = &_space_ofs[0]; for (int i = 0; i < channels * 256; i++) color_weight[i] = exp(i * i * color_coeff); int maxk = 0; for (int i = -radius; i <= radius; i++) { for (int j = -radius; j <= radius; j++) { double r = sqrt(i*i + j * j); if (r > radius) continue; space_weight[maxk] = exp(r * r * space_coeff); space_ofs[maxk++] = i * temp.step + j * channels; } } for (int i = 0; i < src.rows; i++) { const uchar *sptr = temp.data + (i + radius) * temp.step + radius * channels; uchar *dptr = dst.data + i * dst.step; if (channels == 1) { for (int j = 0; j < src.cols; j++) { double sum = 0, wsum = 0; int val0 = sptr[j]; for (int k = 0; k < maxk; k++) { int val = sptr[j + space_ofs[k]]; double w = space_weight[k] * color_weight[abs(val - val0)]; sum += val * w; wsum += w; } dptr[j] = (uchar)cvRound(sum / wsum); } } else if (channels == 3) { for (int j = 0; j < src.cols * 3; j+=3) { double sum_b = 0, sum_g = 0, sum_r = 0, wsum = 0; int b0 = sptr[j]; int g0 = sptr[j + 1]; int r0 = sptr[j + 2]; for (int k = 0; k < maxk; k++) { const uchar *sptr_k = sptr + j + space_ofs[k]; int b = sptr_k[0]; int g = sptr_k[1]; int r = sptr_k[2]; double w = space_weight[k] * color_weight[abs(b - b0) + abs(g - g0) + abs(r - r0)]; sum_b += b * w; sum_g += g * w; sum_r += r * w; wsum += w; } wsum = 1.0f / wsum; b0 = cvRound(sum_b * wsum); g0 = cvRound(sum_g * wsum); r0 = cvRound(sum_r * wsum); dptr[j] = (uchar)b0; dptr[j + 1] = (uchar)g0; dptr[j + 2] = (uchar)r0; } } }}需要注意圖像像素值的獲取,首先獲取到每行的坐標指針
const uchar *sptr = temp.data + (i + radius) * temp.step + radius * channels;uchar *dptr = dst.data + i * dst.step;在濾波循環中,從space_ofs中取出每個模板位置偏移地址
int val = sptr[j + space_ofs[k]];這種實現方法,大大的降低濾波的時間複雜度。
結果對比:
實現的結果和OpenCV的實現相差無幾。sigma = 80,模板大小為20
總結雙邊濾波器,在平滑圖像的同時,還能夠很好的保護圖像的邊緣信息,例如上圖中,圖像的平滑效果非常明顯了,但是頭髮的髮絲還是很明顯的。
雙邊濾波器的最重要參數仍然是標準差sigma,其值小於10時,平滑效果不是很明顯。