A comprehensive Python application that combines state-of-the-art brain MRI segmentation models with AI-powered medical analysis. Built with object-oriented programming best practices and designed for both research and educational purposes.
- 🔬 Multiple Segmentation Models: Choose from 5 different open-source brain segmentation models
- 🤖 AI-Powered Analysis: Get detailed medical explanations using LLMs (NVIDIA NIM or Anthropic Claude)
- 📊 Advanced Visualization: Multi-view segmentation overlays with color-coded regions
- 📁 Flexible Input: Load real NIfTI files or generate synthetic MRI data
- 🎯 Abnormality Detection: Automatic identification of enlarged ventricles, lesions, and tumors
- 💾 Export Options: Save visualizations and detailed reports
- 🏗️ OOP Architecture: Clean, extensible, and maintainable codebase
The system generates comprehensive visualizations showing:
- Original MRI in three anatomical planes (Axial, Sagittal, Coronal)
- Segmentation overlays with color-coded brain regions
- Abnormality highlighting with clinical significance markers
python >= 3.8- Clone the repository
git clone https://github.com/yourusername/brain-mri-segmentation.git
cd brain-mri-segmentation- Install dependencies
pip install numpy nibabel matplotlib requests anthropic- Run the application
python brain_segmentation.py| Model | Speed | Accuracy | Best For | Installation |
|---|---|---|---|---|
| SynthSeg ⭐ | ⚡⚡⚡⚡ | ⭐⭐⭐⭐ | General use, any MRI contrast | pip install freesurfer |
| FastSurfer | ⚡⚡⚡⚡⚡ | ⭐⭐⭐⭐ | Speed priority, GPU acceleration | pip install fastsurfer |
| HD-BET | ⚡⚡⚡⚡ | ⭐⭐⭐⭐ | Tumor/lesion detection | pip install HD-BET |
| nnU-Net 🏆 | ⚡⚡⚡ | ⭐⭐⭐⭐⭐ | Research, maximum accuracy | pip install nnunet |
| MONAI | ⚡⚡⚡ | ⭐⭐⭐⭐ | Custom workflows, extensibility | pip install monai |
- Description: Domain-agnostic segmentation that works on any MRI contrast
- Speed: ~1-2 minutes per scan
- Strengths: No retraining needed, robust to scan variations
- Paper: SynthSeg: Domain Randomisation for Segmentation
- GitHub: FreeSurfer
- Description: Deep learning alternative to FreeSurfer (100x faster)
- Speed: Sub-minute with GPU
- Strengths: High accuracy cortical parcellation, GPU optimized
- Paper: FastSurfer - A fast and accurate deep learning based neuroimaging pipeline
- GitHub: Deep-MI/FastSurfer
- Description: Brain extraction and tumor segmentation
- Speed: Fast inference
- Strengths: Excellent for pathology, robust to tumors
- GitHub: MIC-DKFZ/HD-BET
- Description: State-of-the-art medical image segmentation framework
- Speed: Moderate
- Strengths: Self-configuring, winner of multiple challenges
- Paper: nnU-Net: Self-adapting Framework for U-Net-Based Medical Image Segmentation
- GitHub: MIC-DKFZ/nnUNet
- Description: Medical Open Network for AI - comprehensive framework
- Speed: Varies by model
- Strengths: Multiple pre-trained models, highly extensible
- GitHub: Project-MONAI/MONAI
Get detailed medical analysis using Meta Llama 3.1 70B:
- Visit build.nvidia.com
- Sign up for a free account (no credit card required)
- Generate your free API key
- Enter the key when prompted
Supported Models:
- Meta Llama 3.1 70B/405B Instruct
- Mistral Large
- Microsoft Phi-3
- Google Gemma
For Claude-powered analysis:
- Requires Anthropic API key
- Get key from: console.anthropic.com
- Uses Claude Sonnet 4 for medical analysis
No API key needed - provides structured medical reports without AI analysis.
from brain_segmentation import BrainSegmentationPipeline, SynthSegModel, MedicalLLMAnalyzer, LLMProvider
# Initialize components
seg_model = SynthSegModel()
llm_analyzer = MedicalLLMAnalyzer(
provider=LLMProvider.NVIDIA_NIM,
api_key="your-nvidia-key"
)
# Create pipeline
pipeline = BrainSegmentationPipeline(seg_model, llm_analyzer)
# Load and analyze MRI
mri_data = pipeline.load_mri("path/to/brain_mri.nii.gz")
report = pipeline.run_analysis(mri_data)
# Display results
pipeline.print_report(report)
pipeline.visualize_segmentation()# Generate synthetic brain MRI
pipeline = BrainSegmentationPipeline(SynthSegModel())
mri_data = pipeline.generate_random_mri(shape=(256, 256, 128))
# Analyze
report = pipeline.run_analysis(mri_data)
pipeline.visualize_segmentation(output_dir="results")from brain_segmentation import *
models = [
SynthSegModel(),
FastSurferModel(),
HDBETModel()
]
mri_data = pipeline.load_mri("scan.nii.gz")
for model in models:
pipeline = BrainSegmentationPipeline(model)
report = pipeline.run_analysis(mri_data)
print(f"\n{model.model_name} Results:")
pipeline.print_report(report)- Abstract Base Classes:
BaseSegmentationModeldefines interface for all segmentation models - Strategy Pattern: Interchangeable segmentation models and LLM providers
- Data Classes:
SegmentationResultfor clean data representation - Enumerations: Type-safe brain regions and provider selection
- Pipeline Pattern:
BrainSegmentationPipelineorchestrates the workflow
BaseSegmentationModel (ABC)
├── SynthSegModel
├── FastSurferModel
├── HDBETModel
├── nnUNetModel
└── MONAIModel
MedicalLLMAnalyzer
├── NVIDIA NIM Integration
├── Anthropic Claude Integration
└── Basic Reporting
BrainSegmentationPipeline
├── load_mri()
├── generate_random_mri()
├── run_analysis()
├── visualize_segmentation()
└── print_report()
The system generates the following outputs:
output/
└── segmentation_results.png # Multi-view visualization with overlays
- Segmentation Results: Volume measurements for each brain region
- Abnormality Detection: Highlighted findings with confidence scores
- Clinical Analysis: AI-generated medical interpretation
- Recommendations: Suggested follow-up actions
- Model Information: Details about the segmentation model used
- White Matter: Central brain tissue
- Gray Matter: Cortical tissue
- Cerebrospinal Fluid (CSF): Fluid surrounding brain
- Ventricles: Fluid-filled cavities
- Lesions: Abnormal tissue areas
- Tumors: Abnormal growth masses
The system automatically flags:
- Enlarged ventricles (hydrocephalus indicators)
- Tissue lesions (potential MS, stroke, etc.)
- Tumors or masses
- Abnormal tissue volumes
- Asymmetries
This project can be used for:
- Educational purposes in medical imaging
- Research on brain segmentation algorithms
- Development of clinical decision support tools
- Comparison studies between segmentation models
- Integration into larger medical imaging pipelines
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Additional segmentation models
- More LLM provider integrations
- 3D volume rendering
- DICOM file support
- Web interface
- Docker containerization
- Additional visualization options
- Performance optimizations
This project is licensed under the MIT License - see the LICENSE file for details.
- FreeSurfer team for SynthSeg
- Deep-MI for FastSurfer
- MIC-DKFZ for HD-BET and nnU-Net
- Project MONAI for the medical imaging framework
- NVIDIA for free NIM API access
- Anthropic for Claude AI
- Billot, B., et al. (2023). "SynthSeg: Segmentation of brain MRI scans of any contrast and resolution without retraining." Medical Image Analysis.
- Henschel, L., et al. (2020). "FastSurfer - A fast and accurate deep learning based neuroimaging pipeline." NeuroImage.
- Isensee, F., et al. (2018). "nnU-Net: Self-adapting Framework for U-Net-Based Medical Image Segmentation." Nature Methods.
For questions, issues, or suggestions:
- Open an issue on GitHub
- Email: your.email@example.com
- Twitter: @yourusername
If you find this project useful, please consider giving it a star! ⭐
Disclaimer: This tool is for research and educational purposes only. It is not intended for clinical diagnosis or treatment decisions. Always consult qualified healthcare professionals for medical advice.