Optimize mask annotation ROI blending#2368
Open
Borda wants to merge 3 commits into
Open
Conversation
Blend MaskAnnotator overlays only within the touched mask ROI while preserving dense and CompactMask rendering semantics. Add ROI coverage tests for dense masks, CompactMask masks, and all-false masks, and clarify the compact-mask benchmark annotation speedup output. Co-authored-by: Codex <codex@openai.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #2368 +/- ##
=======================================
Coverage 83% 83%
=======================================
Files 69 69
Lines 9616 9669 +53
=======================================
+ Hits 7941 7989 +48
- Misses 1675 1680 +5 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request optimizes MaskAnnotator.annotate by limiting blending work (cv2.addWeighted) to the minimal region-of-interest (ROI) that actually contains mask pixels, reducing unnecessary full-frame blending for sparse masks. It also extends the shared mask-painting utility to support painting into a sub-canvas with an absolute origin, and adds tests/benchmark wording to validate and explain the new behavior.
Changes:
- Compute a minimal ROI for dense and
CompactMaskmasks and blend only that ROI inMaskAnnotator.annotate. - Extend
_paint_masks_by_area(...)with acanvas_originparameter so masks can be painted correctly into an ROI-sized canvas. - Add tests asserting blending happens only on the ROI (and is skipped for all-false masks), and clarify benchmark reporting text/labels.
Review scores (n/5):
- Code quality: 5/5
- Testing: 5/5
- Documentation/clarity: 5/5
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/supervision/annotators/core.py |
Adds ROI computation helpers, supports sub-canvas mask painting via canvas_origin, and blends only the ROI in MaskAnnotator. |
tests/annotators/test_core.py |
Adds regression tests verifying ROI-only blending for dense/compact masks and skipping blending for all-false masks. |
examples/compact_mask/benchmark.py |
Updates benchmark docstring/table labels to better attribute annotation speedups to ROI-only blending. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces a significant optimization to the
MaskAnnotatorclass, ensuring that mask blending is limited to only the region of interest (ROI) touched by the mask(s), rather than always blending the full image. This change improves annotation speed, especially for large images with sparse masks, and reduces unnecessary computation. The update includes supporting utility functions, test coverage for the new behavior, and improved documentation and reporting in the benchmark script.Mask annotation ROI optimization:
MaskAnnotator.annotateto blend only the minimal bounding rectangle (ROI) covering all true mask pixels, both for dense and compact masks, instead of the entire image. This is achieved by computing the ROI with new helper functions and passing the ROI to the blending operation._paint_masks_by_areafunction to support painting into a subregion of the canvas with a newcanvas_originparameter, aligning mask coordinates with the ROI. [1] [2] [3] [4]Utility functions for ROI calculation:
_mask_to_roi,_compact_masks_to_roi, and_masks_to_roifunctions to compute the minimal bounding box for true mask pixels in dense and compact masks.Testing:
tests/annotators/test_core.pyto verify that blending occurs only within the mask ROI for both dense and compact masks, and that no blending occurs when masks are all-false.Documentation and reporting: