bilateral gaussian blur

Started by
5 comments, last by Yours3!f 11 years, 7 months ago
Hi,

I'm trying to implement a bilateral gaussian blur. I've already implemented a simple gaussian blur (see below), but I don't know how to turn that into a bilateral one. I've read about bilateral filtering, and I know that this way the weights not only depend on the distance between the samples but between the color intensities as well. Despite this I still have no clue how to do this.

I've used this tutorial to implement the gaussian blur: http://rastergrid.co...inear-sampling/


#version 420 core
uniform sampler2D texture0; //color buffer
uniform vec2 direction; //vec2(1, 0) --> horizontal, vec2(0, 1) --> vertical

in cross_shader_data
{
vec2 tex_coord;
} i;

out vec4 color;

void main()
{
float weights[5] =
{
0.0702702703, 0.3162162162, 0.2270270270, 0.3162162162, 0.0702702703
};

float offsets[5] =
{
-3.2307692308, -1.3846153846, 0.0, 1.3846153846, 3.2307692308
};

vec2 tex_size = textureSize(texture0, 0);
vec2 dir = direction / tex_size;

vec3 result = vec3(0.0);

for(int c = 0; c < 5; c++)
{
result += texture(texture0, i.tex_coord + offsets[c] * dir).xyz * weights[c];
}

color = vec4(result, 1.0);
}


so my question is how do I make this blur bilateral?

best regards,
Yours3!f
Advertisement
In order to accomplish a bilateral blur you will need to use FBOs (Frame Buffer Objects). You essentially render your scene to an FBO and then blur that in one direction. You then render the first FBO to either another FBO or to the screen then you blur that in the other direction.

Your Shaders are going to look something like this, you will have two of them, each in a different direction.


uniform sampler2D color_texture;
varying vec2 vTexCoord;

const float blurSize = 1.0/512.0;

void main(void)
{
vec4 sum = vec4(0.0);
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - 4.0*blursize)) * 0.05;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - 3.0*blursize)) * 0.09;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - 2.0*blursize)) * 0.12;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - blursize)) * 0.15;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y)) * 0.16;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + blursize)) * 0.15;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + 2.0*blursize)) * 0.12;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + 3.0*blursize)) * 0.09;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + 4.0*blursize)) * 0.05;
gl_FragColor = sum;
}

In order to accomplish a bilateral blur you will need to use FBOs (Frame Buffer Objects). You essentially render your scene to an FBO and then blur that in one direction. You then render the first FBO to either another FBO or to the screen then you blur that in the other direction.

Your Shaders are going to look something like this, you will have two of them, each in a different direction.


uniform sampler2D color_texture;
varying vec2 vTexCoord;

const float blurSize = 1.0/512.0;

void main(void)
{
vec4 sum = vec4(0.0);
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - 4.0*blursize)) * 0.05;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - 3.0*blursize)) * 0.09;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - 2.0*blursize)) * 0.12;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y - blursize)) * 0.15;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y)) * 0.16;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + blursize)) * 0.15;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + 2.0*blursize)) * 0.12;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + 3.0*blursize)) * 0.09;
sum += texture2D(color_texture, vec2(vTexCoord.x, vTexCoord.y + 4.0*blursize)) * 0.05;
gl_FragColor = sum;
}



thanks for the reply,

I'm already doing the fbos and ping-ponging between them :)
to add I'm doing the gaussian blur by 5 taps, as opposed to 9 taps in your example, but I still don't understand how did you calculate the weights:
0.16, 0.15, 0.12, 0.09, 0.05
also these need to be reduced so that I can still do blur by 5 taps...
Bilateral filtering considers samples depending on two weights (often the secondary weight is more or less a boolean function).

I.e. when using a gaussin blur on a SSAO map, you can make it depending on the depth buffer, that is, only pixels which are on a similar depth level then the target pixel are considered for blurring. Therefor you need more or less two filter criteria (both are more or less functions). Here's some pseudo code with a simple threshold function for depth:

[source lang="cpp"]
// all weights should sum up to 1.0
float weight_sum = 0;

// result color
vec3 color = vec3(0);

// sample depth and color for 0...7, where 4 is the center
if(abs(depth_0-depth_4) &#60; threshold)
{
color += color_0 * weight_0;
weight_sum += weight_0;
}
if(abs(depth_1-depth_4) &#60; threshold)
{
color += color_1 * weight_1;
weight_sum += weight_1;
}
...
// consider weight sum
color *= 1/weight_sum;
[/source]

You can optimize this (ie. comparing 4 samples at once).

Bilateral filtering considers samples depending on two weights (often the secondary weight is more or less a boolean function).

I.e. when using a gaussin blur on a SSAO map, you can make it depending on the depth buffer, that is, only pixels which are on a similar depth level then the target pixel are considered for blurring. Therefor you need more or less two filter criteria (both are more or less functions). Here's some pseudo code with a simple threshold function for depth:

[source lang="cpp"]
// all weights should sum up to 1.0
float weight_sum = 0;

// result color
vec3 color = vec3(0);

// sample depth and color for 0...7, where 4 is the center
if(abs(depth_0-depth_4) &#60; threshold)
{
color += color_0 * weight_0;
weight_sum += weight_0;
}
if(abs(depth_1-depth_4) &#60; threshold)
{
color += color_1 * weight_1;
weight_sum += weight_1;
}
...
// consider weight sum
color *= 1/weight_sum;
[/source]

You can optimize this (ie. comparing 4 samples at once).


thank you Ashaman73, this trick pretty much solved the problem smile.png (and you're right, I'm doing SSDO blurring biggrin.png)

this is how my shader looks like now:

#version 420 core

uniform sampler2D texture0; //ssdo buffer
uniform sampler2D texture1; //normal buffer
uniform sampler2D texture2; //depth buffer

uniform vec2 direction;
uniform float threshold;

in cross_shader_data
{
vec2 tex_coord;
} i;

out vec4 color;

void main()
{
float weights[4] =
{
0.0702702703, 0.3162162162, 0.3162162162, 0.0702702703
};

float offsets[4] =
{
-3.2307692308, -1.3846153846, 1.3846153846, 3.2307692308
};

vec2 tex_size = textureSize(texture0, 0);
vec2 dir = direction / tex_size;

vec3 normal_sample = texture(texture1, i.tex_coord).xyz;

if(normal_sample == vec3(0.0))
{
discard;
}

float center_depth = texture(texture2, i.tex_coord).x;
vec3 center_color = texture(texture0, i.tex_coord).xyz * 2.0 - 1.0;

vec3 result = center_color * 0.2270270270; //center weight
float weight_sum = 0.2270270270;

for(int c = 0; c < 4; c++)
{
if( abs( center_depth - texture(texture2, i.tex_coord + offsets[c] * dir).x ) < threshold )
{
result += ( texture(texture0, i.tex_coord + offsets[c] * dir).xyz * 2.0 - 1.0 ) * weights[c];
weight_sum += weights[c];
}
}

result *= 1.0 / weight_sum;

color = vec4(result * 0.5 + 0.5, 1.0);
}


for the threshold I chose 0.0001, as this seemed to be "good enough".
Actually the depth buffer contains linear depth (in range [0...1]), so if I want to check for 0.1 units difference, then I should set the threshold to:
far * x = 0.1;
as far is 1000 I needed this value: 0.0001

thanks again :D
Good, if you only need one shading value, not a RGB color, then you could optimize it like this:


vec4 sample_0_3;
sample_0_3.x = texture(texture0, i.tex_coord + offsets[0] * dir).xyz * 2.0 - 1.0 ).x;
sample_0_3.y = texture(texture0, i.tex_coord + offsets[1] * dir).xyz * 2.0 - 1.0 ).x;
sample_0_3.z = texture(texture0, i.tex_coord + offsets[2] * dir).xyz * 2.0 - 1.0 ).x;
sample_0_3.w = texture(texture0, i.tex_coord + offsets[3] * dir).xyz * 2.0 - 1.0 ).x;
vec4 depth_0_3;
depth_0_3.x = texture(depth0, i.tex_coord + offsets[0] * dir).xyz * 2.0 - 1.0 ).x;
depth_0_3.y = texture(depth0, i.tex_coord + offsets[1] * dir).xyz * 2.0 - 1.0 ).x;
depth_0_3.z = texture(depth0, i.tex_coord + offsets[2] * dir).xyz * 2.0 - 1.0 ).x;
depth_0_3.w = texture(depth0, i.tex_coord + offsets[3] * dir).xyz * 2.0 - 1.0 ).x;
vec4 if_replacement = step(threshold,abs( vec4(center_depth)-depth_0_3)));
result.x += dot(if_replacement.xyzw * weights_0_3.xyzw , sample_0_3.xyzw);
weight_sum+=dot(if_replacement.xyzw, weights_0_3.xyzw);
... same for 5 to 7



Good, if you only need one shading value, not a RGB color, then you could optimize it like this:


vec4 sample_0_3;
sample_0_3.x = texture(texture0, i.tex_coord + offsets[0] * dir).xyz * 2.0 - 1.0 ).x;
sample_0_3.y = texture(texture0, i.tex_coord + offsets[1] * dir).xyz * 2.0 - 1.0 ).x;
sample_0_3.z = texture(texture0, i.tex_coord + offsets[2] * dir).xyz * 2.0 - 1.0 ).x;
sample_0_3.w = texture(texture0, i.tex_coord + offsets[3] * dir).xyz * 2.0 - 1.0 ).x;
vec4 depth_0_3;
depth_0_3.x = texture(depth0, i.tex_coord + offsets[0] * dir).xyz * 2.0 - 1.0 ).x;
depth_0_3.y = texture(depth0, i.tex_coord + offsets[1] * dir).xyz * 2.0 - 1.0 ).x;
depth_0_3.z = texture(depth0, i.tex_coord + offsets[2] * dir).xyz * 2.0 - 1.0 ).x;
depth_0_3.w = texture(depth0, i.tex_coord + offsets[3] * dir).xyz * 2.0 - 1.0 ).x;
vec4 if_replacement = step(threshold,abs( vec4(center_depth)-depth_0_3)));
result.x += dot(if_replacement.xyzw * weights_0_3.xyzw , sample_0_3.xyzw);
weight_sum+=dot(if_replacement.xyzw, weights_0_3.xyzw);
... same for 5 to 7





you're right I could do this, plus since I'm using OpenGL 4.2 I can go ahead and sample all 4 by one texture fetch using textureGatherOffsets :D
vec4 all_depth_samples = textureGatherOffsets( texture0, i.tex_coord, offsets, 0);
This could be done for the SSAO result too, but for SSDO I need RGB values...

This topic is closed to new replies.

Advertisement