Fix overlap filter to use segmentation masks when available (#1987)
What does this PR do?
This PR fixes the Overlap Filter workflow block (roboflow_core/overlap@v1) so that instance segmentation predictions use masks for overlap checks, rather than implicitly falling back to bounding-box-only overlap.
Before this change, OverlapBlockV1.run() only used predictions.xyxy, which could produce false positives for segmented objects with irregular shapes (their bounding boxes overlap even when the masks do not).
This PR:
- Uses pixel-level mask overlap when segmentation masks are present (and consistent with detection length)
- Keeps the existing bbox overlap behavior as a fallback when masks are not present
- Makes output ordering deterministic by sorting indices before indexing into
sv.Detections - Adds/updates unit tests covering the regression and both mask/bbox code paths
Related Issue(s): Fixes #1987
Type of Change
- Bug fix (non-breaking change that fixes an issue)
Testing
- [x] I have tested this change locally
- [x] I have added/updated tests for this change
Test details:
- Updated/expanded unit tests for the overlap block to include:
- A regression test demonstrating the original false-positive case for instance segmentation (bbox overlap but masks do not overlap)
- Direct tests for the new mask overlap helper (
Center OverlapandAny Overlap) - A fallback test verifying bbox-only behavior still works when masks are absent
- Existing sanity tests for true-positive overlap and no-overlap scenarios
Example command run locally:
pytest tests/workflows/unit_tests/core_steps/analytics/test_coords_overlap_v1.py -v
Checklist
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code where necessary, particularly in hard-to-understand areas
- [ ] My changes generate no new warnings or errors
- [x] I have updated the documentation accordingly (if applicable)
Additional Context
I’m new to the repo, so please let me know if you’d prefer this behavior behind a new version (e.g. @v2) or if the current approach (fixing @v1 since it already accepts instance segmentation predictions) is aligned with the project’s versioning expectations. Happy to adjust based on maintainer feedback.
Good Day, Can you please look into the PR and let me know if any other changes are required.
Thanks and Regards. Nishant.
hi @Nishant-ZFYII, thank you for this contribution! I have ran your PR through Claude and got one suggestion that I think makes sense in terms of performance, please let me know what are your thoughts about this.
Sorry for the long silence on this — took me way too long to circle back. Thanks for the suggestion though, it makes sense and it's done. I applied the bbox-intersection approach so we only run logical_and over the overlapping region and bail early when the boxes don't intersect.
One small tweak on top: since xyxy comes through as floats, I floor/ceil and cast to int before slicing (and clamp to the mask bounds) — without that the slice throws on real detections. Passed the params as named args like you suggested and updated the tests.
Happy to change anything else if needed!