# Bilateral blur

This topic is 894 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

?Just wondering does the code look right for a bilateral blur?

float4 BilateralFiltering(VertexOut input) : SV_Target
{

const float blursize = 1.0f/300.0f;

float weights[9] = {0.05f,0.09f,0.12f,0.15f,0.16f,0.15f,0.12f,0.09f,0.05f};

float normalization = 1;
float offsets[9] =
{
-5.0,-4.0,-3.0,-2.0,1.0,2.0,3.0,4.0,5.0,
};

float4 centercolor =  gDiffuseMap.Sample( samAnisotropic, input.Tex);
float4 result = centercolor;
for(int c = 0,space = -4; c < 9; c++,space+=2)
{

float4 sample =  gDiffuseMap.Sample( samAnisotropic, float2(input.Tex.x+ offsets[c]*blursize, input.Tex.y));

float closeness = distance(sample,centercolor)/0.07;

float sampleWeight = closeness * weights[c];

result += sample * sampleWeight;
normalization += sampleWeight;
}
float4 bilateral = result / normalization;
return bilateral;
}

Edited by terryeverlast

##### Share on other sites

Your input image isn't 300 pixels wide which is kind of peculiar and you're only doing a horizontal blur which is also kind of funny. You can't do a separable bilateral blur without a bit of artifacting remember.

##### Share on other sites

Ok, I made those changes and didn't get what I liked.

I think my problem is the line of code:

float closeness = distance(sample,centercolor)/0.07;


I'm going to change to something with a compare function like "step". That will compare the two values and either return 0 or 1, comparing to a value of 0.0001.

So when it gets a color that changes a lot, it will preserve the edges.

I also need a depth texture.

Am I correct?

Edited by terryeverlast

##### Share on other sites

Why do you need a depth texture? A bilateral filter just means it's using two distance metrics. You can make the metrics whatever you want. E.g. should you use a linear pixel distance weight or gaussian? It really depends on what you're trying to do. You should figure out what effect you're trying to achieve first.

##### Share on other sites

Accidently got on a ssao or ssod blurring tutorial, that's where the depth came in.

So i guess ill try to figure this out

Edited by terryeverlast

##### Share on other sites

Oh, yeah, depth makes sense for ssao, probably not for a van gogh though. Besides, he already painted in all the lighting! :P

You want to choose your distance functions based on their aesthetic appeal imo.

##### Share on other sites

Still cant figure this out, i have a gaussian blur in the code below

float4 BilateralFiltering(VertexOut input) : SV_Target
{

const float pixel = 1.0f/500.0f; // my image is 500x500

float4 centralColor;
float gaussianWeightTotal;
float4 sum;
float4 sampleColor;
float distanceFromCentralColor;
float gaussianWeight;
float distanceNormalizationFactor = 1.0;
centralColor = gDiffuseMap.Sample(samAnisotropic, input.Tex.xy + float2(0,0));
gaussianWeightTotal = 0.18;
sum = centralColor * 0.18;

sampleColor = gDiffuseMap.Sample(samAnisotropic,  input.Tex.xy + float2(-pixel,-pixel));
distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);
gaussianWeight = 0.05 * (1.0 - distanceFromCentralColor);
gaussianWeightTotal += gaussianWeight;
sum += sampleColor * gaussianWeight;

sampleColor = gDiffuseMap.Sample(samAnisotropic,  input.Tex.xy + float2(-pixel,0));
distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);
gaussianWeight = 0.09 * (1.0 - distanceFromCentralColor);
gaussianWeightTotal += gaussianWeight;
sum += sampleColor * gaussianWeight;

sampleColor = gDiffuseMap.Sample(samAnisotropic,  input.Tex.xy + float2(-pixel,pixel));
distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);
gaussianWeight = 0.12 * (1.0 - distanceFromCentralColor);
gaussianWeightTotal += gaussianWeight;
sum += sampleColor * gaussianWeight;

sampleColor = gDiffuseMap.Sample(samAnisotropic,  input.Tex.xy + float2(0,-pixel));
distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);
gaussianWeight = 0.15 * (1.0 - distanceFromCentralColor);
gaussianWeightTotal += gaussianWeight;
sum += sampleColor * gaussianWeight;

sampleColor = gDiffuseMap.Sample(samAnisotropic,  input.Tex.xy + float2(0,pixel));
distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);
gaussianWeight = 0.15 * (1.0 - distanceFromCentralColor);
gaussianWeightTotal += gaussianWeight;
sum += sampleColor * gaussianWeight;

sampleColor = gDiffuseMap.Sample(samAnisotropic,  input.Tex.xy + float2(pixel,-pixel));
distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);
gaussianWeight = 0.12 * (1.0 - distanceFromCentralColor);
gaussianWeightTotal += gaussianWeight;
sum += sampleColor * gaussianWeight;

sampleColor = gDiffuseMap.Sample(samAnisotropic,  input.Tex.xy + float2(pixel,0));
distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);
gaussianWeight = 0.09 * (1.0 - distanceFromCentralColor);
gaussianWeightTotal += gaussianWeight;
sum += sampleColor * gaussianWeight;

sampleColor = gDiffuseMap.Sample(samAnisotropic,  input.Tex.xy + float2(pixel,pixel));
distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);
gaussianWeight = 0.05 * (1.0 - distanceFromCentralColor);
gaussianWeightTotal += gaussianWeight;
sum += sampleColor * gaussianWeight;

return sum / gaussianWeightTotal;


Icant find nothing on bilateral blurring!

What are the steps in doing a bilateral blur?

I'm wondering if I can do a bilateral blur by taking the center pixel and a nearby pixel and using a if statement to test Color Intensity(or color average). Then, if the color goes from one color to another, it will not blur and will preserve the edges (which what bilateral blurring is).

??

Edited by terryeverlast

##### Share on other sites

The reuslt i get when i take the above code and multiply it by 3.0..ie

 sampleColor = gDiffuseMap.Sample(samAnisotropic,  input.Tex.xy + float2(-pixel,-pixel)*3.0);



Good for noise reduction....almost looks little cartoonny

Edited by terryeverlast

##### Share on other sites

I'm wondering if I can do a bilateral blur by taking the center pixel and a nearby pixel and using a if statement to test Color Intensity(or color average). Then, if the color goes from one color to another, it will not blur and will preserve the edges (which what bilateral blurring is)

That's fine but using a continuous function might give better control. I'm pretty sure the photoshop "surface blur" filter is a bilteral filter -- and probably several others too.

1. 1
2. 2
Rutin
21
3. 3
JoeJ
18
4. 4
5. 5

• 14
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631722
• Total Posts
3001892
×