We will continue from a previous example of RGBA to gray image conversion with CUDA 5 and add gaussian filter. After applying the filter we will achieve the following transformation:
Here’s the information showing time it took to run CUDA kernel on the GPU compared to time on the CPU:
This code will work with RGBA images where each channel (Red, Green, Blue, and Alpha) is represented by one byte (8-bits) and a range of values between 0 and 255 (2^8 – 1) for a total of 4-bytes per pixel.
Gaussian function expresses normal distribution in statistics. In image processing, Gaussian blur is based on the following function in one dimension:
In two dimensions, it is the product of two Gaussians, one in each dimension, and has to be applied to each pixel of the image.
In the formula, x is the distance from the origin in the horizontal axis (number of pixels to the left and to the right), y is the distance from the origin in the vertical axis (number of pixels to the top and to the bottom), and σ is the standard deviation of the Gaussian distribution (arbitrary number). When applied in two dimensions, this formula produces a surface whose contours are concentric circles with a bell-shaped distribution from the center point. Values from this distribution are used to build a convolution matrix which is applied to the original image. Each pixel’s new value is set to a weighted average of that pixel’s neighboring pixels and self. The original pixel’s value receives the heaviest weight (having the highest Gaussian value) and neighboring pixels receive smaller weights as their distance to the original pixel increases. This results in a blur that preserves boundaries and edges.
Visually convollution matrix looks like this:
Please download code and read more here.