Text line segmentation

Also known as: text line detection

On this page 7

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.

RepresentationDescriptionStrengthTypical use
Bounding boxAxis-aligned rectangle per lineSimple and fastClean printed documents
Rotated boxRectangle with an angleHandles uniform skewPhotographed pages, receipts
Baseline polylinePoints along the line bottomFollows curved linesHandwriting, historical archives
Polygon maskOutline of the full line areaPrecise pixel coverageOverlapping or touching lines
Output representations for text 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 familyCore ideaStrengthsWeaknesses
Projection profilesRow sums of ink densityFast, no training dataFails on skew and curvature
Component groupingCluster characters into linesHandles moderate skewSensitive to noise and touching text
Seam carvingFind low-energy paths between linesFollows irregular gapsStruggles with overlapping strokes
Baseline detection networksPredict baseline pixels, then traceRobust on handwritingRequires annotated training pages
Instance segmentationPredict one mask per linePrecise boundariesHigher compute cost
Families of text line segmentation methods

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.

MeasureWhat it comparesInterpretation
Intersection over unionPredicted and true line areasShare of overlapping pixels per line
Detection F1 scoreMatched lines at an overlap thresholdBalance of missed and extra lines
Baseline precision and recallPredicted and true baselinesStandard in handwritten document competitions
End-to-end character error rateFinal text against the referenceCombined effect on transcription quality
Common evaluation measures

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 typeRecommended approachReason
Clean printed pagesBuilt-in OCR line detectorStraight lines need no special model
Phone photos of documentsRotated box or polygon detectorHandles skew and perspective
Modern handwritingPretrained baseline networkFollows fluctuating lines
Historical manuscriptsFine-tuned baseline modelAdapts to specific hands and layouts
Layout-heavy formsRegion detection, then linesSeparates fields before line splitting
Decision guide by document type

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.