Recursive AI

Get OCR character boxes in C#

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

Highlight individual letters with approximate OCR boxes.

Terminal
dotnet add package RecursiveAI.OneOcr

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

C#
using OneOcr;
var engine = OcrEngine.LoadBundled();
var page = engine.OcrPages("map.png")[0];
foreach (var c in page.Words[0].Chars)
    Console.WriteLine((c.Text, c.Box.Left,
        c.Box.Top, c.Box.Right, c.Box.Bottom));

Unzip the example, then dotnet run.

map.png is a note beginning “Map for the morning train” in the download. This prints each character in its first word and the four edges of its box. Box coordinates can fall between pixels, so some values have decimals. IncludeChars is on by default:

Text
(M, 40, 32, 59.59701492537313, 64)
(a, 59.59701492537313, 32, 82.11194029850746, 64)
(p, 82.11194029850746, 32, 99, 64)

The full program

Compare the full bounding box of “Map” with the boxes of its three letters. The program prints the left, top, right, and bottom edges, rounded to whole pixels for display.

C#
using System.Globalization;
using OneOcr;
 
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
var engine = OcrEngine.LoadBundled();
var options = new OcrOptions { IncludeChars = true };
var page = engine.OcrPages("map.png", options).Single();
foreach (var word in page.Words.Take(1))
{
    Console.WriteLine($"Word {word.Text}: {Edges(word.Box)}");
    foreach (var ch in word.Chars)
        Console.WriteLine($"  {ch.Text}: {Edges(ch.Box)}; approximate: {ch.BoxIsApproximate}");
}
 
static string Edges(OcrBox box) =>
    $"({box.Left:F0}, {box.Top:F0}) to ({box.Right:F0}, {box.Bottom:F0})";

dotnet run prints:

Text
Word Map: (40, 32) to (99, 64)
  M: (40, 32) to (60, 64); approximate: True
  a: (60, 32) to (82, 64); approximate: True
  p: (82, 32) to (99, 64); approximate: True

A page with no recognized words has an empty Words list; check its count before indexing it on images other than this sample.

Variations

Keep word boxes but skip character boxes. Replace the code from var page down in the snippet at the top:

C#
var opts = new OcrOptions { IncludeChars = false };
var page = engine.OcrPages("map.png", opts).Single();
var word = page.Words.First();
Console.WriteLine($"{word.Text}: {word.Chars.Count} characters");

It prints Map: 0 characters.

The short answer

Use OcrPages with IncludeChars = true, then read word.Chars for each OcrWord in page.Words. Each OcrChar has Text and a Box. The option defaults to true; setting it explicitly makes it clear that the code needs character geometry. The boxes match the default text (greedy decoding). If you use beam search to read the text separately, that text may not line up with the boxes.

What is a bounding box in OCR?

It’s a rectangle that locates recognized text on the page. On the PNG in this example, its edges are expressed in pixels from the image’s top-left origin. Left and Top locate the upper-left corner; Right and Bottom locate the opposite corner. Subtract left from right for width and top from bottom for height. If you draw the image at a different scale, scale the coordinates to match the displayed image.

A word box describes the location of the whole word. OneOcr derives character positions from where the recognizer fires along that word. These are not traced letter outlines. In the sample, all three letters share top and bottom edges with “Map”; the p doesn’t get a different height for its descender. Read BoxIsApproximate to see that distinction in the data. For rotated text, Quad can hold four corners following the text rather than just its enclosing rectangle. See the page format.

How do I get a bounding box for each OCR character instead of each word?

Keep the word and its Chars collection together. For a review screen that highlights a whole questionable word, draw word.Box; for a screen that steps through its recognized characters, draw ch.Box and use ch.Text as the label. Text holds one entry from the model’s character set, usually one code point; don’t make the drawing code depend on a character always being one C# char. Spaces between words aren’t items in a word’s Chars collection.

If your app shows beam-search text and character boxes side by side, keep the characters from the OCR page with their own boxes rather than matching by string index across the two readings.

All oneocr recipes