File size: 5,788 Bytes
4f9a5ba
b4dcf94
 
 
 
206cb4c
ed77b30
b4dcf94
bc86a15
b4dcf94
 
 
 
 
92f937f
b4dcf94
92f937f
b4dcf94
92f937f
b4dcf94
 
7eaddc8
b4dcf94
 
92f937f
 
 
7eaddc8
92f937f
7eaddc8
b4dcf94
92f937f
 
 
 
 
 
 
 
 
b4dcf94
 
7eaddc8
 
b4dcf94
 
 
 
 
7eaddc8
 
 
 
 
92f937f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7eaddc8
 
 
 
 
 
b4dcf94
 
 
 
 
 
7d5a300
92f937f
 
b4dcf94
7d5a300
 
 
 
 
 
 
 
 
 
b4dcf94
 
 
 
7d5a300
b4dcf94
 
 
 
 
 
 
92f937f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26b858a
 
 
 
 
 
 
 
 
 
 
 
b4dcf94
 
 
bc86a15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
---
title: NeuroSAM 3
emoji: πŸ₯
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 6.1.0
app_file: app.py
pinned: true
license: apache-2.0
---

# NeuroSAM 3: Medical Image Segmentation

A comprehensive medical image segmentation application using SAM 3 (Segment Anything Model 3) for DICOM file analysis with advanced features.

## ✨ Features

### Core Features
- 🧠 **SAM 3 Integration**: Uses the latest Segment Anything Model 3 for medical image segmentation
- πŸ“ **DICOM Support**: Process CT and MRI DICOM files
- πŸ–ΌοΈ **Image Formats**: Supports DICOM (.dcm), PNG, and JPG files
- 🎯 **Text Prompts**: Describe what you want to segment (e.g., "brain", "tumor", "skull")
- βš™οΈ **Windowing Strategies**: Optimized windowing presets for CT images

### Interactive Features
- πŸ“Š **Interactive Slice Viewer**: Scroll through multiple slices with auto-play
- πŸ” **Subject Detection**: Automatically groups images by patient/subject ID
- ▢️ **Auto-play**: Automatically animate through slices
- πŸ“ˆ **Ground Truth Comparison**: Compare SAM 3 results with expert annotations (Dice score, IoU)

### Enhanced Features (NEW!)
- ✨ **Enhanced Single Image**: Brightness/contrast controls, colormap options, transparency adjustment
- ✨ **Enhanced Batch Processing**: Process multiple images with ZIP download
- 🎯 **Point/Box Prompts**: Interactive point-click and bounding box-based segmentation
- πŸ“Š **ROI Statistics**: Detailed statistics (area, intensity, centroid, bounding box)
- πŸ“₯ **NIFTI Export**: Export segmentation masks to NIFTI format for medical imaging software
- πŸ’Ύ **Annotation Save/Load**: Save and reload annotations for later use
- 🎭 **Multi-Mask Output**: Generate multiple mask candidates with confidence scores

## Usage

### Single Image Processing
1. Upload a DICOM (.dcm), PNG, or JPG file
2. Enter a text prompt describing what to segment
3. Select the imaging modality (CT or MRI)
4. Choose the windowing strategy (for CT images)
5. Click "Segment Structure" to process

### Interactive Slice Viewer
1. Upload multiple slices/images from the same subject
2. Click "πŸ” Detect Subjects" to auto-group by patient ID
3. Select a subject from the dropdown
4. Click "Process All Slices"
5. Use the slider, navigation buttons, or ▢️ Auto-play to scroll through slices

### Enhanced Single Image (with Image Adjustments)
1. Upload an image
2. Adjust brightness and contrast sliders
3. Choose colormap and transparency for mask overlay
4. Click "Segment with Progress"
5. Download result using the download button

### Point/Box Prompts
1. Upload an image
2. **Point Prompt**: Enter X, Y coordinates and click "Segment at Point"
3. **Box Prompt**: Enter bounding box coordinates (X1, Y1, X2, Y2) and click "Segment in Box"

### ROI Statistics & Export
1. Process an image using "Process & Calculate Statistics"
2. View detailed ROI statistics (area, intensity, centroid)
3. Export to NIFTI format for use in medical imaging software
4. Save annotation for later use
5. Load previously saved annotations

### Multi-Mask Output
1. Upload an image and enter prompt
2. Choose number of masks to generate (1-5)
3. Click "Generate Multiple Masks"
4. Compare mask candidates with confidence scores

### Compare with Ground Truth
1. Upload a medical image and its ground truth mask
2. Enter segmentation prompt
3. Click "Compare Segmentation" to see metrics (Dice score, IoU)

## Requirements

- Python 3.8+
- PyTorch
- Gradio
- PyDICOM
- Transformers (SAM 3 requires latest version from GitHub)
- nibabel (for NIFTI export)
- scipy (for ROI statistics)

## Installation

To install the required dependencies:

```bash
pip install -r requirements.txt
```

**Note:** SAM 3 is only available in the development version of transformers. The requirements.txt file automatically installs it from the GitHub repository.

## Model

This app uses the SAM 3 model from Facebook/Meta. You need:
- A Hugging Face account
- Access token with read permissions for the SAM 3 model (get it from https://huggingface.co/settings/tokens)
- Set `HF_TOKEN` environment variable in Space settings

## Public Datasets

- UniqueData/dicom-brain-dataset (Hugging Face) - MRI Brain scans
- The Cancer Imaging Archive (TCIA) - Various medical imaging
- Imaging Data Commons (IDC) - Large collection of DICOM files
- Kaggle Brain MRI datasets - PNG/JPG format images

## Tabs Overview

| Tab | Description |
|-----|-------------|
| Single Image | Basic single image segmentation |
| Interactive Slice Viewer | Browse through multiple slices with auto-play |
| Gallery View | View all segmentations in a grid |
| Compare with Ground Truth | Evaluate with Dice/IoU metrics |
| ✨ Enhanced Single Image | Advanced controls with download |
| ✨ Enhanced Batch Processing | Batch processing with ZIP export |
| 🎯 Point/Box Prompts | Interactive region selection |
| πŸ“Š ROI Statistics & Export | Statistics + NIFTI/annotation export |
| 🎭 Multi-Mask Output | Multiple mask candidates |
| πŸ”¬ Automatic Mask Generator | AMG - segment entire image without prompts |
| πŸ”§ Advanced Transforms | ResizeLongestSide + CLAHE enhancement |
| 🌊 Edge-Based Segmentation | Sobel edge detection (no AI required) |

## SAM-Medical-Imaging Utilities

Inspired by [amine0110/SAM-Medical-Imaging](https://github.com/amine0110/SAM-Medical-Imaging), we've integrated several utilities:

- **Automatic Mask Generator (AMG)**: Automatically segment all regions in an image using multiple prompts
- **ResizeLongestSide Transform**: Optimal image resizing that maintains aspect ratio
- **CLAHE-like Enhancement**: Contrast enhancement for low-visibility images
- **Edge-Based Segmentation**: Classical computer vision approach using Sobel gradients

## License

Apache 2.0