问题描述
我如何实现双边滤波器,给高斯滤波器?
How do I implement a bilateral filter, given a gaussian filter?
推荐答案
一个简单的双向滤波器可以被定义为
A simple bilateral filter can be defined as
INEW(X,Y)=求和(J =炔/ 2; J&其中; = Y + N / 2)求和(ⅰ= XM / 2; J&其中; = X +米/ 2)瓦特(I,J,的x,y)I(I,J)
Inew (x,y) = Summation(j=y-n/2; j<=y+n/2)Summation(i=x-m/2; j<=x+m/2)w(i,j,x,y)I(i,j)
,其中常见的低通滤波器,例如高斯滤波器,具有权重w(I,J,X,Y)的基础上从内核的中心的距离(X,Y)到各像素(i, j)条。对于双边滤波器,重量是基于两个距离确定:图像空间中的距离和一个colorht空间距离
where common low-pass filter, such as a Gaussian filter, has a weight w(i,j,x,y) based on the distance from the center of the kernel (x,y) to each pixel (i,j). For the bilateral filter, the weight is determined based on two distances: an image space distance and a colorht space distance.
一个简单的C实现低于
void convolution(uchar4 *_in, uchar4 *_out, int width, int height, int ~ halfkernelsize, float id, float cd)
{
int kernelDim = 2*halfkernelsize+1;
for(int y=0; y
float sumWeight = 0;
unsigned int ctrIdx = y*width + x;
float ctrPix[3];
ctrPix[0] = _in[ctrIdx].x;
ctrPix[1] = _in[ctrIdx].y;
ctrPix[2] = _in[ctrIdx].z;
// neighborhood of current pixel
int kernelStartX, kernelEndX, kernelStartY, kernelEndY;
kernelStartX = x-halfkernelsize;
kernelEndX = x+halfkernelsize;
kernelStartY = y-halfkernelsize;
kernelEndY = y+halfkernelsize;
for(int j= kernelStartY; j<= kernelEndY; j++)
{
for(int i= kernelStartX; i<= kernelEndX; i++)
{
unsigned int idx = max(0, min(j, height-1))*width + max(0, min(i,width-1));
float curPix[3];
curPix[0] = _in[idx].x;
curPix[1] = _in[idx].y;
curPix[2] = _in[idx].z;
float currWeight;
// define bilateral filter kernel weights
float imageDist = sqrt( (float)((i-x)*(i-x) + (j-y)*(j-y)) );
float colorDist = sqrt( (float)( (curPix[0] - ctrPix[0])*(curPix[0] - ctrPix[0]) +
(curPix[1] - ctrPix[1])*(curPix[1] - ctrPix[1]) +
(curPix[2] - ctrPix[2])*(curPix[2] - ctrPix[2]) ) );
currWeight = 1.0f/(exp((imageDist/id)*(imageDist/id)*0.5)*exp((colorDist/cd)*(colorDist/cd)*0.5));
sumWeight += currWeight;
_sum[0] += currWeight*curPix[0];
_sum[1] += currWeight*curPix[1];
_sum[2] += currWeight*curPix[2];
}
}
_sum[0] /= sumWeight;
_sum[1] /= sumWeight;
_sum[2] /= sumWeight;
_out[ctrIdx].x = (int)(floor(_sum[0]));
_out[ctrIdx].y = (int)(floor(_sum[1]));
_out[ctrIdx].z = (int)(floor(_sum[2]));
_out[ctrIdx].w = _in[ctrIdx].w;
}
}
}
这篇关于双向滤波器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!