Table of Contents

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

width int

The width, an odd number from 1 to 255.

height int

The height, an odd number from 1 to 255.

values ReadOnlySpan<float>

The weights, width × height of them, row by row.

divisor float

What the weighted sum is divided by. Defaults to 1.

offset float

What 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

float

Height

Gets the height, an odd number.

public int Height { get; }

Property Value

int

this[int, int]

Gets one weight.

public float this[int x, int y] { get; }

Parameters

x int

The column, 0 to Width − 1.

y int

The row, 0 to Height − 1.

Property Value

float

The weight.

Exceptions

ArgumentOutOfRangeException

x or y is 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

float

Width

Gets the width, an odd number.

public int Width { get; }

Property Value

int

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

row ReadOnlySpan<float>

The horizontal weights, an odd count.

column ReadOnlySpan<float>

The vertical weights, an odd count.

divisor float

What the weighted sum is divided by. Defaults to 1.

offset float

What 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.