Jump to content

  • Log In with Google      Sign In   
  • Create Account

Gaussian Blur Filter


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 glJack   Members   -  Reputation: 122

Like
0Likes
Like

Posted 28 April 2005 - 11:39 AM

Tell me please, where I can find detailed description of how to implement Gaussian Blur Filter in OpenGL

Sponsor:

#2 dimensionX   Members   -  Reputation: 100

Like
0Likes
Like

Posted 28 April 2005 - 11:50 AM

Do you know how to use pixel shaders ?

If so, search for "Image Processing on the GPU" on Google. I am sure you can find many resources. There is also a Chapter dedicated to Image Processing on the GPU in the book "GPU Gems". Hope this helps!

#3 glJack   Members   -  Reputation: 122

Like
0Likes
Like

Posted 28 April 2005 - 01:24 PM

dimensionX
Yes, I have some experience writing shaders. I googled many times with different queries, but I couldn't find any tutorials about Gaussian Blur. Everything I find are many Demos, and nothing but demos.
And, unfortunately, I don't have GPU Gems book.

Some tutorial links would be very nice.

#4 Andy Gainey   Members   -  Reputation: 2004

Like
0Likes
Like

Posted 28 April 2005 - 01:31 PM

Gamasutra: Real-Time Glow

#5 atcdevil   Members   -  Reputation: 145

Like
0Likes
Like

Posted 28 April 2005 - 02:14 PM

Did the demos you see have source code? If just source code isn't good enough, then ignore this since I didn't write a tutorial but I have source code available in Cg and GLSL of an anisotropic diffusion filter. (anisotropic diffusion is a generalization of Gaussian blurring).


To convert this to Gaussian blurring (isotropic diffusion):
In the GLSL version, file anisotropic_diffusion.glsl change the line:

vec4 c = exp(-(dif*dif)/k_squared);

to:

vec4 c = vec4(1.0f, 1.0f, 1.0f, 1.0f);


In the Cg version, file anisotropic_diffusion.cg change the line:

float4 c = exp(-(dif*dif)/k_squared);

to:

float4 c = (1.0f).xxxx;

The Cg version might complain since k_squared will no longer be used. If it does just set k_squared to be a huge number when it's sent from the host application and leave the shader as is.

[Edited by - atcdevil on January 30, 2007 11:14:33 AM]

#6 glJack   Members   -  Reputation: 122

Like
0Likes
Like

Posted 29 April 2005 - 01:06 AM

atcdevil

Thanx for help.

<code>
float sample = texRECT(previous_step, gl_TexCoord[0].st).r;
vec4 neighbors = vec4(texRECT(previous_step, gl_TexCoord[1].st).r,
texRECT(previous_step, gl_TexCoord[2].st).r,
texRECT(previous_step, gl_TexCoord[3].st).r,
texRECT(previous_step, gl_TexCoord[4].st).r);
</code>

I just don't undestand, what you pass in texcoords (0-4).
Can you explain me?

#7 atcdevil   Members   -  Reputation: 145

Like
0Likes
Like

Posted 29 April 2005 - 01:34 AM

Take a look at compute_locations.glsl, which computes the texture coordinate which tje anisotropic_diffusion.glsl fragment shader use.

// Compute Positions
void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_MultiTexCoord0 + vec4(1.0, 0.0, 0.0, 0.0); //east
gl_TexCoord[2] = gl_MultiTexCoord0 + vec4(-1.0, 0.0, 0.0, 0.0); //west
gl_TexCoord[3] = gl_MultiTexCoord0 + vec4(0.0, -1.0, 0.0, 0.0); //north
gl_TexCoord[4] = gl_MultiTexCoord0 + vec4(0.0, 1.0, 0.0, 0.0); //south
gl_Position = ftransform();
}

Also the code which draws a quad in the first place:
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f((float)width,0); glVertex2f((float)width, 0.0f);
glTexCoord2f((float)width, (float)height); glVertex2f((float)width, (float)height);
glTexCoord2f(0.0f, (float)height); glVertex2f(0.0f, (float)height);
glEnd();

I'm drawing a full-screen quad. Pretend that what I'm actually doing is exactly texture mapping the image onto that quad (with no blurring). If you used the 0th texture coordinate you would get an exact copy of the image. If you used the first coordinate you would get the image shifted by 1 pixel to the right. If you used the second coordinate you would get the image shifted by 1 pixel to the left. If you used the third coordinate you would get the image shifted by 1 pixel up. If you used the fourth coordinate you would get the image shifted by 1 pixel down.

For example if the current fragment is at (2,2)
texcoord0 = (2,2)
texcoord1 = (3,2)
texcoord1 = (1,2)
texcoord1 = (2,1)
texcoord1 = (2,3)


#8 glJack   Members   -  Reputation: 122

Like
0Likes
Like

Posted 29 April 2005 - 11:28 AM

atcdevil
Thanx for such cool explanation!
Now I understand, how to implemen blur. I still don't understand those formulas, but I see the concept.

Btw, why you use only red color component for blurring?

#9 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 29 April 2005 - 12:03 PM

Quote:
Original post by glJack
dimensionX
Yes, I have some experience writing shaders. I googled many times with different queries, but I couldn't find any tutorials about Gaussian Blur. Everything I find are many Demos, and nothing but demos.
And, unfortunately, I don't have GPU Gems book.

Some tutorial links would be very nice.


Fragment Shader code:
---------------------

uniform sampler2DRect rectTexture;

void main()
{
vec4 color = texture2DRect(rectTexture, gl_FragCoord.st);

color += texture2DRect(rectTexture, vec2(gl_FragCoord.s + 1, gl_FragCoord.t));
color += texture2DRect(rectTexture, vec2(gl_FragCoord.s + 1, gl_FragCoord.t + 1));
color += texture2DRect(rectTexture, vec2(gl_FragCoord.s, gl_FragCoord.t + 1));

gl_FragColor = color * 0.25;
}

This is simply a 2x2 averaging filter. To do a 3x3 Gaussian just plug-in the weights.

Also Keep in mind that gl_FragCoord.st is not at integer locations. The lower left window coordinate (gl_FragCoord) is (0.5, 0.5). There is a 0.5 offset. Now if you have mentioned the texture sampling a GL_LINEAR be aware of the 0.5 offsets.

GL_LINEAR works as follows:

s = floor(texCoord.s - 0.5); NOT s = floor(texCoord.s); as we usually do in C.
t = floor(texCoord.t - 0.5);

This -0.5 subtraction is to compensate for the 0.5 offset in the window coordinate. The reason for mentioning this is, if you have computed a texture coordinate as (1.2, 1.7) we would expect the lowe left corner to be (1, 1) but as per opengl's GL_LINEAR the lower left corner will be (0 = floor(1.2-0.5), 1 = floor(1.7-0.5)). Do you notice the difference. To overcome this just add 0.5 to the texture coordinates before sampling the textures i.e texture2DRect(rectTexture, texCoord.st + 0.5); While sampling OpenGL will subtract the 0.5 we have added and the result would be (1 = floor(1.2+0.5-0.5), 1 = floor(1.7+0.5-0.5))

Hope this information is useful to all those who are doing image processing on the GPU.


#10 atcdevil   Members   -  Reputation: 145

Like
0Likes
Like

Posted 29 April 2005 - 04:11 PM

Quote:
Original post by glJack
atcdevil

Btw, why you use only red color component for blurring?


It's not really the red component. My textures only have 1 component, so it's just a grayscale value.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS