Skip to content

[utils] Crop the visible part of a box starting before the image in extract_crops - #2200

Merged
felixdittrich92 merged 2 commits into
mindee:mainfrom
RavSinghChandan:extract-crops-negative-start
Oct 30, 2026
Merged

felixdittrich92 merged 2 commits into
mindee:mainfrom
RavSinghChandan:extract-crops-negative-start

Conversation

@RavSinghChandan

Copy link
Copy Markdown
Contributor

extract_crops sliced the image with the box coordinates as they are, so a box starting before the image (a negative xmin or ymin) sliced from the end of the image and gave an empty crop:

>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> extract_crops(img, np.array([[-0.2, -0.1, 0.3, 0.4]]))[0].shape
(0, 0, 3)

This happens with crop_bboxes_from_image, which passes the absolute boxes of a dataset annotation as they are: a word box starting at x = -1 gave an empty image. The start of a box is now clamped to 0, so its visible part is cropped. Boxes inside the image (e.g. the clipped detection predictions) give the same crops as before

@felixdittrich92 felixdittrich92 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @RavSinghChandan Thanks for the PR 👍

Thanks for the fix! Boxes that start before the image are now cropped to their visible part (41×31 and 40×30 in your test cases instead of empty crops). One case is still wrong, and this PR makes it worse:

A box entirely before the image

Only the start is clamped, so a box whose end is also negative now slices img[0:-k], which is most of the image:

Box main This PR Expected
relative [-0.5, -0.5, -0.2, -0.2] on a 100×100 image 31×31 (from the end) 81×81 (from the top-left) empty
absolute [-50, -50, -20, -20] 30×30 80×80 empty

extract_crops is also used by crop_bboxes_from_image to build recognition datasets, so an annotation lying outside the image would produce a crop that doesn't match its label. Clamping all four coordinates fixes this, since a negative end becomes 0 and gives an empty crop:

    # A box starting before the image would slice from its end: crop its visible part instead (empty if none)
    _boxes = np.maximum(_boxes, 0)

Your test cases give the same results with this change. Could you also add a box entirely outside the image to the test, e.g.:

# A box entirely before the image gives an empty crop
assert geometry.extract_crops(gradient_img, np.array([[-50, -50, -20, -20]]))[0].size == 0

@felixdittrich92 felixdittrich92 self-assigned this Oct 30, 2026
@felixdittrich92 felixdittrich92 added this to the 1.2.0 milestone Oct 30, 2026
@felixdittrich92 felixdittrich92 added type: enhancement Improvement module: utils Related to doctr.utils ext: tests Related to tests folder labels Oct 30, 2026

@felixdittrich92 felixdittrich92 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks LGTM now 👍

@codecov

codecov Bot commented Oct 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.77%. Comparing base (2e274dc) to head (cde68b2).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2200      +/-   ##
==========================================
+ Coverage   97.75%   97.77%   +0.01%     
==========================================
  Files         170      170              
  Lines       10974    10976       +2     
==========================================
+ Hits        10728    10732       +4     
+ Misses        246      244       -2     
Flag Coverage Δ
unittests 97.77% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@felixdittrich92
felixdittrich92 merged commit 8e8210b into mindee:main Oct 30, 2026
57 checks passed
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 type: enhancement Improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants