Edge Detection Filters

Edge Detection Custom Kernel Filter in Java

         The custom kernel filter designed for edge detection bears a resemblance to the Sharpen filter, with a notable difference: the sum of all matrix elements is set to zero. As a result, the generated image appears nearly black overall, except for pixels that exhibit variations from their neighboring counterparts. Typically, these deviating pixels are located at the boundaries between different regions or edges.

         In this example, the central pixel value is compared to its surrounding pixels in horizontal and vertical directions.

// horizontal edge detection
{
    { 0, 0, -1, 0, 0,},
    { 0, 0, -1, 0, 0,},
    { 0, 0,  4, 0, 0,},
    { 0, 0, -1, 0, 0,},
    { 0, 0, -1, 0, 0,},
};
// vertical edge detection
{
    { 0,  0,  0,  0,  0,},
    { 0,  0,  0,  0,  0,},
    { -1, -1, 4, -1, -1,},
    { 0,  0,  0,  0,  0,},
    { 0,  0,  0,  0,  0,},
};

         Eventually, the application of the filter leads to preserving solely the outlines of the image set against a black background.

Horizontal edges
Vertical edges
Horizontal edges 5x5 kernel filter detection in Java
Vertical edges 5x5 kernel filter detection in Java
Edge detection kernel filter

Java code example

         The provided Java code example demonstrates how to utilize the Aspose.Imaging for Java API. Utilize the `ConvolutionFilter` class, which provides pre-defined kernel filters, along with a custom kernel matrix. In this scenario, templates in PNG and SVG formats are loaded from the "templates" folder, and a set of filters are applied from a predefined list.