Repository navigation
[utils] Crop the visible part of a box starting before the image in extract_crops - #2200
Conversation
felixdittrich92
left a comment
There was a problem hiding this comment.
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
…y before the image gives an empty crop
felixdittrich92
left a comment
There was a problem hiding this comment.
Thanks LGTM now 👍
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
extract_cropssliced the image with the box coordinates as they are, so a box starting before the image (a negativexminorymin) sliced from the end of the image and gave an empty crop: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