Text line segmentation, also called text line detection, is the process of locating and separating the individual lines of text in a document image so that each line can be passed to a recognition model. In Document AI, it is a core step of layout analysis that sits between page-level preprocessing, such as binarization and deskewing, and line-level text recognition by OCR or handwritten text recognition engines.
The task looks trivial on clean printed pages, where lines are straight and well separated, but it becomes one of the hardest parts of the pipeline for historical manuscripts, handwritten notes, curved book scans and photographs of documents. Errors made at this stage propagate directly into the transcription, because a recognizer that receives two merged lines or half a line cannot recover the correct text.
How text line segmentation works
A text line segmentation system takes a page or a text region as input and outputs a geometric description of every line. Depending on the method, this description is an axis-aligned bounding box, a rotated rectangle, a baseline polyline that follows the bottom of the characters or a polygon mask that encloses the full line including ascenders and descenders.
Modern pipelines usually first detect text regions, then segment lines inside each region, and finally determine the reading order of the lines. Each line image is cropped, often dewarped along its baseline, normalized to a fixed height and sent to a line recognizer. The table below shows the main output representations and where each is preferred.
| Representation | Description | Strength | Typical use |
|---|---|---|---|
| Bounding box | Axis-aligned rectangle per line | Simple and fast | Clean printed documents |
| Rotated box | Rectangle with an angle | Handles uniform skew | Photographed pages, receipts |
| Baseline polyline | Points along the line bottom | Follows curved lines | Handwriting, historical archives |
| Polygon mask | Outline of the full line area | Precise pixel coverage | Overlapping or touching lines |
Archival standards such as PAGE XML and ALTO store these geometries together with the recognized text, which allows later correction and search.
Classical and deep learning approaches
Early methods relied on projection profiles: summing dark pixels per row produces peaks for lines and valleys for gaps, and the valleys become cut positions. This works well for printed text with straight lines but fails on skewed, curved or touching handwriting. Later classical methods used connected component grouping, smearing algorithms, Hough transforms and seam carving to follow irregular lines.
Current systems are dominated by deep neural networks. Fully convolutional networks such as U-Net variants predict a pixel map of baselines or line regions, and post-processing turns the map into polylines. Object detection and instance segmentation models predict boxes or masks per line, and some end-to-end architectures combine detection and recognition in a single model.
| Method family | Core idea | Strengths | Weaknesses |
|---|---|---|---|
| Projection profiles | Row sums of ink density | Fast, no training data | Fails on skew and curvature |
| Component grouping | Cluster characters into lines | Handles moderate skew | Sensitive to noise and touching text |
| Seam carving | Find low-energy paths between lines | Follows irregular gaps | Struggles with overlapping strokes |
| Baseline detection networks | Predict baseline pixels, then trace | Robust on handwriting | Requires annotated training pages |
| Instance segmentation | Predict one mask per line | Precise boundaries | Higher compute cost |
Difficulties that affect line segmentation quality
Most failures come from irregular document geometry and script properties. Handwritten lines fluctuate in direction, ascenders and descenders of neighboring lines touch or overlap, and marginal notes or interlinear corrections break the regular rhythm of the page. Page curvature near the spine of a bound book bends lines, and photographs add perspective distortion.
- Touching and overlapping lines: long descenders cross into the next line.
- Curved or fluctuating baselines: lines drift up or down across the page.
- Mixed layouts: columns, tables, marginalia and stamps on the same page.
- Degraded material: bleed-through, stains and faded ink create false lines.
- Script differences: Arabic, Devanagari and vertical East Asian text need adapted models.
Because a recognition model trained on full lines cannot read fragments, over-segmentation and under-segmentation are both costly, and their effect appears directly as a higher character error rate.
Evaluating text line segmentation
Evaluation compares predicted lines with ground-truth annotations made by humans. Region-based metrics measure pixel or polygon overlap, while baseline-based metrics measure how closely predicted baselines match reference baselines within a tolerance. The most informative evaluation, however, is often downstream: the character error rate of the full recognition pipeline.
| Measure | What it compares | Interpretation |
|---|---|---|
| Intersection over union | Predicted and true line areas | Share of overlapping pixels per line |
| Detection F1 score | Matched lines at an overlap threshold | Balance of missed and extra lines |
| Baseline precision and recall | Predicted and true baselines | Standard in handwritten document competitions |
| End-to-end character error rate | Final text against the reference | Combined effect on transcription quality |
Choosing a line segmentation approach in practice
The right choice depends on document type, volume and available training data. Modern printed business documents are usually handled well by the line detector built into standard OCR engines such as Tesseract or PaddleOCR. Historical and handwritten collections justify trainable baseline models, which platforms such as Transkribus allow archives to fine-tune on a few dozen annotated pages.
| Document type | Recommended approach | Reason |
|---|---|---|
| Clean printed pages | Built-in OCR line detector | Straight lines need no special model |
| Phone photos of documents | Rotated box or polygon detector | Handles skew and perspective |
| Modern handwriting | Pretrained baseline network | Follows fluctuating lines |
| Historical manuscripts | Fine-tuned baseline model | Adapts to specific hands and layouts |
| Layout-heavy forms | Region detection, then lines | Separates fields before line splitting |
OCR-free document understanding models skip explicit line segmentation, but line-level pipelines remain preferred where transcriptions must be aligned with exact image coordinates for search, redaction or scholarly editing.
Put simply
A computer that reads a page usually does not read it all at once. It first cuts the page into single lines of writing. Then it reads one line at a time. Text line segmentation is the cutting step.
Think of a person running a ruler under each line of an old letter. If the ruler slips between two lines, the reading goes wrong. Good cutting makes good reading possible, especially for messy handwriting.
In practice
A regional archive wants to make 40,000 pages of nineteenth-century parish registers searchable. The pages are handwritten in several hands, with curved lines, marginal notes and ink bleed-through. A first test with a standard printed-text OCR engine merges many lines and produces unusable text.
The digitization team annotates baselines on 80 representative pages and fine-tunes a baseline detection network on them. The new model finds about 98 percent of lines correctly, and a handwriting recognizer trained on the same registers then transcribes each cropped line.
The end-to-end character error rate falls from above 30 percent to about 8 percent. That is accurate enough for name searches, and genealogists can now find baptism and marriage entries in seconds instead of browsing scanned images page by page.