ThresholdOptions Class

Immutable options of a binarization (ImageProcessingExtensions.Threshold): how the threshold of each pixel is chosen, and the two colors of the result.

public sealed class ThresholdOptions

Remarks

Every pixel is compared by its luminance: the Rec. 709 luma of its stored (encoded) samples at the storage precision, the value ImageProcessingExtensions.Grayscale stores (a gray pixel is its own luminance). A pixel whose luminance is greater than its threshold gets ThresholdOptions.UpperColor, any other pixel gets ThresholdOptions.LowerColor. The thresholds are on the scale of the samples, from 0 to max, the largest sample value (255 for 8-bit pixel formats, 65,535 for 16-bit ones); the levels of these options are fractions of it.

The windows of the adaptive methods (ThresholdMethod.Mean and ThresholdMethod.Sauvola) are squares of ThresholdOptions.WindowSize pixels centered on each pixel, which read the pixels outside the frame as ThresholdOptions.EdgeMode says. Their sums are exact integers; the means, the deviations and the thresholds are IEEE doubles, so the result is the same on every platform.

Constructors

NameDescription
ThresholdOptions()

ThresholdOptions()

ThresholdOptions.cs
public ThresholdOptions()

No documentation is available.

Properties

NameDescription
DefaultGets the default options: Otsu's method on each frame, which becomes black (below the threshold) and white (above it).
EdgeModeGets the pixels the windows of the adaptive methods read outside the frame. Defaults to ConvolutionEdgeMode.Mirror. With ConvolutionEdgeMode.Zero, the outside pixels count as black pixels of the window. Ignored by ThresholdMethod.Otsu and ThresholdMethod.Fixed.
LevelGets the threshold of ThresholdMethod.Fixed, a fraction of the sample range: a pixel whose luminance is greater than Level * max gets ThresholdOptions.UpperColor. Defaults to 0.5, which puts the 8-bit levels from 128 up in the upper class. Ignored by the other methods.
LowerColorGets the color of the pixels whose luminance is not greater than their threshold, alpha included. Defaults to opaque black. The pixel format must store it exactly (see ImageProcessingExtensions.Threshold).
MethodGets how the thresholds are chosen. Defaults to ThresholdMethod.Otsu.
OffsetGets what ThresholdMethod.Mean subtracts from the mean of the window, a fraction of the sample range: the threshold is mean - Offset * max. Defaults to 0. A positive offset keeps the pixels slightly darker than their surroundings, such as the noise of a blank background, in the upper class. Ignored by the other methods.
SauvolaKGets the sensitivity k of ThresholdMethod.Sauvola, from 0 to 1: the larger it is, the further below the mean the threshold of a window with little contrast falls. Defaults to 0.2. Ignored by the other methods.
SauvolaRGets the dynamic range R of the standard deviation of ThresholdMethod.Sauvola, a fraction of the sample range: R = SauvolaR * max. Defaults to 0.5 (127.5 for 8-bit samples, the value of the original paper). Ignored by the other methods.
ShareHistogramAcrossFramesGets a value indicating whether ThresholdMethod.Otsu chooses one threshold from the histogram of every frame of an image and its poster frame together, so that the frames of an animation get the same threshold and do not flicker. Defaults to false: each frame gets the threshold of its own histogram. Ignored when one frame is binarized, and by the other methods.
UpperColorGets the color of the pixels whose luminance is greater than their threshold, alpha included. Defaults to opaque white. The pixel format must store it exactly (see ImageProcessingExtensions.Threshold).
WindowSizeGets the width and the height, in pixels, of the window of the adaptive methods: an odd number from 3 to 20,001. Defaults to 15. Choose a window larger than the strokes to keep (about twice the height of a line of text, for a document). Ignored by ThresholdMethod.Otsu and ThresholdMethod.Fixed.

Gets the default options: Otsu's method on each frame, which becomes black (below the threshold) and white (above it).

public static ThresholdOptions Default { get; }

Property value

ThresholdOptions

Gets the pixels the windows of the adaptive methods read outside the frame. Defaults to ConvolutionEdgeMode.Mirror. With ConvolutionEdgeMode.Zero, the outside pixels count as black pixels of the window. Ignored by ThresholdMethod.Otsu and ThresholdMethod.Fixed.

public ConvolutionEdgeMode EdgeMode { get; init; }

Property value

ConvolutionEdgeMode

Exceptions

ArgumentOutOfRangeException
The value is not a defined ConvolutionEdgeMode.

Gets the threshold of ThresholdMethod.Fixed, a fraction of the sample range: a pixel whose luminance is greater than Level * max gets ThresholdOptions.UpperColor. Defaults to 0.5, which puts the 8-bit levels from 128 up in the upper class. Ignored by the other methods.

public double Level { get; init; }

Property value

double

Exceptions

ArgumentOutOfRangeException
The value is not between 0 and 1.

Gets the color of the pixels whose luminance is not greater than their threshold, alpha included. Defaults to opaque black. The pixel format must store it exactly (see ImageProcessingExtensions.Threshold).

public Rgba64 LowerColor { get; init; }

Property value

Rgba64

Gets how the thresholds are chosen. Defaults to ThresholdMethod.Otsu.

public ThresholdMethod Method { get; init; }

Property value

ThresholdMethod

Exceptions

ArgumentOutOfRangeException
The value is not a defined ThresholdMethod.

Gets what ThresholdMethod.Mean subtracts from the mean of the window, a fraction of the sample range: the threshold is mean - Offset * max. Defaults to 0. A positive offset keeps the pixels slightly darker than their surroundings, such as the noise of a blank background, in the upper class. Ignored by the other methods.

public double Offset { get; init; }

Property value

double

Exceptions

ArgumentOutOfRangeException
The value is not between -1 and 1.

Gets the sensitivity k of ThresholdMethod.Sauvola, from 0 to 1: the larger it is, the further below the mean the threshold of a window with little contrast falls. Defaults to 0.2. Ignored by the other methods.

public double SauvolaK { get; init; }

Property value

double

Exceptions

ArgumentOutOfRangeException
The value is not between 0 and 1.

Gets the dynamic range R of the standard deviation of ThresholdMethod.Sauvola, a fraction of the sample range: R = SauvolaR * max. Defaults to 0.5 (127.5 for 8-bit samples, the value of the original paper). Ignored by the other methods.

public double SauvolaR { get; init; }

Property value

double

Exceptions

ArgumentOutOfRangeException
The value is not greater than 0 and at most 1.

ShareHistogramAcrossFrames

ThresholdOptions.cs

Gets a value indicating whether ThresholdMethod.Otsu chooses one threshold from the histogram of every frame of an image and its poster frame together, so that the frames of an animation get the same threshold and do not flicker. Defaults to false: each frame gets the threshold of its own histogram. Ignored when one frame is binarized, and by the other methods.

public bool ShareHistogramAcrossFrames { get; init; }

Property value

bool

Gets the color of the pixels whose luminance is greater than their threshold, alpha included. Defaults to opaque white. The pixel format must store it exactly (see ImageProcessingExtensions.Threshold).

public Rgba64 UpperColor { get; init; }

Property value

Rgba64

Gets the width and the height, in pixels, of the window of the adaptive methods: an odd number from 3 to 20,001. Defaults to 15. Choose a window larger than the strokes to keep (about twice the height of a line of text, for a document). Ignored by ThresholdMethod.Otsu and ThresholdMethod.Fixed.

public int WindowSize { get; init; }

Property value

int

Exceptions

ArgumentOutOfRangeException
The value is even, less than 3 or greater than 20,001.