Add Grounded SAM2 Interactive Image Segmentation to Computer Vision#13791
Open
balaraj74 wants to merge 6 commits intoTheAlgorithms:masterfrom
Open
Add Grounded SAM2 Interactive Image Segmentation to Computer Vision#13791balaraj74 wants to merge 6 commits intoTheAlgorithms:masterfrom
balaraj74 wants to merge 6 commits intoTheAlgorithms:masterfrom
Conversation
- Implement partition-based divide-and-conquer solution - Time complexity: O(log(min(m, n))) - Space complexity: O(1) - Handles empty arrays, integers, floats, and negative numbers - Includes comprehensive doctests for edge cases - Fixes TheAlgorithms#13717
Add Median of Two Sorted Arrays Algorithm to Divide and Conquer Section
- Implement interactive segmentation with multiple prompt types - Support point-based prompts (positive/negative) - Support bounding box prompts - Support text-grounded prompts - Include mask visualization with color overlay - Add comprehensive doctests (31 tests, all passing) - Include demonstration function showing all features - Full type hints and detailed documentation Fixes TheAlgorithms#13516
Use adaptive padding based on box size to handle small bounding boxes better.
for more information, see https://pre-commit.ci
Sort imports alphabetically (typing before numpy) to comply with ruff/isort rules.
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.
🎯 What I Did
Hey there! I've implemented Grounded SAM2 Image Segmentation for the computer vision section - a powerful interactive segmentation tool that demonstrates modern segmentation techniques with multiple prompt types.
Quick Overview
This adds a flexible, educational image segmentation implementation that works with three different prompt types:
The implementation is designed to be educational, showing learners how modern AI segmentation models like SAM2 work and how they can be integrated into practical workflows.
📂 What's Included
File Added:
computer_vision/grounded_sam2_segmentation.py(379 lines)Key Features:
🔧 Implementation Details
Class:
GroundedSAM2SegmenterMain Methods:
segment_with_points(point_coords, point_labels)segment_with_box(bbox)segment_with_text(text_prompt, confidence_threshold)apply_color_mask(image, mask, color, alpha)Design Philosophy:
✅ Testing & Validation
Doctests: 31 tests, 100% passing ✨
$ python3 -m doctest computer_vision/grounded_sam2_segmentation.py -v ... 31 tests in 9 items. 31 passed and 0 failed. Test passed.Test Coverage:
Demonstration Output:
$ python3 computer_vision/grounded_sam2_segmentation.py ============================================================ Grounded SAM2 Segmentation Demonstration ============================================================ 1. Point-based segmentation Generated mask shape: (200, 200) Segmented pixels: 7245 2. Bounding box segmentation Generated mask shape: (200, 200) Segmented pixels: 8100 3. Text-grounded segmentation Detected objects: 1 Object 1: - Label: object in center - Confidence: 0.85 - BBox: (50, 50, 150, 150) - Mask pixels: 7845 4. Visualization Result image shape: (200, 200, 3)📚 Why This Matters
Educational Value:
Practical Applications:
Modern CV Concepts:
📋 Contribution Checklist
Describe your change:
Requirements Met:
computer_vision/) ✅grounded_sam2_segmentation.py✅GroundedSAM2Segmenter(PascalCase) ✓segment_with_points,apply_color_mask(snake_case) ✓mask_threshold,point_coords(snake_case) ✓list[tuple[int, int]], etc.)🔗 References
🙏 Acknowledgments
Thanks to @NANDAGOPALNG for requesting this feature! This implementation provides a solid foundation for understanding how modern interactive segmentation systems work, making cutting-edge computer vision concepts accessible to learners.
Ready for review! Happy to make any adjustments based on maintainer feedback. 😊
Fixes #13516