Class ConvolutionKernel
- Namespace
- Egami.Operations
- Assembly
- Egami.dll
A convolution kernel: an odd width and height of weights, applied as laid out (the weight at column 0, row 0 multiplies the pixel up and to the left of the center). The sum is divided by Divisor and Offset is added to the color channels.
public sealed class ConvolutionKernel
- Inheritance
-
ConvolutionKernel
- Inherited Members
Examples
using Raster soft = photo.Derive(ops => ops.Blur(3), Ct); // Gaussian, sigma in pixels
using Raster boxed = photo.Derive(ops => ops.BoxBlur(4), Ct); // fast, radius in pixels
using Raster crisp = photo.Derive(ops => ops.Sharpen(0.8f, 1.5f), Ct); // unsharp mask: amount, sigma
// Any kernel: here a 3 × 3 emboss, with edges clamped and alpha left alone.
ConvolutionKernel emboss = new(3, 3, [-2, -1, 0, -1, 1, 1, 0, 1, 2]);
using Raster embossed = photo.Derive(ops => ops.Convolve(emboss, EdgeMode.Clamp, preserveAlpha: true), Ct);
Constructors
ConvolutionKernel(int, int, ReadOnlySpan<float>, float, float)
Creates a kernel from its weights, row by row.
public ConvolutionKernel(int width, int height, ReadOnlySpan<float> values, float divisor = 1, float offset = 0)
Parameters
widthintThe width, an odd number from 1 to 255.
heightintThe height, an odd number from 1 to 255.
valuesReadOnlySpan<float>The weights,
width×heightof them, row by row.divisorfloatWhat the weighted sum is divided by. Defaults to 1.
offsetfloatWhat is added to red, green and blue afterwards, in 0..1 units. Defaults to 0.
Exceptions
- ArgumentException
A size is even or out of range, the weight count is wrong, or a value is not finite.
Properties
Divisor
Gets what the weighted sum is divided by.
public float Divisor { get; }
Property Value
Height
Gets the height, an odd number.
public int Height { get; }
Property Value
this[int, int]
Gets one weight.
public float this[int x, int y] { get; }
Parameters
Property Value
- float
The weight.
Exceptions
- ArgumentOutOfRangeException
xoryis out of range.
Offset
Gets what is added to red, green and blue after dividing, in 0..1 units.
public float Offset { get; }
Property Value
Width
Gets the width, an odd number.
public int Width { get; }
Property Value
Methods
Separable(ReadOnlySpan<float>, ReadOnlySpan<float>, float, float)
Creates a separable kernel: weight (x, y) is row[x] × column[y], and it runs as a horizontal pass and then a vertical
one, costing width + height taps per pixel instead of width × height.
public static ConvolutionKernel Separable(ReadOnlySpan<float> row, ReadOnlySpan<float> column, float divisor = 1, float offset = 0)
Parameters
rowReadOnlySpan<float>The horizontal weights, an odd count.
columnReadOnlySpan<float>The vertical weights, an odd count.
divisorfloatWhat the weighted sum is divided by. Defaults to 1.
offsetfloatWhat is added to red, green and blue afterwards, in 0..1 units. Defaults to 0.
Returns
- ConvolutionKernel
The kernel.
Exceptions
- ArgumentException
A count is even or out of range, or a value is not finite.