# How does one calculate color intensity?

How do I calculate color intensity if I have a image sample of RGB?

The sample is of a float3 of red, green, blue. I'm trying to do a bilateral blur and have to not change the image if the color intensity is the same so it will preserve edges.

 float4 BilateralFiltering(VertexOut input) : SV_Target
{

const float pixel = 0.01f;

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;

}

• Bilateral blur usually works the opposite way to what you've described — blurring the image where intensities are similar and leaving edges (areas of intensity difference) sharp. Can you tell us more about what kind of effect you're using the blur to achieve? There are multiple ways to define intensity for a colour, each with their own applications. Feb 4 '16 at 14:02
• Depends what "intensity" is for you. There are tens if not hundreds ways how to calculate color intensity. Feb 4 '16 at 14:02
• So far I'm doing a simple Gaussian blur of with weighted average. then I need to test the color for color. Feb 4 '16 at 14:09
• code up top in original post Feb 4 '16 at 14:12
• @JoshPetrie In the context of a bilateral blur, I think it's more likely that the "intensity" sought is luminance/luma/luminosity, or a measure of RGB difference between the two pixels. It really depends what the bilateral blur is being used to accomplish though (I've seen it used with depth values used as intensity, for instance). I'd like to ask again that user3513743 clarify what they're trying to use the blur to accomplish - what's the goal of applying this filter? Feb 4 '16 at 18:44

"Intensity" of a color is often the purity of a hue in color theory. It's also known as saturation.

This means you can compute the intensity (saturation) of a single color by converting it from the RGB space to the HSV space. The saturation component, specifically, can be computed by selecting the minimum and maximum color components

Cmax = max(R, G, B);
Cmin = min(R, G, B);


and computing the delta between them

d = Cmax - Cmin


If the delta is 0, the saturation is 0. Otherwise, the saturation is

saturation = delta / Cmax


Note that this computation assumes the color components are specified in the 0..1 range, so if you're using 0..255 you'll want to scale them first.

• I think the calculation will still work the same if you multiply everything by 255 (I.e. scale it all up), and leave you with something from 1 to 0. I'm not certain though.
– Nic
Feb 5 '16 at 5:51