Spaces:
No application file
No application file
Upload folder using huggingface_hub
Browse files- requirements.txt +1 -0
- utils.py +88 -0
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
utils.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Utility functions for neuroimaging preprocessing and analysis
|
| 3 |
+
"""
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
def normalize_medical_image(image_array: np.ndarray) -> np.ndarray:
|
| 8 |
+
"""
|
| 9 |
+
Normalize medical image intensities to 0-255 range
|
| 10 |
+
Handles various bit depths common in medical imaging
|
| 11 |
+
"""
|
| 12 |
+
img = image_array.astype(np.float32)
|
| 13 |
+
|
| 14 |
+
# Handle different intensity ranges
|
| 15 |
+
if img.max() > 255:
|
| 16 |
+
# Likely 12-bit or 16-bit image
|
| 17 |
+
p1, p99 = np.percentile(img, [1, 99])
|
| 18 |
+
img = np.clip(img, p1, p99)
|
| 19 |
+
|
| 20 |
+
# Normalize to 0-255
|
| 21 |
+
img_min, img_max = img.min(), img.max()
|
| 22 |
+
if img_max > img_min:
|
| 23 |
+
img = (img - img_min) / (img_max - img_min) * 255
|
| 24 |
+
|
| 25 |
+
return img.astype(np.uint8)
|
| 26 |
+
|
| 27 |
+
def apply_window_level(image_array: np.ndarray, window: float, level: float) -> np.ndarray:
|
| 28 |
+
"""
|
| 29 |
+
Apply window/level (contrast/brightness) adjustment
|
| 30 |
+
Common in CT viewing
|
| 31 |
+
|
| 32 |
+
Args:
|
| 33 |
+
image_array: Input image
|
| 34 |
+
window: Window width (contrast)
|
| 35 |
+
level: Window center (brightness)
|
| 36 |
+
"""
|
| 37 |
+
img = image_array.astype(np.float32)
|
| 38 |
+
|
| 39 |
+
min_val = level - window / 2
|
| 40 |
+
max_val = level + window / 2
|
| 41 |
+
|
| 42 |
+
img = np.clip(img, min_val, max_val)
|
| 43 |
+
img = (img - min_val) / (max_val - min_val) * 255
|
| 44 |
+
|
| 45 |
+
return img.astype(np.uint8)
|
| 46 |
+
|
| 47 |
+
def enhance_brain_contrast(image: Image.Image) -> Image.Image:
|
| 48 |
+
"""
|
| 49 |
+
Enhance contrast specifically for brain MRI visualization
|
| 50 |
+
"""
|
| 51 |
+
img_array = np.array(image)
|
| 52 |
+
|
| 53 |
+
# Convert to grayscale if needed
|
| 54 |
+
if len(img_array.shape) == 3:
|
| 55 |
+
gray = np.mean(img_array, axis=2)
|
| 56 |
+
else:
|
| 57 |
+
gray = img_array
|
| 58 |
+
|
| 59 |
+
# Apply histogram equalization
|
| 60 |
+
from PIL import ImageOps
|
| 61 |
+
enhanced = ImageOps.equalize(Image.fromarray(gray.astype(np.uint8)))
|
| 62 |
+
|
| 63 |
+
# Convert back to RGB
|
| 64 |
+
enhanced_array = np.array(enhanced)
|
| 65 |
+
rgb_array = np.stack([enhanced_array] * 3, axis=-1)
|
| 66 |
+
|
| 67 |
+
return Image.fromarray(rgb_array)
|
| 68 |
+
|
| 69 |
+
# Common neuroimaging structure mappings
|
| 70 |
+
STRUCTURE_ALIASES = {
|
| 71 |
+
"hippocampus": ["hippocampal formation", "hippocampal", "medial temporal"],
|
| 72 |
+
"ventricle": ["ventricular system", "lateral ventricle", "CSF space"],
|
| 73 |
+
"white matter": ["WM", "cerebral white matter", "deep white matter"],
|
| 74 |
+
"gray matter": ["GM", "cortical gray matter", "cortex"],
|
| 75 |
+
"tumor": ["mass", "lesion", "neoplasm", "growth"],
|
| 76 |
+
"thalamus": ["thalamic", "diencephalon"],
|
| 77 |
+
"basal ganglia": ["striatum", "caudate", "putamen", "globus pallidus"],
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
def get_structure_aliases(structure: str) -> list:
|
| 81 |
+
"""Get alternative names for a neuroanatomical structure"""
|
| 82 |
+
structure_lower = structure.lower()
|
| 83 |
+
|
| 84 |
+
for key, aliases in STRUCTURE_ALIASES.items():
|
| 85 |
+
if structure_lower == key or structure_lower in aliases:
|
| 86 |
+
return [key] + aliases
|
| 87 |
+
|
| 88 |
+
return [structure]
|