Recursive AI

Threshold an image in linear light in C#

OneImage is a free, open-source image library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Use a cutoff of half the linear luminance of white, not half the encoded sRGB value. sRGB stores brightness on a curve, while linear light measures the actual amount of light.

Terminal
dotnet add package RecursiveAI.OneImage

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneImage;
 
var img = Image.FromPixels<Gray8>(
    [new(128), new(192), new(255)], 3, 1);
img.Threshold(0.5, linear: true)
    .Save("linear.png");
img.Threshold(0.5).Save("encoded.png");

The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.

Writes linear.png and encoded.png from the same three gray samples.

The full program

The example creates a 3 × 1 image in memory: its gray values are 128, 192 and 255, left to right. No input file is needed.

C#
using OneImage;
 
var image = Image.FromPixels<Gray8>(
    [new(128), new(192), new(255)], 3, 1);
var linear = image.Threshold(0.5, linear: true);
var encoded = image.Threshold(0.5);
 
linear.Save("linear.png");
encoded.Save("encoded.png");
Print("Linear", linear);
Print("Encoded", encoded);
 
static void Print(string label, Image image)
{
    var values = image.ToArray<Gray8>().Select(pixel => pixel.V);
    Console.WriteLine($"{label}: {string.Join(", ", values)}");
}
Text
Linear: 0, 255, 255
Encoded: 255, 255, 255

Gray 128 is just above 0.5 on the encoded scale but below 0.5 in linear light, so only that pixel changes between the two outputs. The cutoff must be between 0 and 1 inclusive; an out-of-range value throws ArgumentOutOfRangeException.

Variations

Keep pixels at or above one quarter of white’s linear luminance. Replace the linear threshold line:

C#
img.Threshold(0.25, linear: true)
    .Save("quarter.png");

Start with a file. Use your own sRGB JPEG named in.jpg:

C#
Image.Load("in.jpg").Threshold(0.5, linear: true)
    .Save("out.png");

The short answer

Use Threshold(0.5, linear: true) for a black-and-white image with a cutoff at half of white’s linear luminance. Pass the cutoff as a linear value. Leave linear off if you want to compare the encoded values instead. Those decisions are different even when the cutoff numbers are the same.

What is binary thresholding?

A binary threshold maps samples at or above a cutoff to white and those below it to black. On a color image, OneImage uses Rec. 709 luminance by default; on a grayscale image it uses the gray channel. Alpha is unchanged, so black and white here describe color, not a change to transparency. The operation does not accept CMYK or Lab images directly.

This is a global threshold: one number for the entire image. Linear light changes the scale on which that number is interpreted, not where the cutoff is applied. If a shadow on one part of a scanned page needs a different cutoff than a bright part, that is a local or adaptive thresholding problem.

Why does a 0.5 cutoff give different results in sRGB and linear light?

The sRGB transfer curve does not store brightness in direct proportion to light. Take gray 128: dividing by 255 gives about 0.502, which passes an encoded cutoff of 0.5. Converting that same gray sample to linear light gives about 0.216, which fails a linear cutoff of 0.5. On a neutral sRGB ramp, half linear light corresponds to an encoded value of about 0.735, or about 188 on a scale of 0 to 255. That is why gray 192 passes both tests but gray 128 does not.

Upgrading to OneImage 1.11.4. Check what your previous cutoff represented. Before 1.11.4, linear: true also converted the cutoff itself to linear light. In 1.11.4 it no longer does; use the default encoded threshold if the rule is about encoded luminance. If the rule is about the amount of light, keep linear: true and supply the linear cutoff directly.

All oneimage recipes