A computer vision pipeline for automated diamond image segmentation using GrabCut algorithm with CLAHE preprocessing. This project segments diamonds from background images across 14 different shape categories.
This project was developed as part of the MiNeD Hackathon hosted by Nirma University. The system implements a comprehensive automated segmentation pipeline for diamond images, processing over 57,000 images across multiple dataset variants.
- Rushir Bhavsar - Lead Developer
- Harshil Sanghvi - Core Developer
- Ruju Shah - Core Developer
- Vrunda Shah - Core Developer
- Khushi Patel - Core Developer
Our segmentation pipeline demonstrates robust performance across different diamond shapes. Each video shows the complete processing pipeline: Original → CLAHE Enhanced → Segmented.
- Overview
- Features
- Dataset
- Installation
- Quick Start
- Usage
- Pipeline Architecture
- Video Generation
- Results Analysis
- Performance
- Project Structure
- License
- Acknowledgments
This project implements an automated segmentation pipeline for diamond images using OpenCV's GrabCut algorithm enhanced with CLAHE (Contrast Limited Adaptive Histogram Equalization) preprocessing. The system processes three dataset variants:
- Shape_1d_256i: Single diamond variation (256 images per shape)
- Shape_5d_256i: Five diamond variations (1,280 images per shape)
- Shape_10d_256i: Ten diamond variations (2,560 images per shape)
Total Dataset: 57,344 images across 14 diamond shape categories
| Category | Features |
|---|---|
| Segmentation | GrabCut-based background removal CLAHE preprocessing for enhanced contrast Morphological operations for mask refinement Contour detection and bounding box annotation Multi-threaded batch processing Comprehensive error handling and logging |
| Video Generation | Triple-split videos (Before | CLAHE | After) Five-split comparison videos (5 variations side-by-side) Mask evolution animations Pipeline step-by-step demonstrations Dataset coverage visualizations |
| Analysis & Reporting | Quality metrics calculation Statistical analysis by shape category Automated report generation (JSON/CSV) Comparison charts and distribution plots Performance profiling and benchmarking |
| Developer Tools | Interactive processing mode Command-line interface (CLI) Jupyter notebooks for exploration Performance profiling utilities Structured logging with rotation Configurable parameters via YAML |
┌─────────────────────────────────────────────────────────────┐
│ Input Image (256x256) │
└──────────────────────┬──────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ CLAHE Enhancement │ Clip Limit: 2.5 │ Grid: 8×8 │
└──────────────────────┬──────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ GrabCut Init │ Border-based mask (20px margin) │
└──────────────────────┬──────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ GrabCut Iterations │ 5× iterations with mask refinement │
└──────────────────────┬──────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Post-processing │ Morphological operations & cleanup │
└──────────────────────┬──────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Output │ Binary mask + Segmented image │
└─────────────────────────────────────────────────────────────┘
| Code | Shape Name | Description |
|---|---|---|
| AS | Asscher | Square step cut with cropped corners |
| BR | Brilliant | Round brilliant cut |
| CMB | Combination | Mixed faceting styles |
| EM | Emerald | Rectangular step cut |
| HS | Heart | Heart-shaped cut |
| MQ | Marquise | Elongated pointed ends |
| OV | Oval | Elliptical shape |
| PE | Pear | Teardrop shape |
| PR | Princess | Square brilliant cut |
| PS | Pearshape | Pear-shaped variation |
| RA | Radiant | Rectangular brilliant cut |
| RD | Round | Traditional round cut |
| SEM | Semi | Semi-faceted style |
| TRI | Triangle | Triangular cut |
data/
├── raw/
│ ├── Shape_1d_256i/
│ │ ├── AS/ (256 images)
│ │ ├── BR/ (256 images)
│ │ └── ...
│ ├── Shape_5d_256i/
│ │ ├── AS/ (1,280 images)
│ │ ├── BR/ (1,280 images)
│ │ └── ...
│ └── Shape_10d_256i/
│ ├── AS/ (2,560 images)
│ ├── BR/ (2,560 images)
│ └── ...
└── processed/
├── segmented/
├── masks/
├── annotated/
└── stats/
- Python 3.9 or higher
- pip package manager
- Virtual environment (recommended)
git clone https://github.com/yourusername/diamond-shape-segmentation.git
cd diamond-shape-segmentationpython -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activatepip install -r requirements.txtpython -m src.main info --data-path data/raw --variant Shape_1d_256i# Process Asscher shape (AS) with default settings
python -m src.main process --shape AS --variant Shape_1d_256i# Process all 14 shapes with custom iterations
python -m src.main process --all --variant Shape_1d_256i --iterations 5# Generate all demo videos for selected shapes
python scripts/generate_demo_videos.py --shapes AS BR EM MQ OV# Launch interactive processing interface
python -m src.main interactive# Analyze segmentation quality
python scripts/analyze_results.py --input data/processed --output analysis/# Show dataset info and validate structure
python -m src.main info --data-path data/raw --variant Shape_1d_256i --validate# Basic processing
python -m src.main process --shape AS --variant Shape_1d_256i
# With annotations and masks
python -m src.main process --shape BR --annotations --save-masks
# Process all shapes with profiling
python -m src.main process --all --profile --iterations 5
# Debug mode with verbose logging
python -m src.main process --shape EM --debug --log-dir logs/# Triple-split video (Before | CLAHE | After)
python -m src.main video triple-split --shape AS --output videos/
# Five-split video (5 variations)
python -m src.main video five-split --shape BR --output videos/
# Generate all demo videos
python -m src.main video generate-demos --shapes AS BR EM MQ OV# Run benchmark on 50 images
python -m src.main benchmark --num-images 50 --iterations 5from src.data.loader import DiamondDataLoader
from src.pipe.processor import DiamondProcessor
# Initialize loader
loader = DiamondDataLoader('data/raw', 'Shape_1d_256i')
# Create processor
processor = DiamondProcessor(
data_loader=loader,
output_dir='data/processed',
iterations=5
)
# Process a shape category
results = processor.process_shape_category(
shape_id=1, # Asscher
max_images=100
)
print(f"Processed: {results['processed']}")
print(f"Failed: {results['failed']}")from src.pipe.video_comparison import VideoComparator
# Create triple-split video
comparator = VideoComparator('output/video.avi', fps=15)
video_path = comparator.create_triple_split(
image_paths=['img1.png', 'img2.png', ...],
iterations=5,
add_labels=True
)from src.utils.profiling import PerformanceProfiler
profiler = PerformanceProfiler()
# Measure operation
with profiler.measure('segmentation', metadata={'iterations': 5}):
# Your segmentation code here
pass
# Print summary
profiler.print_summary()
# Save metrics
profiler.save_metrics('metrics.json', format='json')The project includes three interactive notebooks:
01_data_exploration.ipynb: Dataset visualization and exploration02_diamond_segmentation.ipynb: Complete segmentation pipeline demo03_video_generation.ipynb: Video creation and comparisons
# Launch Jupyter
jupyter notebook notebooks/Input Image (256x256)
↓
1. CLAHE Enhancement
- Clip Limit: 2.5
- Grid Size: 8×8
↓
2. GrabCut Initialization
- Border-based mask (20px margin)
- Probable foreground: center region
- Probable background: border region
↓
3. GrabCut Iterations (5×)
- Gaussian Mixture Models (GMM)
- Graph Cut optimization
- Mask refinement
↓
4. Post-processing
- Morphological operations
- Noise reduction
↓
5. Final Segmentation
- Binary mask
- Segmented image
- Optional annotations
| Stage | Input | Output | Purpose |
|---|---|---|---|
| Load | Raw image | BGR image | Read from disk |
| Preprocess | BGR image | Enhanced BGR | Improve contrast |
| Initialize | Enhanced image | Initial mask | Setup GrabCut |
| Segment | Enhanced + mask | Binary mask | Remove background |
| Refine | Binary mask | Clean mask | Morphological ops |
| Annotate | Segmented image | Annotated image | Add contours/boxes |
| Save | Final image | Disk file | Store results |
Shows the complete pipeline: Original → CLAHE Enhanced → Segmented
python -m src.main video triple-split --shape AS --fps 15 --output videos/Features:
- Side-by-side comparison
- Stage labels
- Progress indicators
- 256 frames (one per image)
Demonstrates algorithm robustness across 5 variations of the same shape
python -m src.main video five-split --shape BR --fps 15 --output videos/Features:
- 5 variations in parallel
- Synchronized playback
- Variation labels
- Grid layout (1×5)
- Mask Evolution: Shows GrabCut refinement over iterations
- Pipeline Steps: Step-by-step algorithm demonstration
- Coverage Map: Dataset processing visualization
The analysis script calculates the following metrics:
- Foreground Percentage: Ratio of diamond to total pixels
- Mean Intensity: Average brightness of segmented region
- Contrast: Standard deviation of pixel intensities
- Edge Density: Percentage of edge pixels
- Sharpness: Laplacian variance
python scripts/analyze_results.py --input data/processed --output analysis/Outputs:
analysis_report.json: Detailed JSON reportanalysis_summary.csv: Summary tablequality_comparison.png: Bar charts by shapemetric_distributions.png: Histograms
Overall Statistics:
Total images processed: 3,584
Shapes analyzed: 14
Avg foreground: 68.45%
Avg contrast: 52.31
Avg sharpness: 145.67
Hardware: Intel Core i7, 16GB RAM, No GPU
| Operation | Time (ms) | Throughput |
|---|---|---|
| Load Image | 5.2 | 192 img/s |
| CLAHE Preprocess | 12.4 | 81 img/s |
| GrabCut (5 iter) | 245.8 | 4.1 img/s |
| Total Pipeline | 268.5 | 3.7 img/s |
- Reduce Iterations: Use 3-4 iterations for faster processing
- Batch Processing: Process multiple images in parallel
- Skip Annotations: Disable when not needed
- Lower Resolution: Resize images if acceptable
# Fast processing (minimal quality loss)
python -m src.main process --all --iterations 3 --max-images 100diamond-shape-segmentation/
├── src/
│ ├── __init__.py
│ ├── main.py # CLI entry point
│ ├── config.py # Configuration management
│ ├── data/
│ │ ├── __init__.py
│ │ └── loader.py # Dataset loading
│ ├── pipe/
│ │ ├── __init__.py
│ │ ├── processor.py # Batch processing
│ │ ├── video_creator.py # Video generation
│ │ ├── video_comparison.py # Comparison videos
│ │ └── animation.py # Animations
│ └── utils/
│ ├── __init__.py
│ ├── file_utils.py # File operations
│ ├── segmentation.py # Core algorithms
│ ├── visualization.py # Drawing utilities
│ ├── profiling.py # Performance tools
│ └── logging_config.py # Logging setup
├── scripts/
│ ├── __init__.py
│ ├── generate_demo_videos.py # Demo generation
│ ├── analyze_results.py # Results analysis
│ └── quick_demo.sh # Quick demo script
├── notebooks/
│ ├── 01_data_exploration.ipynb
│ ├── 02_diamond_segmentation.ipynb
│ └── 03_video_generation.ipynb
├── data/
│ ├── raw/ # Input datasets
│ └── processed/ # Output results
├── tests/
│ └── (unit tests)
├── config.example.yaml # Configuration template
├── requirements.txt # Dependencies
├── README.md # This file
├── LICENSE # MIT License
└── .gitignore # Git ignore rules
This project is licensed under a Custom Research and Educational License.
Key Points:
- View and study the code freely
- Use for educational purposes
- Reference in academic papers
- Copying/forking requires written permission
- Commercial use requires written permission
- Modification and redistribution require written permission
To request permission: Contact rushirbhavsar@gmail.com
See the LICENSE file for complete terms.
- OpenCV community for the GrabCut implementation
- Nirma University for hosting the MiNeD Hackathon
- Diamond dataset providers
- Academic advisors and mentors
- Open-source contributors
-
Rother, C., Kolmogorov, V., & Blake, A. (2004). "GrabCut: Interactive foreground extraction using iterated graph cuts." ACM Transactions on Graphics, 23(3), 309-314.
-
Pizer, S. M., et al. (1987). "Adaptive histogram equalization and its variations." Computer Vision, Graphics, and Image Processing, 39(3), 355-368.
-
Bradski, G. (2000). "The OpenCV Library." Dr. Dobb's Journal of Software Tools.
For questions, issues, or suggestions:
- Email: rushirbhavsar@gmail.com
- Issues: GitHub Issues
- Discussions: GitHub Discussions
Developed by Team MiNeD at Nirma University
Last Updated: March 11, 2022

