Class Kernel

  • All Implemented Interfaces:
    Filter, Serializable
    Direct Known Subclasses:
    Kernel.GrayScaleFilter, MultiKernel, NonAveragingKernel

    public class Kernel
    extends Object
    implements Filter
    Kernel operations are shifting window masks applied to data point of an array to recompute it's value. This class supports 2D kernels which are mainly used on images pixel data. e.g. the bellow mask will calculate the value of the pixel by multiplying it's original value by 5 and subtracting surrounding pixels values.
     [  0 -1  0 
       -1  5 -1 
        0 -1  0 ]
     
    In image processing sharpening or blurring is realized by applying kernels to pixel data. Convolution example TODO support separability for custom kernels TODO kernel indices are swapped and twisted.
    Since:
    2.0.0
    Author:
    Kilian
    See Also:
    Serialized Form
    • Field Detail

      • mask

        protected double[][] mask
        Kernel mask applied to the pixels
    • Constructor Detail

      • Kernel

        public Kernel​(Kernel template)
        Create a clone of the supplied kernel
        Parameters:
        template - the kernel to clone
      • Kernel

        @Deprecated
        protected Kernel​(Kernel.EdgeHandlingStrategy strat)
        Deprecated.
        Empty constructor used by inheriting classes which are not able to provide a mask during first constructor call. The inheriting class promises to provide a mask and all checks by itself
        Parameters:
        strat - EdgeHandlingStrategy to use
      • Kernel

        public Kernel​(double[][] mask)
        Construct a non normalized kernel with the given pixel mask and the default edge handling strategy of Kernel.EdgeHandlingStrategy.EXPAND.
        Parameters:
        mask - used to filter pixel. So far only oddly shaped masks are allowed
        Throws:
        IllegalArgumentException - if mask's width or height is even
        Since:
        2.0.0
      • Kernel

        public Kernel​(double[][] mask,
                      boolean normalize)
        Construct a kernel with the given pixel mask and the default edge handling strategy of Kernel.EdgeHandlingStrategy.EXPAND.
        Parameters:
        mask - used to filter pixel. So far only oddly shaped masks are allowed
        normalize - If true the mask is normalized resulting in the sum of the mask being 1. This will preserve the magnitude of the original range. If the mask will be blindly copied without adjustment
        Throws:
        IllegalArgumentException - if mask's width or height is even
        Since:
        2.0.0
      • Kernel

        public Kernel​(double[][] mask,
                      Kernel.EdgeHandlingStrategy edgeHandling)
        Construct a non normalized kernel with the given pixel mask.
        Parameters:
        mask - used to filter pixel. So far only oddly shaped masks are allowed
        edgeHandling - the edge handling strategy used at the corners of the image
        Throws:
        IllegalArgumentException - if mask's width or height is even
        Since:
        2.0.0
      • Kernel

        public Kernel​(double[][] mask,
                      Kernel.EdgeHandlingStrategy edgeHandling,
                      boolean normalize)
        Construct a kernel with the given pixel mask.
        Parameters:
        mask - used to filter pixel. So far only oddly shaped masks are allowed
        edgeHandling - the edge handling strategy used at the corners of the image
        normalize - If true the mask is normalized resulting in the sum of the mask being 1. This will preserve the magnitude of the original range. If the mask will be blindly copied without adjustment
        Throws:
        IllegalArgumentException - if mask's width or height is even
        Since:
        2.0.0
    • Method Detail

      • identityFilter

        public static Kernel identityFilter()
        Return an identity kernel. This kernel is a 1x1 kernel and copies the original value to the new array
        Returns:
        a identity kernel
      • zeroFilter

        public static Kernel zeroFilter()
        Return an zero kernel. This kernel is a 1x1 kernel and zeroes out all values
        Returns:
        a zero kernel
      • boxFilter

        public static Kernel boxFilter​(int width,
                                       int height,
                                       double factor)
        A box filter is a filter which applies the same factor to squared region of pixels and can be counted to the blurring filters.
                Example factor: 0.3 width: 3 height: 3
         
                  0.3  0.3  0.3
                  0.3  0.3  0.3
                  0.3  0.3  0.3
         
        A normalized box filter is available via boxFilterNormalized(int, int).
        Parameters:
        width - of the kernel has to be odd and positive
        height - if the kernel has to be odd and positive
        factor - the factor applied to each pixel
        Returns:
        the box filter kernel
      • boxFilterNormalizedSep

        public static Kernel boxFilterNormalizedSep​(int width,
                                                    int height)
      • boxFilterNormalized

        public static Kernel boxFilterNormalized​(int width,
                                                 int height)
        A box filter is a filter which applies the same factor to squared region of pixels and can be counted to the blurring filters.

        This filter is normalized ensuring the same magnitude of the values.

        Parameters:
        width - of the kernel has to be odd and positive
        height - of the kernel has to be odd and positive
        Returns:
        the box filter kernel
      • gaussianFilter

        public static Kernel gaussianFilter​(int width,
                                            int height,
                                            double std)
        Creates a gaussian blur.

        A gaussian filter blurs the kernel with decreasing amount depending on the distance to the pixel and produces a more natural blurring effect than a box filter.

        The gaussian filter is normalized.

        Parameters:
        width - of the kernel has to be odd and positive
        height - of the kernel has to be odd and positive
        std - the standard deviation of the kernel. The higher the stronger the blur effect
        Returns:
        the gaussian kernel
      • edgeDetectionFilter

        @Deprecated
        public static Kernel edgeDetectionFilter​(int strength)
        Deprecated.
      • embossHorizontontalFilter

        public static Kernel embossHorizontontalFilter​(int depth)
        Parameters:
        depth - width and height. higher values will create a stronger effect
        Returns:
        a kernel creating a horizontal emboss effect
      • embossLeftDiagonalFilter

        public static Kernel embossLeftDiagonalFilter​(int depth)
                         1  0  0 
                         0  0  0 
                         0  0 -1
         
        Parameters:
        depth - width and height. higher values will create a stronger effect
        Returns:
        a kernel creating a left diagonal emboss effect
      • embossRightDiagonalFilter

        public static Kernel embossRightDiagonalFilter​(int depth)
                         0 0 1 
                         0 0 0 
                        -1 0 0
         
        Parameters:
        depth - width and height. higher values will create a stronger effect
        Returns:
        a kernel creating a right diagonal emboss effect
      • embossleftRightFilter

        public static Kernel embossleftRightFilter​(int depth)
                        0  0  0 
                        1  0 -1 
                        0  0  0
         
        Parameters:
        depth - width and height. higher values will create a stronger effect
        Returns:
        a kernel creating a right left emboss effect
      • apply

        public double[][] apply​(int[][] input)
        Apply the kernel to the 2d array. If the desired output is a int[][] array refer to applyInt(int[][]).
        Parameters:
        input - the input array to apply the kernel on
        Returns:
        a new array created by the kernel
      • applyInt

        public int[][] applyInt​(int[][] input)
        Apply the kernel to the 2d array with each value casted to a int value.
        Parameters:
        input - the input array to apply the kernel on
        Returns:
        a new array created by the kernel
      • applyInt

        public int[][] applyInt​(double[][] input)
        Apply the kernel to the 2d array with each value casted to a int value.
        Parameters:
        input - the input array to apply the kernel on
        Returns:
        a new array created by the kernel
      • apply

        public double[][] apply​(double[][] input)
        Apply the kernel to the 2d array.
        Parameters:
        input - the input array to apply the kernel on
        Returns:
        a new array created by the kernel
      • apply

        public double[][] apply​(byte[][] input)
        Apply the kernel to the 2d array. If the desired output is a byte[][] array refer to applyByte(byte[][]).
        Parameters:
        input - the input array to apply the kernel on
        Returns:
        a new array created by the kernel
      • applyByte

        public byte[][] applyByte​(byte[][] input)
        Apply the kernel to the 2d array with each value casted to a byte value.
        Parameters:
        input - the input array to apply the kernel on
        Returns:
        a new array created by the kernel
      • applyByte

        public byte[][] applyByte​(double[][] input)
      • calcValue

        protected double calcValue​(byte[][] input,
                                   int x,
                                   int y)
      • calcValue

        protected double calcValue​(int[][] input,
                                   int x,
                                   int y)
      • calcValue

        protected double calcValue​(double[][] input,
                                   int x,
                                   int y)
        Parameters:
        input - array
        x - pixelToLookAt
        y - pixelToLookAt
        Returns:
        convolutedPixel fo this x and y
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • filter

        public BufferedImage filter​(BufferedImage input)
        Description copied from interface: Filter
        Apply the filter to the input image and return an altered copy
        Specified by:
        filter in interface Filter
        Parameters:
        input - the input image to apply the filter on
        Returns:
        the altered image