跳到正文
DEV Community· hao jia·· 15 小時前AI 評分45

OCR 行分組應用方框中心還是底邊?兩者在 2° 傾斜時均失效

Box centre or bottom edge for OCR row grouping? Both broke at 2 of skew

AI 導讀

兩種以方框垂直中心或底邊進行 OCR 行分組的方法,在兩張無框表格傾斜至 2° 時均出現明顯退化;先估算頁面傾斜並校正 y 座標,則能改善分組結果。兩種方法在傾斜不超過 1° 時均將 9 行全部分對;不等高表格在 2° 時兩者均只分對 3 行。校正傾斜後以中心 y 分組,等高表格在測試的各個角度均分對 9 行。

正文

An OCR engine hands over text boxes with coordinates, and rebuilding a table starts with deciding which boxes sit on the same line. One design question is whether to cluster on each box's vertical centre or on its bottom edge, which looks closer to the line the text sits on. On two borderless tables rotated in steps up to 5°, the choice made almost no difference. Both anchors grouped 9 of 9 rows up to 1° and both fell apart at 2°. What held was estimating the page skew first and levelling y before clustering.

Two self-made tables with fictional data: a price quote of 9 rows and 7 columns without ruling lines, and the same content redrawn with row heights between 44 and 96 px. Each was rotated by 0.5, 1, 2, 3 and 5 degrees, pure rotation with no perspective. The boxes came from ImgIng https://imging.ai/ image text recognition at its default tier, on an Apple-silicon Mac in a desktop browser, exported as JSON with four corner coordinates per box. Each image was recognised once, on 2026-11-20. A row counts as correct only when the text of its boxes, joined, equals the source row exactly, and a program did the scoring. Tolerance is 0.5 × box height unless stated.

Both anchors grouped every row up to 1 degree

Rotation Centre y Bottom-edge y Chain to row tail Level first, then centre y
0°, 0.5°, 1° 9 9 9 9
2° 0 0 9 9
3° 0 0 9 9
5° 0 0 0 9

That table is the evenly spaced quote sheet, in correct rows out of 9. At 2° both anchors cut the page into 14 rows, at 3° and 5° into 15, with no row matching the source. The uneven-height sheet had the same shape: 9 of 9 through 1°, then 3 of 9 for both anchors at 2°, then 0.

"Chain to row tail" walks the boxes left to right and attaches each one to the row whose latest box is nearest in y. It lasted to 3° on both sheets and broke at 5°.

Bottom edges were the more scattered anchor

With no rotation, the boxes inside each row were compared on three sheets: a ruled score sheet, the quote sheet and its uneven-height variant. The largest spread of centre y within one row was 3.2, 3.2 and 3.0 px; for bottom-edge y it was 7.0, 5.5 and 7.0 px, with box heights from 35 to 51 px. On these samples the bottom edge was the more scattered anchor.

Estimating the skew first held through 5 degrees

The estimate uses only boxes wider than 2.5 × their own height, takes the angle of each one's top edge, and keeps the median. For true rotations of 0.5, 1, 2, 3 and 5° the quote sheet came out at −0.43, −1.09, −2.16, −3.15 and −5.03° (signs as recorded), a largest gap of 0.16°. Rotating the coordinates back by that angle and clustering on plain centre y gave 9 of 9 at every angle on the quote sheet. The uneven sheet at 0.5° was estimated as 0.0°; grouping still scored 9 of 9 there, and the cause was not looked into.

Below is a simplified sketch written from that definition. The numbers in this post come from a fuller script, and the corner order is an assumed input format.

import math, statistics

def page_tilt(quads):  # quad = (tl, tr, br, bl), each an (x, y) pair
    tilts = [math.atan2(tr[1] - tl[1], tr[0] - tl[0])
             for tl, tr, br, bl in quads
             if math.dist(tl, tr) > 2.5 * math.dist(tl, bl)]
    return statistics.median(tilts) if tilts else 0.0

def levelled_y(cx, cy, tilt):
    return cy * math.cos(tilt) - cx * math.sin(tilt)

A detection box has a bottom edge and no baseline

"Bottom edge" here means the lower side of the detection box, not the typographic baseline. The export gives each box as four corners, so a true baseline was not measured, and the comparison says nothing about baseline clustering versus centre clustering.

Two explanations here are inferred

The numbers above are measured, on two sheets with one recognition per image. Two explanations are inferred. First, why bottom edges scatter more: box height seems to follow the glyphs a box wraps, and if only the lower side moves, the centre moves half as much, which roughly fits 7.0 against 3.2 px. Second, the cliff between 1° and 2°: tan 2° is about 0.035, so across an assumed 1000 px of row width the far end sits about 35 px lower, roughly a box height, while 1° gives about 17 px, around half a box. That is arithmetic on an assumed width, not a measurement; image widths are not among the recorded figures.

For a pipeline that receives scans or photos, a safer order is to estimate the tilt from the wide boxes before arguing about anchors, and to level the coordinates when it is near or above 1°. Perspective from an angled phone shot was not tested, and no angle between 1° and 2° was sampled.

來源:DEV Community · dev.to