I'm looking to do a "selective blur" or "surface blur" on the GPU. I've read some research papers on the subject, and seen a good deal of C code, but ideally I'd like something in GLSL, HSL, or CG.
Actually, a bilateral blur is not very different from a gaussian blur, you just have to multiply the value of the sample by the gaussian factor multiplied by the result of a "closeness' function between the actual values of the centre pixel and the sampled pixel.
So, in pseude GLSL
vec3 centreColour = texture2D(image,texCoord);
vec3 result = centreColour;
float normalization = 1;
for (int i = 0; i < NUM_SAMPLES; ++1)
{
vec3 sample = texture2D(image, texCoord + offsets[i]);
float gaussianCoeff = computeGaussianCoeff(offsets[i]);
//depends on your implementation, this is quick
float closeness = 1.0f - distance(sample,centreColour) / length(vec3(1,1,1));
float sampleWeight = closeness * gaussian;
result += sample * sampleWeight;
noralization += sampleWeight;
}
vec3 bilateral = result / normalization;
You should probably have a better way to determine the closeness value, eg do it in a perceptively uniform space such as LUV.
For optimization, you can apply all techniques that work for gaussian blur, apart from the ones that ignore the fact you have to know the pixel's value. For example, with a pure gaussian you can choose your texcoords wisely and integrate 4 pixels in one fetch. That won't work.
The big things that do work:
*Separate your filter. Instead of 5x5 samples (25), do 5 horizontally, save to an intermediate buffer, and do 5 vertically. It won't look exactly the same, but it will still remove noise.
*Perform your filter on a lower resolution, and composit it with the orignal to get some detail back.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With