Graphics Reference
In-Depth Information
The previous comparison of blur filter sizes and types shows how a box blur filter applied
multiple times is able to approximate a Gaussian blur. In our blur recipes, this is of little
consequence as our box blur filter and Gaussian blur filter have a similar cost.
How it works…
A Gaussian blur convolution kernel is a separable filter that is generated using the following
Gaussian function formula. The weights of the kernel elements reduce as you move further
away from the origin, meaning that the weighted average of the Gaussian blur is better at
preserving the edge details than the box blur filter (see the previous comparison of the 11x11
Gaussian and box blur). The sum of the weights, as with most image filters, is 1.0.
Gaussian blur formula in a single dimension, where σ (sigma) is the standard deviation of the Gaussian distribution
(for example, blur radius) and x represents the distance from the origin
The previous formula can be implemented in C# for a given distance with the following
code snippet:
var sigma = radius;
var twoSigmaSqrd = (2.0 * sigma * sigma);
var value = Math.Exp(-(distance*distance) / twoSigmaSqrd) /
Math.Sqrt(twoSigmaSqrd));
When we specify a filter-tap size, we are effectively controlling the radius of the blur.
For example, a 3-tap horizontal Gaussian blur has a radius of one, while a 9-tap filter has a
radius of four. This is calculated by subtracting the origin pixel and dividing the result by two,
for example, (9-1) / 2 = 4 . Therefore, creating a Gaussian blur with a radius of nine requires
a 19-tap filter.
The filter-tap size must be larger than one, an odd number, and no more than 33 depending
on the thread group dimensions. The maximum size is dependent upon the maximum size
of a compute shader's group-shared memory, which in Shader Model 5 is 32 KB.
The command-line project ComputeGaussian.csproj used to generate these Gaussian
weights is included with the downloadable content for this Chapter, available on Packt's website.
The weights used here were generated with the appropriate radius and a blur amount of 1.0 .
There's more…
By applying a blur multiple times, it is possible to approximate a larger radius blur;
for example, a 9-tap filter applied three times results in a similar amount of blur
compared to a single pass of a 27-tap blur filter.
 
Search WWH ::




Custom Search