Skip to content

sergiosgatidis/CheXsynth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CheXsynth

Synthetic Chest X-ray Generation from 3D CT Volumes

CheXsynth is a Python package for generating synthetic chest radiographs from 3D CT volumes with projected segmentation labels. The package integrates TotalSegmentator for comprehensive anatomical segmentation and DiffDRR for fast GPU-accelerated projection to create realistic synthetic X-rays.

It is designed for a practical research workflow: start from volumetric chest CT, derive anatomically meaningful masks, crop to the relevant body region, and generate aligned PA or lateral DRR-style projections for the CT and selected structures.

Features

  • NIfTI Processing Pipeline: Streamlined workflow for medical imaging data
  • TotalSegmentator Integration: 889 anatomical structures with automatic task selection
  • Fast GPU Projection: DiffDRR-powered projection with PA/LR views
  • Comprehensive Segmentation: Multiple tasks including organs, bones, vessels, and tissues
  • Volume Cropping: Automatic body-region cropping with air value replacement
  • Target Structure Generation: Clinically relevant structure combinations and post-processing

Workflow

CheXsynth is organized as a two-stage pipeline:

  1. Segment and prepare the CT volume.
  2. Project the cropped CT and masks into synthetic radiographic views.
flowchart TD
    A[Input CT volume<br/>NIfTI] --> B[Segmentation pipeline<br/>TotalSegmentator tasks]
    B --> C[Target structure generation]
    C --> D[Body-region cropping]
    D --> E[Cropped CT<br/>and masks]

    E --> F[Projection pipeline<br/>DiffDRR]
    F --> G[PA projection]
    F --> H[LR projection]
    G --> I[CT DRR<br/>and mask projections]
    H --> I

    classDef input fill:#eef6ff,stroke:#3b82f6,stroke-width:1.5px,color:#0f172a;
    classDef process fill:#f8fafc,stroke:#64748b,stroke-width:1.2px,color:#0f172a;
    classDef output fill:#ecfdf5,stroke:#10b981,stroke-width:1.5px,color:#0f172a;

    class A input;
    class B,C,D,E,F process;
    class G,H,I output;
Loading

Typical outputs are:

  • cropped CT volumes ready for projection
  • anatomically aligned mask volumes
  • PA and LR DRR-like projections for the CT and selected masks
  • logs and run summaries for dataset-scale processing

Installation

Conda Environment Setup (Recommended)

# Create conda environment
conda create -n chexsynth python=3.10
conda activate chexsynth

# Clone and install
git clone https://github.com/sergiosgatidis/CheXsynth.git
cd CheXsynth
pip install -r requirements.txt
pip install -e .

Key Dependencies

  • Python 3.8+ (tested with 3.10)
  • TotalSegmentator 2.12.0+: Anatomical structure segmentation
  • DiffDRR 0.6.0+: Fast GPU projection
  • PyTorch: Deep learning backend
  • Medical imaging: nibabel, SimpleITK
  • Scientific computing: NumPy, SciPy, PIL

See requirements.txt for the complete dependency list.

Quick Start

from chexsynth.segmentation import SegmentationPipeline
from chexsynth.projection import ProjectionPipeline

# Process CT volume with segmentation
seg_pipeline = SegmentationPipeline()
result = seg_pipeline.process_nifti_case(
    nifti_file="/path/to/volume.nii.gz",
    output_dir="/path/to/output",
    case_id="case_001"
)

# Generate projections
proj_pipeline = ProjectionPipeline()
proj_result = proj_pipeline.project_case(
    ct_file="/path/to/output/cropped/cropped_ct.nii.gz",
    masks_dir="/path/to/output/cropped/",
    output_dir="/path/to/projections",
    view="PA"
)

Project Structure

CheXsynth/
├── chexsynth/              # Main package
│   ├── segmentation/      # TotalSegmentator pipeline
│   │   ├── pipeline.py          # Complete segmentation workflow
│   │   ├── totalsegmentator.py  # TotalSegmentator wrapper
│   │   ├── mask_operations.py   # Mask combining operations
│   │   ├── crop_utils.py        # Volume cropping utilities
│   │   └── target_structures.py # Clinical structure generation
│   └── projection/        # Fast projection pipeline
│       ├── pipeline.py          # Complete projection workflow
│       └── fast_projector.py    # DiffDRR wrapper
├── scripts/              # Processing scripts
│   ├── segment_ctrate.py      # CT-RATE segmentation script
│   └── project_ctrate.py      # CT-RATE projection script
├── example_notebook/     # End-to-end projection walkthrough notebook
└── config/               # Configuration files

Usage Examples

Processing CT-RATE Dataset

# Run complete segmentation pipeline
python scripts/segment_ctrate.py \
    --input-dir /path/to/ctrate/dataset \
    --output-dir /path/to/processed \
    --limit 10

# Generate PA projections
python scripts/project_ctrate.py \
    --input-dir /path/to/processed \
    --output-dir /path/to/projections/PA \
    --view PA

# Generate LR projections  
python scripts/project_ctrate.py \
    --input-dir /path/to/processed \
    --output-dir /path/to/projections/LR \
    --view LR

In practice, the dataset-scale flow is:

  1. Run segment_ctrate.py to produce segmentations and cropped volumes.
  2. Run project_ctrate.py to generate PA or LR projections from those cropped outputs.
  3. Use the walkthrough notebook in example_notebook/ for interactive inspection and qualitative checks.

Python API Usage

from chexsynth.segmentation import SegmentationPipeline, TotalSegmentatorWrapper
from chexsynth.projection import ProjectionPipeline, FastProjector

# Custom segmentation
segmentator = TotalSegmentatorWrapper(device='cpu', fast=True)
result = segmentator.segment_file(
    input_file="volume.nii.gz",
    output_dir="segmentation/",
    task="total"
)

# Fast projection
projector = FastProjector(device='auto')
proj_result = projector.project_standard_view(
    input_file="cropped_ct.nii.gz",
    output_file="projection_PA.png", 
    view="PA"
)

Configuration

CheXsynth uses YAML configuration files to manage processing parameters. Pass --config config/ctrate_projection.yaml to scripts/project_ctrate.py to control projector settings, view angles, output format, preprocessing, and logging. View angles in that file are expressed in radians.

The default validated views are:

  • PA: alpha=0.0, beta=0.0, gamma=0.0
  • LR: alpha=1.5708, beta=0.0, gamma=0.0

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • TotalSegmentator for comprehensive anatomical segmentation
  • DiffDRR for fast GPU-accelerated projection
  • CT-RATE dataset for chest CT data
  • Medical imaging community for open-source tools and datasets

Citation

If you use CheXsynth in your research, please cite the repository and the CheXanatomy paper:

@software{chexsynth,
  title={CheXsynth: Synthetic Chest X-ray Generation from 3D CT Volumes},
    author={Gatidis, Sergios},
  year={2026},
    url={https://github.com/sergiosgatidis/CheXsynth}
}

@article{gatidis2026chexanatomy,
    title={CheXanatomy: Anatomy-Aware Vision-Language Modeling for Chest Radiographs},
    author={Gatidis, Sergios and Langlotz, Curtis and Bluethgen, Christian},
    journal={arXiv preprint arXiv:2606.08420},
    year={2026},
    doi={10.48550/arXiv.2606.08420},
    url={https://arxiv.org/abs/2606.08420}
}

About

Synthetic Chest Radiograph Generation from 3D CT volumes

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages