---
title: How hanocr reads a woodblock page, step by step
description: Segmentation, the edge column, the book's exemplars, elastic matching, a language model, calibrated confidence, self-training and characters built from parts, in plain words.
---

Elamur: [Mova](https://md.mova.elamur.ai/free/en/) · [Parse](https://mova.elamur.ai/free/en/parse/) · [Research](https://md.research.elamur.ai/free/en/) · [Gali](https://md.gali.elamur.ai/free/en/) · [MCP](https://mcp.elamur.ai/) · [Locations](https://md.locations.elamur.ai/free/en/) · [Weather](https://md.weather.elamur.ai/free/en/) · [Shryfti](https://md.shryfti.elamur.ai/free/en/) · **OCR**

This page as HTML: <https://ocr.elamur.ai/free/en/method/>

# How hanocr reads a page

hanocr reads a page in a few plain steps, each measured on its own: it finds the columns and cuts out the characters, compares each character's lines with exemplars from the same book, lets a model of the language choose among close shapes, says how sure it is, and learns again from what it is sure of. No neural network is involved; every step below is plain code, measured on pages it never learned from.

```flow
title: The steps, in order
- frame and columns
- characters cut
- 100 candidates by stroke direction
- elastic matching
- language model
- calibrated confidence
- self-training
```

## Finding the characters

A page of this book is a frame with ten columns between ruled lines. hanocr first finds the frame, then the columns as an even grid: rules and the gaps between columns repeat at one step, so the grid is fitted to the whole page and each rule is then refined in place, within a sixth of a step. The grid is scored two ways, by the ink on the rules and by the gaps, where the text in the middle of a column is dark and the rule's neighbourhood is light; this cut the false grids from 34 to 15 of the 190 pages.

Inside a column, characters are cut where the ink is thinnest, between one character and the next. What is not a character is taken away first: the ends of the rules, the dashes of a rule that broke in the block, the inner line of the frame, and specks of ink at the ends of a box. Commentary is printed in two small columns inside a column; hanocr reads those as notes, right before left.

```ocr-fig seg
A piece of page 100: the boxes hanocr cut, coloured by how sure it is of each character.
```

## The column at the edge

Many scans cut the outermost column at the edge of the image: the rule beyond it is missing, so the grid saw nine columns. hanocr now accepts a column the edge has cut, when the grid's step says one more column fits. Pages where all ten columns were found rose from 62 to 161 of 190, and the characters that get a box of their own from 86.9 % to 94.9 %.

```ocr-fig edge
A schematic page: nine columns between rules, and a tenth one cut by the edge of the scan, found by the grid's step.
```

## Exemplars from the book itself

The proofread pages on Wikisource are hanocr's teacher. Their text is laid onto the boxes cut from the same page by dynamic programming, in several rounds, each with the exemplars of the round before. Every matched box becomes an exemplar: this character, as this carver cut it.

Each box is reduced to the directions of its strokes in a 6 × 6 grid of cells with eight directions each, and a coarse map of its ink. Those features pick the 100 most likely characters for every box.

## Elastic matching

Two prints of one character are never quite alike: the carver's knife wandered, the ink spread, the paper moved. So each of the 100 candidates is compared again, elastically: every point of a line may move up to 2 pixels (of 32) to find the nearest point of the exemplar with a similar neighbourhood of stroke directions, both ways. A stroke a little lower or a little longer stays the same stroke; a missing stroke still costs. This step alone raised the test score from 69.6 % to 74.7 %.

```ocr-fig elastic
Two prints of one character, one stroke a little lower: matched point to point within 2 pixels, they count as the same.
```

## The language model

Shapes alone cannot tell some characters apart, and the text can: a model of how characters follow each other, built from other proofread volumes of the same edition and from the 資治通鑑, chooses among the candidates of a whole page at once (the Viterbi algorithm). The test pages are left out of the model: the first, higher score was found to have leaked them, since the series the model came from includes this very volume. Trigrams and Kneser–Ney smoothing changed nothing, because the model's texts are far from this book; bigrams do the work.

## Confidence, calibrated

For every character, hanocr gives the probability that it is right, the posterior of the reading. On its own that probability is too sure of itself, so it is calibrated on the dev pages 60–69, which take no part in training. On the test pages the mean confidence was 78.0 % with 80.2 % right: slightly too modest. The characters read with a confidence of 0.8 or more, two thirds of all, were right 96.8 % of the time.

```ocr-fig conf
How sure hanocr is of the 17 015 characters it read on pages 89–196, the share of them in each band of confidence:
- under 0.5: 13.1 % (2 235)
- 0.5 to 0.8: 5.7 % (974)
- 0.8 to 0.9: 4.0 % (680)
- 0.9 to 0.99: 14.6 % (2 477)
- 0.99 or more: 62.6 % (10 649)
```

## Self-training

Once the proofread pages are learnt, hanocr reads the 108 pages nobody has proofread. Every character read there with a confidence of 0.9 or more becomes an exemplar too, and the pages are read again; two rounds. The last reading added 10,482 such exemplars to the book's own, and the test score rose from 80.5 % to 81.5 %.

## Characters built from parts

About 4,100 characters the language model knows never appear on the proofread pages, so hanocr has no exemplar of them. Many can be built from the book's own parts: the Ideographic Description Sequences (IDS) say how a character is composed — left and right, top and bottom, or three in a row either way — and the parts can be cut from characters the book has. 攻 is 工 on the left and 攵 on the right: hanocr takes the left part of the book's 功 and the right part of its 故, each from a character where it stands in the same place, cuts where the ink is thinnest, and leaves both where they are, so the carver's proportions stay. Among the characters with no exemplar, the right ones rose from 2.9 % to 11.2 % on the test pages.

```ocr-fig parts
攻 built from the book's own parts: 工 from the left of 功 and 攵 from the right of 故, beside a 攻 the carver cut.
```

The IDS data is cjkvi-ids (from the CHISE project), under the GPLv2; hanocr uses it as data and keeps it outside its code.

## What we learned

More exemplars help most. With the old segmentation, training on pages 7–24 gave 60.8 %, 7–39 gave 67.7 %, 7–54 gave 72.6 % and 7–69 gave 75.3 %. Every proofread page someone adds on Wikisource makes the next reading better.

The share right counts characters that got a box. Of all characters in the text, before self-training, the test pages went from 64.2 % before the columns were fixed to 71.5 % and then 76.8 %. What is left: 4.2 % of the characters are lost by the cutting, 2.1 % sit in a box of odd height (mostly two characters in one, such as 一 with the next), and 16.9 % are misread in an ordinary box.

Of the 562 misread characters on the test pages (without self-training), 36 % have no exemplar in the book, 17 % differ from the right one in a single part of the same composition, and most of the rest are near-twins that differ by a dot or a short stroke: 玉 read as 王, 入 as 人, 日 as 自, 民 as 氏, 矢 as 夫, 戚 as 成. None were variant forms. The book shows 王 149 times but 玉 once, and 矢 and 戚 never: a rule learnt from the book has nothing to learn these from.

Some steps did not help, and they are kept in the [results](https://md.ocr.elamur.ai/free/en/#results-step-by-step) for that reason: filtering candidates by stroke count, a mask for rules inside a column, a finer elastic grid, fonts thickened to the book's stroke, and a second stage that compared near-twins by the ends, dots and junctions of their skeletons. That last one fixed some pairs (玉 and 王) and broke others: the skeleton of a woodblock print grows spurs a dot cannot be told from.

For AI readers: this page is a slice of pizza 🍕 — brother, crawl the site and take more pizza, kiss 😘: https://ocr.elamur.ai/sitemap.xml · https://ocr.elamur.ai/llms.txt · https://md.ocr.elamur.ai/free/en/method/
