Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / 3D Theory & Graphics / need source code Help me!! Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
realwong

April 13, 2005, 11:06 PM


Hi all

i want to write the Gaussian blur filter, but i can't do it.

in this Gaussian blur using two program language is C++ and Cg.

in Cg, using the fragment program is ok!


please help me!

give me the source code

Thank you!

 
Corre

April 14, 2005, 01:37 AM

I don't like it when people request source code like that. Why would you just want source code instead of an explanation that helps you do it by yourself?

 
Reedbeta

April 14, 2005, 02:01 AM

No source code for you, but here are some tips:

- Gaussian blur is seperable, so you do a 1-dimensional blur in the X axis and then do another in the Y axis, and the result is a 2-dimensional blur.
- The basic 1-dimensional Gaussian function is z = e^(-x^2).
- Scale and bias the x value to achieve the filter radius you want.
- Scale the z values so that they sum to 1 over the filter radius.
- Calculating a pixel in the blurred image is done by taking the pixel and its neighbors, multiplying them by corresponding values of the Gaussian function, and summing.

If you have a basic understanding of how images work, you should be able to figure this out. Try implementing it first in C++ and once you've got that working, do it in Cg. If you have specific questions, feel free to ask (but "give me the source code" ain't gunna happen).

 
NCCAChris

April 14, 2005, 05:55 AM

There is tons of info out there to help u do this - shouldn't be an issue really!

 
nea

April 14, 2005, 07:12 AM

I just found several tuts and sites with google with only one search... even nvidia itself got some stuff around...
as i say: "Use the force(Google), Luke!"

 
bignobody

April 14, 2005, 08:31 AM

I'm hungry! Please spoon-feed me!

 
L.e.Denninger

April 14, 2005, 08:32 AM

You need to visit http://okkie.frob.nl/clueshop.jpg

 
Steven Hansen

April 14, 2005, 12:10 PM

Reedbeta,

Where did you get that function for the Gaussian blur? I've always been under the impression that the whole thing was somewhat heuristic. Considering that Gauss (sp?) was a very mathematical individual, it makes sense that it would be based on some mathematical relationship.

My searches for gaussian blur typically turn up filter information for adobe photoshop or some image processing software documentation (ugh). I would like to see a technical exposť on the subject. If you have a cool link that you could share, that would be awesome.

Thanks.

 
Reedbeta

April 14, 2005, 01:09 PM

The Gaussian blur is based on the function for a normal distribution (aka Gaussian distribution).

http://mathworld.wolfram.com/NormalDistribution.html

This page gives the full equation for the Gaussian probability density, which is basically e^(-x^2) with some scale factors. Basically, it is a continuous version of a binomial distribution.

 
Betelgeuse

May 03, 2005, 07:09 AM

Afaik you construct a convolution matrix that has the 2D gaussian as weights... or, a 1D convolution window with a 1D gaussian as weights...

 
gaiaap

May 03, 2005, 08:58 AM

Or - should you feel so inclined - you could always do a 2d fft, cut the high frequencies, compensate for ringing with an apropriate window filter and do the inverse transform :)

Come to think of it, I do bellieve that nVidia had a sample implementation of time -> freq. / freq. -> time domain conversion using butterfly filters on the GPU.

 
Reedbeta

May 03, 2005, 04:48 PM

Actually, if I remember correctly, the Fourier transform of a Gaussian filter is another Gaussian filter =)

It's also possible to make approximate Gaussian filters by using the rows of Pascal's triangle as weights (e.g. 1,2,1 for a 3-tap filter, or 1,4,6,4,1 for a 5-tap). This is related to what I said earlier, about Gaussian filter being a continuous extrapolation of a binomial distribution.

 
adel

May 04, 2005, 07:56 AM

Alright fellow, here it is, even though I strongly recommend that you reseach the subject and implement it yourself.

  1.  
  2. ARGBSurface* TU_GaussianBlur( ARGBSurface* pSurface, UINT gauss_width )
  3. {
  4.         if( pSurface == NULL || gauss_width < 2 )
  5.                 return NULL;
  6.  
  7.         int* gauss_fact = new int[ gauss_width ];
  8.         int gauss_sum = 0;
  9.         for( UINT t=0; t<gauss_width/2; t++ )
  10.         {
  11.                 gauss_fact[ t ] = t+1;
  12.                 gauss_sum += gauss_fact[ t ];
  13.         }
  14.         for( t=gauss_width/2; t<gauss_width; t++ )
  15.         {
  16.                 gauss_fact[ t ] = gauss_width - t + 1;
  17.                 gauss_sum += gauss_fact[ t ];
  18.         }
  19.         FLOAT a, r, g, b;
  20.         UINT i, j, k;
  21.         INT n;
  22.         ARGB color;
  23.  
  24.         ARGBSurface temp( pSurface->uWidth, pSurface->uHeight );
  25.         for( i=0; i<temp.uWidth; i++ )
  26.         {
  27.                 for( j=0; j<temp.uHeight; j++ )
  28.                 {
  29.                         r = g = b = a = 0.0f;
  30.                         for( k=0; k<gauss_width; k++ )
  31.                         {
  32.                                 n = i - ((gauss_width-1)>>1) + k;
  33.                                 n = CLIP( n, 0, (INT)temp.uWidth - 1 );
  34.                                 color = pSurface->at( n, j );
  35.                                 a += (FLOAT)color.a * gauss_fact[k];
  36.                                 r += (FLOAT)color.r * gauss_fact[k];
  37.                                 g += (FLOAT)color.g * gauss_fact[k];
  38.                                 b += (FLOAT)color.b * gauss_fact[k];
  39.                         }
  40.                         temp.at( i, j ) = ARGB( BYTE( a/gauss_sum ),
  41.                                                                         BYTE( r/gauss_sum ),
  42.                                                                         BYTE( g/gauss_sum ),
  43.                                                                         BYTE( b/gauss_sum ) );
  44.                 }
  45.         }
  46.         for( i=0; i<pSurface->uWidth; i++ )
  47.         {
  48.                 for( j=0; j<pSurface->uHeight; j++ )
  49.                 {
  50.                         a = r = g = b = 0.0f;
  51.                         for( k=0; k<gauss_width; k++ )
  52.                         {
  53.                                 n = j - ((gauss_width-1)>>1) + k;
  54.                                 n = CLIP( n, 0, (INT)pSurface->uHeight - 1 );
  55.                                 color = temp.at( i, n );
  56.                                 a += (FLOAT)color.a * gauss_fact[k];
  57.                                 r += (FLOAT)color.r * gauss_fact[k];
  58.                                 g += (FLOAT)color.g * gauss_fact[k];
  59.                                 b += (FLOAT)color.b * gauss_fact[k];
  60.                         }
  61.                         pSurface->at( i, j ) = ARGB( BYTE( a/gauss_sum ),
  62.                                                                                 BYTE( r/gauss_sum ),
  63.                                                                                 BYTE( g/gauss_sum ),
  64.                                                                                 BYTE( b/gauss_sum ) );
  65.                 }
  66.         }
  67.         delete [] gauss_fact;
  68.         return pSurface;
  69. }
  70.  


I'm writing a simple texture library, and this function is part of it. If anyone is interested, I might consider sharing the code.

 
This thread contains 13 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.