Recursive AI

Extract numbers with OCR in C#

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

Read the image with OCR, then keep only words made up of digits.

Terminal
dotnet add package RecursiveAI.OneOcr

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

C#
using OneImage;
using OneOcr;
 
var engine = OcrEngine.LoadBundled();
// stock.png is in the download.
var page = Image.Load("stock.png").Ocr(engine);
var words = page.Blocks.SelectMany(b => b.Lines)
    .SelectMany(line => line.Words);
foreach (var word in words)
{
    if (word.Text.Length > 0 &&
        word.Text.All(char.IsAsciiDigit))
        Console.WriteLine(word.Text);
}

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

If you paste the code, copy stock.png from the zip into the new project folder, or change the filename in the code to point to your own image.

Prints the digit-only words, keeping the leading zeros:

Text
0042
128

The included stock.png is a sample stock label with this text:

Text
BIN 0042
COUNT 128
WEIGHT 12.50

Image comes from OneImage, installed as a dependency of OneOcr. If OCR reads a zero as a capital letter O and returns that letter, the filter skips the whole word instead of correcting it.

Variations

Keep signed values and decimals. Add using System.Globalization; at the top and replace the foreach loop. This uses . as the decimal separator and doesn’t accept grouping separators.

C#
foreach (var word in words)
{
    const NumberStyles style =
        NumberStyles.AllowLeadingSign |
        NumberStyles.AllowDecimalPoint;
    if (decimal.TryParse(word.Text, style,
        CultureInfo.InvariantCulture, out _))
        Console.WriteLine(word.Text);
}
Text
0042
128
12.50

The short answer

Call image.Ocr(engine), then filter OcrWord.Text from the page’s blocks and lines. Use char.IsAsciiDigit when you want whole words that contain only 0–9, or parse each word when you want decimal values. Keep the original word text if the number is an identifier and leading zeros matter. Neither filter changes what the OCR engine reads.

How do I extract only numbers from an image in C#?

Work with words rather than removing every nondigit character from the entire OCR result. Removing nondigits would turn BIN 0042 and COUNT 128 into part of the same string and would turn 12.50 into 1250. Filtering whole words gives you 0042 and 128 as separate entries. It also rejects mixed words like A128 instead of silently turning them into numbers. This filter expects the number and its label to be separate recognized words. If you need to know where a value came from, keep the matching OcrWord: it also carries a box and a confidence score.

How do I extract decimal numbers from an image?

Use the variation above if a decimal point or a leading sign belongs to the value. decimal.TryParse checks the whole word against the number styles and culture you pass. This example uses invariant culture for a dot decimal point. If your documents use a comma decimal separator, choose their culture explicitly instead of relying on the machine’s default. Use the parsed value for arithmetic and the original word for display if you need the exact recognized spelling, such as 0042.

That parsing rule assumes the sign, digits and separator came back in one word. It doesn’t join OCR words, infer missing separators or check that the value makes sense for the field. A bin ID and a weight can both parse successfully even though only one is the weight.

Is OCR 100% accurate?

No. A filter can tell you whether a word looks like a number, not whether the digits match the image. A word containing the wrong digit may pass both filters. OneOcr also has a digit-lookalike correction that can rewrite a capital O as zero in some words. That correction is separate from the word filter you apply after OCR. For a field you rely on, compare questionable readings against the image and check constraints you already know, such as a fixed ID length or a valid range for a count.

All oneocr recipes