{"slug": "box-centre-or-bottom-edge-for-ocr-row-grouping-both-broke-at-2-of-skew", "title": "Box centre or bottom edge for OCR row grouping? Both broke at 2 of skew", "summary": "A developer tested whether OCR text boxes should be grouped into rows by their vertical centre or bottom edge, finding both anchors performed identically: 9 of 9 rows correct up to 1° of page rotation, then failing at 2°. Estimating page skew from wide boxes' top edges and levelling the y-coordinates before clustering restored 9 of 9 rows at every rotation tested up to 5°, with the skew estimate within 0.16° of the true angle.", "body_md": "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.\n\nTwo 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/](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.\n\n| Rotation | Centre y | Bottom-edge y | Chain to row tail | Level first, then centre y | \n|---|---|---|---|---|\n| 0°, 0.5°, 1° | 9 | 9 | 9 | 9 | \n| 2° | 0 | 0 | 9 | 9 | \n| 3° | 0 | 0 | 9 | 9 | \n| 5° | 0 | 0 | 0 | 9 | \n\nThat 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.\n\n\"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°.\n\nWith 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.\n\nThe 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.\n\nBelow 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.\n\n``` python\nimport math, statistics\n\ndef page_tilt(quads):  # quad = (tl, tr, br, bl), each an (x, y) pair\n    tilts = [math.atan2(tr[1] - tl[1], tr[0] - tl[0])\n             for tl, tr, br, bl in quads\n             if math.dist(tl, tr) > 2.5 * math.dist(tl, bl)]\n    return statistics.median(tilts) if tilts else 0.0\n\ndef levelled_y(cx, cy, tilt):\n    return cy * math.cos(tilt) - cx * math.sin(tilt)\n```\n\n\"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.\n\nThe 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.\n\nFor 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.", "url": "https://wpnews.pro/news/box-centre-or-bottom-edge-for-ocr-row-grouping-both-broke-at-2-of-skew", "canonical_source": "https://dev.to/iterandum/box-centre-or-bottom-edge-for-ocr-row-grouping-both-broke-at-2deg-of-skew-37l8", "published_at": "2026-11-21 08:03:40+00:00", "updated_at": "2026-11-21 08:12:10.374668+00:00", "lang": "en", "topics": ["computer-vision", "ai-tools"], "entities": ["ImgIng"], "also_reported_by": [], "takeaway": "Estimating page skew from boxes wider than 2.5× their height and levelling y before clustering kept 9 of 9 OCR rows correct at rotations up to 5°, where centre-y and bottom-edge clustering both collapsed at 2°.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/box-centre-or-bottom-edge-for-ocr-row-grouping-both-broke-at-2-of-skew", "markdown": "https://wpnews.pro/news/box-centre-or-bottom-edge-for-ocr-row-grouping-both-broke-at-2-of-skew.md", "text": "https://wpnews.pro/news/box-centre-or-bottom-edge-for-ocr-row-grouping-both-broke-at-2-of-skew.txt", "jsonld": "https://wpnews.pro/news/box-centre-or-bottom-edge-for-ocr-row-grouping-both-broke-at-2-of-skew.jsonld"}}