Anyone know where i can find a glsl implementation of a bilateral filter (blur)?
I'm looking to do a "selective blur" or "surface blur" on the GPU. I've read some research papers on the s开发者_运维问答ubject, 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.
For anyone who get here, if you don't re-implement by your own, there is a fully implementation in GPUImageView for Android.
The implementation include vertex shader and fragment shader.
精彩评论