Sharpen Filters
Sharpen Kernel Filter in Java
To achieve a Sharpen image effect, formulate a kernel matrix that amplifies pixel values in relation to their surroundings. In this instance, the central element is attributed a value nine times higher than its surroundings, with a subtraction applied for the eight adjacent pixels. This setup ensures that the total sum of coefficients remains at 1, thereby preserving the original brightness. The impact is more noticeable when neighboring pixels exhibit variations, accentuating the contrast between pixels and augmenting the sharpness of the image.
// sharpen 3x3 custom kernel
{
{ -1, -1, -1,},
{ -1, 9, -1,},
{ -1, -1, -1,},
};
The Sharpen effect enhances an image by highlighting pixel contrasts, leading to improved image detail and overall visual clarity.


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 "Sharpen3x3" and "Sharpen5x5," each with different kernel matrix sizes. Moreover, you retain the flexibility to craft your custom 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()); |