Newer
Older
ss2502 / readme_02.md
@刘老老 刘老老 on 15 Oct 5 KB complete

Brain CT/MR Extraction Tool

Overview

A Python tool for automated brain region extraction from CT/MR images in NIfTI format. This tool processes medical imaging data to generate binary masks of brain regions, suitable for neuroimaging analysis and preprocessing pipelines.

Features

  • ✅ Support for both CT and MR imaging modalities
  • ✅ Automatic thresholding and brain region extraction
  • ✅ Morphological processing for noise removal and hole filling
  • ✅ 3D connected component analysis
  • ✅ Preservation of original image geometry
  • ✅ Standard NIfTI format output

Requirements

Python Dependencies

SimpleITK
numpy
scipy
scikit-image

Installation

pip install SimpleITK numpy scipy scikit-image

Usage

Method 1: Command Line Arguments

# Process CT images
python brain_extraction.py input_image.nii.gz output_mask.nii.gz --modality CT

# Process MR images
python brain_extraction.py input_image.nii.gz output_mask.nii.gz --modality MR

Method 2: Direct Code Execution

Modify the paths in the script and run directly:

if __name__ == "__main__":
    input_path = "/path/to/your/input.nii"
    output_path = "/path/to/your/output.nii.gz"
    modality = "CT"  # or "MR"
    
    #extract_brain_region(input_path, output_path, modality)

Processing Pipeline

1. Image Reading & Analysis

  • Read NIfTI format input images
  • Analyze image statistics (value range, data type, percentiles)
  • Extract voxel spacing information

2. Threshold Processing

CT Images:

  • Brain tissue HU range (20-80 HU)
  • Adaptive thresholding strategies
  • Multi-threshold validation

MR Images:

  • Otsu adaptive thresholding
  • Slice-level threshold processing

3. Morphological Processing

  • 2D slice-wise opening operations for noise removal
  • Hole filling (particularly for ventricular regions)
  • 8-connected component analysis
  • Preservation of major brain components

4. 3D Volume Processing

  • 6-connected 3D component analysis
  • Anisotropic morphological processing
  • Voxel spacing consideration
  • Largest connected region extraction

5. Output Generation

  • Binary mask generation (brain=1, background=0)
  • Preservation of original image geometry and spatial coordinates
  • Output as uint8 NIfTI files

Output Specification

Output files contain:

  • Brain region: Pixel value = 1
  • Background: Pixel value = 0
  • File format: NIfTI (.nii or .nii.gz)
  • Data type: np.uint8

Examples

Process Brain CT Image

python brain_extraction.py \
    /Users/liulaolao/Desktop/R2/HeadCtSample_2022_volume_2.nii \
    /Users/liulaolao/Desktop/R2/brain_mask.nii.gz \
    --modality CT

Result Verification

After processing, the program outputs:

  • Image statistics
  • Processing progress
  • Final brain region percentage
  • Output file path

Parameters

Command Line Arguments

  • input_file: Input NIfTI file path
  • output_file: Output NIfTI file path
  • --modality: Imaging modality CT|MR, default: CT

Threshold Parameters (CT Images)

  • Brain tissue HU range: 20-80 HU
  • Window Width (WW): 50
  • Window Level (WL): 400
  • Adaptive thresholding: Auto-adjusted based on image statistics

Troubleshooting

Common Issues

  1. Completely Black Output

    • Verify input image is valid CT/MR data
    • Confirm correct modality setting
    • Check image statistics output
  2. Insufficient Memory

    • Reduce number of simultaneously processed slices
    • Use smaller structural elements
  3. Long Processing Time

    • Consider downsampling for large images
    • Adjust morphological processing parameters

Debug Information

The program provides detailed processing information:

  • Image dimensions and data type
  • Value ranges and statistics
  • Thresholding results
  • Processing progress at each stage
  • Final extraction statistics

File Structure

brain_extraction.py     # Main program file
README.md              # Documentation
input_images/          # Input image directory
output_masks/          # Output mask directory

Technical Details

Libraries Used

  • SimpleITK: NIfTI file I/O operations
  • scipy.ndimage: Morphological processing and connected component analysis
  • numpy: Array operations and numerical computations
  • scikit-image: Image processing algorithms

Algorithm Characteristics

  • Automatic adaptation to different image contrasts
  • Consideration of medical image anisotropy
  • Robust noise handling
  • Preservation of brain anatomical structure integrity

Version History

  • v1.0: Initial release with CT/MR brain extraction support
  • v1.1: Optimized brain-specific thresholds, improved morphological processing

Important Notes

  1. Ensure input images are valid NIfTI format
  2. Processing time may be longer for very large images
  3. Recommend backing up original data before processing
  4. Results should be verified using medical image viewers

Support

For issues or suggestions, please check:

  1. Input image format and integrity
  2. Dependency package version compatibility
  3. System memory availability
  4. Output directory write permissions