Gaussian Blur Filters
Gaussian Blur Kernel Filter in Java
Diverging from typical Blur Box image filters, which employ an averaging filter, the simple average applied to pixel values in a defined neighborhood results in a uniform and straightforward blurring effect. In such scenario, each pixel contributes equally to its neighboring pixels.
In contrast, Gaussian blur utilizes a Gaussian distribution to ascertain the weights of pixels within the neighborhood. This implies that pixels closer to the center hold higher weights, leading to a smoother and more natural blurring effect. To simulate the Gaussian distribution, the following 3x3 matrix can be employed:
// gaussian blur 3x3 kernel matrix
{
{1, 2, 1,},
{2, 4, 2,},
{1, 2, 1,},
};
To maintain the brightness of the source image, all elements are divided by 16, a value that corresponds to the sum of the matrix elements.
// gaussian blur 3x3 kernel matrix /16
{
{ 0.0625, 0.125, 0.0625,},
{ 0.125, 0.25, 0.125,},
{ 0.0625, 0.125, 0.0625,},
};
Gaussian blur filter tends to yield a visually appealing and smooth result, surpassing the uniform blurring effect produced by blur box filters.


Java code example
The provided Java code example demonstrates the utilization of the Aspose.Imaging for Java API. Utilize the `ConvolutionFilter` class, which provides pre-defined kernel filters, including the GetGaussian() method with adjustable size and sigma value for the Gauss distribution. Moreover, you retain the flexibility to craft your personalized kernel matrix. Within this example, image templates in PNG and SVG formats are loaded from the "templates" folder, and a set of filters are applied from a predefined list.
import com.aspose.imaging.Image; | |
import com.aspose.imaging.RasterImage; | |
import com.aspose.imaging.VectorImage; | |
import com.aspose.imaging.imagefilters.complexutils.Complex; | |
import com.aspose.imaging.imagefilters.convolution.ConvolutionFilter; | |
import com.aspose.imaging.imagefilters.filteroptions.*; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Random; | |
static void filter(RasterImage raster, FilterOptionsBase options, String outputPath) | |
{ | |
raster.filter(raster.getBounds(), options); | |
raster.save(outputPath); | |
} | |
static double[][] getRandomKernel(int cols, int rows, Random random) | |
{ | |
double[][] customKernel = new double[cols][rows]; | |
for (int y = 0; y < customKernel.length; y++) | |
{ | |
for (int x = 0; x < customKernel[0].length; x++) | |
{ | |
customKernel[y][x] = random.nextDouble(); | |
} | |
} | |
return customKernel; | |
} | |
final int Size = 5; | |
final double Sigma = 1.5, Angle = 45; | |
double[][] customKernel = getRandomKernel(Size, 7, new Random()); | |
Complex[][] customComplex = ConvolutionFilter.toComplex(customKernel); | |
FilterOptionsBase[] kernelFilters = new FilterOptionsBase[] | |
{ | |
// convolution filters | |
new ConvolutionFilterOptions(ConvolutionFilter.getEmboss3x3()), | |
new ConvolutionFilterOptions(ConvolutionFilter.getEmboss5x5()), | |
new ConvolutionFilterOptions(ConvolutionFilter.getSharpen3x3()), | |
new ConvolutionFilterOptions(ConvolutionFilter.getSharpen5x5()), | |
new ConvolutionFilterOptions(ConvolutionFilter.getBlurBox(Size)), | |
new ConvolutionFilterOptions(ConvolutionFilter.getBlurMotion(Size, Angle)), | |
new ConvolutionFilterOptions(ConvolutionFilter.getGaussian(Size, Sigma)), | |
new ConvolutionFilterOptions(customKernel), | |
new GaussianBlurFilterOptions(Size, Sigma), | |
new SharpenFilterOptions(Size, Sigma), | |
new MedianFilterOptions(Size), | |
// deconvolution filters | |
new DeconvolutionFilterOptions(ConvolutionFilter.getGaussian(Size, Sigma)), | |
new DeconvolutionFilterOptions(customKernel), | |
new DeconvolutionFilterOptions(customComplex), | |
new GaussWienerFilterOptions(Size, Sigma), | |
new MotionWienerFilterOptions(Size, Sigma, Angle), | |
}; | |
// get path of the input data | |
String templatesFolder = System.getenv("DATA_PATH"); | |
if (templatesFolder == null) | |
{ | |
templatesFolder = "c:\\Users\\USER\\Downloads\\templates\\"; | |
} | |
// get output path | |
String outputFolder = System.getenv("OUT_PATH"); | |
if (outputFolder == null) | |
{ | |
outputFolder = templatesFolder; | |
} | |
String dataDir = templatesFolder; | |
String[] inputPaths = | |
{ | |
"template.png", | |
"template.svg" | |
}; | |
List<String> outputs = new ArrayList<>(); | |
for (String inputPath : inputPaths) | |
{ | |
for (int i = 0; i < kernelFilters.length; i++) | |
{ | |
FilterOptionsBase options = kernelFilters[i]; | |
try (Image image = Image.load(dataDir + inputPath)) | |
{ | |
String outputPath = String.format("%s%c%s-%d.png", outputFolder, File.separatorChar, inputPath, i); | |
if (image instanceof RasterImage) | |
{ | |
filter((RasterImage) image, options, outputPath); | |
} | |
else if (image instanceof VectorImage) | |
{ | |
String vectorAsPng = inputPath + ".png"; | |
if (!new File(vectorAsPng).exists()) | |
{ | |
image.save(vectorAsPng); | |
outputs.add(vectorAsPng); | |
} | |
try (Image png = Image.load(vectorAsPng)) | |
{ | |
filter((RasterImage) png, options, outputPath); | |
} | |
} | |
} | |
} | |
} | |
outputs.forEach(p -> new File(p).delete()); |