版权声明:涉猎过的知识都像是不断汇入大海的涓涓细流,你怎么知道是哪条汇入的溪流让海洋成为海洋呢【转载请注明出处】 https://blog.csdn.net/panda1234lee/article/details/88239678
原理介绍:
请参考这篇博文——https://blog.csdn.net/panda1234lee/article/details/88016834
代码及详细注释:
//#version 120
uniform sampler2D iChannel0;
const vec2 iResolution = vec2(512., 512.);
const vec2 inv_res = vec2(1.) / iResolution.xy;
const int search_radius = 3; // 搜索窗口半径
const int block_radius = 1; // 模板窗口半径
const int search_window = 2 * search_radius + 1; // 搜索窗口大小
const float minus_search_window2_inv = -1.f/(search_window * search_window); // -.5f // opencv-cuda
const int h = 10; // 控制高斯函数衰减的程度
const int block_window = 2 * block_radius + 1; // 模板窗口大小
const float minus_h2_inv = -1.f/(h * h * 4); // 通道数为 4, opencv-cuda
//const float noise_mult = minus_h2_inv / (block_window * block_window); // opencv-cuda
const float noise_mult = minus_h2_inv * 500; // 保留更多细节
//const float noise_mult = -10.f; // 0.f;
// L2 范数
float norm2(const vec4 v)
{
//return v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w;
return pow(v.x, 2) + pow(v.y, 2) + pow(v.z, 2) + pow(v.w, 2);
}
void main(void)
{
vec2 coord_xy = gl_FragCoord.xy;
vec4 sum1 = vec4(0.f);
float sum2 = 0.f;
// 遍历搜索窗口
for(float y = -search_radius; y <= search_radius; ++y)
{
for(float x = -search_radius; x <= search_radius; ++x)
{
// 统计一个搜索窗口内的颜色(所有模板窗口内颜色的) L2 范数之和
float dist2 = 0;
// 遍历模板窗口
for(float ty = -block_radius; ty <= block_radius; ++ty)
{
for(float tx = -block_radius; tx <= block_radius; ++tx)
{
// 搜索窗口邻域
vec2 uv0 = (coord_xy + vec2(x + tx, y + ty)) * inv_res;
vec4 bv = /*clamp(*/ texture2D(iChannel0, uv0)/*, 0., 1. )*/;
// 当前像素邻域
vec2 uv1 = (coord_xy + vec2(tx, ty)) * inv_res;
vec4 av = /*clamp(*/ texture2D(iChannel0, uv1)/*, 0., 1. )*/;
dist2 += norm2(av - bv);
}
}
// 一个搜索窗口下,(所有模板窗口的颜色距离和像素距离计算出的)高斯权重
float w = exp(dist2 * noise_mult + (pow(x, 2) + pow(y, 2)) * minus_search_window2_inv);
vec2 uv2 = (coord_xy + vec2(x, y)) * inv_res;
sum1 += w * /*clamp(*/ texture2D(iChannel0, uv2) /*, 0., 1.)*/; // 高斯权重 * 像素值
sum2 += w; // 累加所有搜索窗口的高斯权重,用于归一化
}
}
gl_FragColor = vec4(sum1 / sum2);
//gl_FragColor = texture2D(iChannel0, coord_xy * inv_res); // test
}
原图:
滤波效果图(在我机器上瞬时的 fps 达到了 205 左右):
NLM 去噪效果是挺不错,但是它的最大缺点就是计算复杂度太高,程序非常耗时,一旦增加 block_radius,FPS 就 “嗖嗖” 得下降了。 -_-b