Skip to content

fix(geometry): apply inclusive last-index per box in extract_crops#2071

Open
devteamaegis wants to merge 1 commit into
mindee:mainfrom
devteamaegis:fix/extract-crops-off-by-one
Open

fix(geometry): apply inclusive last-index per box in extract_crops#2071
devteamaegis wants to merge 1 commit into
mindee:mainfrom
devteamaegis:fix/extract-crops-off-by-one

Conversation

@devteamaegis

Copy link
Copy Markdown

What's broken

extract_crops is meant to add an inclusive +1 to each box's xmax/ymax ("Add last index"), but the increment lands on the wrong axis: _boxes[2:] += 1 adds 1 to all four coordinates of every box from batch index 2 onward, instead of adding 1 to the xmax/ymax columns of every box. So with 3+ boxes, every crop from index 2 on is cut from a region shifted one pixel in both x and y. The detection→recognition pipeline calls this with many boxes per page, so crops 3+ are silently misaligned.

img = np.tile(np.arange(100, dtype=np.uint8).reshape(100,1,1), (1,100,3))
box = [0.10,0.10,0.20,0.20]
crops = extract_crops(img, np.array([box,box,box], dtype=np.float32))
print([int(c[0,0,0]) for c in crops])   # [10, 10, 11]  -> 3rd identical box starts a row too low

Why it happens

_boxes[2:] += 1 indexes the box axis; it should be _boxes[:, 2:] += 1 (the coordinate columns).

Fix

Change _boxes[2:] += 1 to _boxes[:, 2:] += 1.

Test

Added a case to test_extract_crops using a vertical-gradient image: three identical boxes must yield identical crops regardless of batch position. Fails before, passes after.

extract_crops incremented the wrong axis (`_boxes[2:] += 1` shifted every
box from index 2 onward by +1 in all four coordinates), instead of adding
the inclusive +1 to the xmax/ymax columns of each box (`_boxes[:, 2:] += 1`).
This made crops positionally inconsistent: identical boxes at batch index >=2
sampled a region shifted by one pixel in x and y.
@felixdittrich92 felixdittrich92 self-assigned this Jun 11, 2026
@felixdittrich92 felixdittrich92 added type: bug Something isn't working ext: tests Related to tests folder topic: text recognition Related to the task of text recognition module: utils Related to doctr.utils labels Jun 11, 2026
@felixdittrich92 felixdittrich92 added this to the 1.1.0 milestone Jun 11, 2026
@felixdittrich92

Copy link
Copy Markdown
Collaborator

Hi @devteamaegis 👋

Thanks for the fix 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ext: tests Related to tests folder module: utils Related to doctr.utils topic: text recognition Related to the task of text recognition type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants