双边过滤器[关闭]
Posted
技术标签:
【中文标题】双边过滤器[关闭]【英文标题】:Bilateral filter [closed] 【发布时间】:2011-08-07 10:35:19 【问题描述】:在给定高斯滤波器的情况下,如何实现双边滤波器?
【问题讨论】:
【参考方案1】:一个简单的双边滤波器可以定义为
Inew (x,y) = Summation(j=yn/2; j
其中常见的低通滤波器,例如高斯滤波器,根据内核中心 (x,y) 到每个像素 (i, j)。对于双边滤波器,权重基于两个距离确定:图像空间距离和颜色空间距离。
下面是一个简单的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;
【讨论】:
此代码无法编译。在函数的开头有一个不完整的流浪 for 循环。我假设这个循环遍历每个像素。请考虑修复您的代码。以上是关于双边过滤器[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
opencv学习笔记15种图像滤波辨析:方框均值高斯中值双边
我正在尝试运行 CUDA 示例,但我在 Ubuntu 16.04 LTS 上遇到了 openGL (libGLEW.a) 问题