Class Kernel
- java.lang.Object
-
- dev.brachtendorf.jimagehash.hashAlgorithms.filter.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.TODO support separability for custom kernels TODO kernel indices are swapped and twisted.
- Since:
- 2.0.0
- Author:
- Kilian
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classKernel.EdgeHandlingStrategyThe edge handling strategy defines the behaviour when a kernel reaches the edge of an image and does not have enough information to compute it's value.static classKernel.GrayScaleFilterKernels whose filter class work with grayscale instead of seperate color channels
-
Field Summary
Fields Modifier and Type Field Description protected Kernel.EdgeHandlingStrategyedgeHandlingHow are edged of the images handledprotected double[][]maskKernel mask applied to the pixels
-
Constructor Summary
Constructors Modifier Constructor Description Kernel(double[][] mask)Construct a non normalized kernel with the given pixel mask and the default edge handling strategy ofKernel.EdgeHandlingStrategy.EXPAND.Kernel(double[][] mask, boolean normalize)Construct a kernel with the given pixel mask and the default edge handling strategy ofKernel.EdgeHandlingStrategy.EXPAND.Kernel(double[][] mask, Kernel.EdgeHandlingStrategy edgeHandling)Construct a non normalized kernel with the given pixel mask.Kernel(double[][] mask, Kernel.EdgeHandlingStrategy edgeHandling, boolean normalize)Construct a kernel with the given pixel mask.Kernel(Kernel template)Create a clone of the supplied kernelprotectedKernel(Kernel.EdgeHandlingStrategy strat)Deprecated.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description double[][]apply(byte[][] input)Apply the kernel to the 2d array.double[][]apply(double[][] input)Apply the kernel to the 2d array.double[][]apply(int[][] input)Apply the kernel to the 2d array.byte[][]applyByte(byte[][] input)Apply the kernel to the 2d array with each value casted to a byte value.byte[][]applyByte(double[][] input)int[][]applyInt(double[][] input)Apply the kernel to the 2d array with each value casted to a int value.int[][]applyInt(int[][] input)Apply the kernel to the 2d array with each value casted to a int value.static KernelboxFilter(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.static KernelboxFilterNormalized(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.static KernelboxFilterNormalizedSep(int width, int height)protected doublecalcValue(byte[][] input, int x, int y)protected doublecalcValue(double[][] input, int x, int y)protected doublecalcValue(int[][] input, int x, int y)static KerneledgeDetectionFilter(int strength)Deprecated.static KernelembossHorizontontalFilter(int depth)static KernelembossLeftDiagonalFilter(int depth)1 0 0 0 0 0 0 0 -1static KernelembossleftRightFilter(int depth)0 0 0 1 0 -1 0 0 0static KernelembossRightDiagonalFilter(int depth)0 0 1 0 0 0 -1 0 0booleanequals(Object obj)BufferedImagefilter(BufferedImage input)Apply the filter to the input image and return an altered copystatic KernelgaussianFilter(int width, int height, double std)Creates a gaussian blur.inthashCode()static KernelidentityFilter()Return an identity kernel.StringtoString()static KernelzeroFilter()Return an zero kernel.
-
-
-
Field Detail
-
mask
protected double[][] mask
Kernel mask applied to the pixels
-
edgeHandling
protected Kernel.EdgeHandlingStrategy edgeHandling
How are edged of the images handled
-
-
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 ofKernel.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 ofKernel.EdgeHandlingStrategy.EXPAND.- Parameters:
mask- used to filter pixel. So far only oddly shaped masks are allowednormalize- 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 allowededgeHandling- 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 allowededgeHandling- the edge handling strategy used at the corners of the imagenormalize- 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.3A normalized box filter is available viaboxFilterNormalized(int, int).- Parameters:
width- of the kernel has to be odd and positiveheight- if the kernel has to be odd and positivefactor- 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 positiveheight- 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 positiveheight- of the kernel has to be odd and positivestd- 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 toapplyInt(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 toapplyByte(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- arrayx- pixelToLookAty- pixelToLookAt- Returns:
- convolutedPixel fo this x and y
-
filter
public BufferedImage filter(BufferedImage input)
Description copied from interface:FilterApply the filter to the input image and return an altered copy
-
-