Skip to content

This repository was developed as part of a bachelor thesis at the bfh.

License

Notifications You must be signed in to change notification settings

schaertim/MRI-Segmentation-Helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MRI-Segmentation-Helper

This repository contains tools for processing and handling MRI-based segmentation of ribs and sternum. The tools assist in creating training data for neural networks and reconstructing 3D segmentations from 2D predictions.

Repository Structure

├── images/
│   ├── mri/                    # Contains sample MRI images
│   └── segmentation/           # Contains sample segmentation masks
├── segmentation-helper/
│   ├── predictions/            # Contains nnU-Net predictions and metadata
│   ├── x_train/               # Contains extracted 2D slices for training
│   ├── y_train/               # Contains corresponding segmentation masks
│   ├── seg_reconstructor.py    # Script for 3D volume reconstruction
│   ├── slice_creator.py        # Script for creating 2D training slices
│   └── requirements.txt        # Package dependencies
└── README.md

Installation

To install the required packages, navigate to the segmentation-helper directory and run:

cd segmentation-helper
pip install -r requirements.txt

This will install all necessary dependencies:

  • numpy: For numerical computations and array operations
  • SimpleITK: For medical image I/O and processing
  • scipy: For scientific computing and image processing
  • matplotlib: For visualization and plotting
  • Pillow (PIL): For image processing and file I/O

Features

Slice Creator

The slice_creator.py script provides an interactive tool for:

  • Loading 3D MRI volumes and their segmentation masks
  • Rotating volumes along specified rotation axes
  • Interactive visualization and manipulation of slice positions
  • Extracting and saving 2D slices with corresponding segmentation masks
  • Automatic metadata generation for reconstruction

Segmentation Reconstructor

The seg_reconstructor.py script enables:

  • Reconstruction of 3D volumes from 2D slice predictions
  • Preservation of original medical image properties (origin, spacing, direction)
  • Application of morphological operations for cleaning and smoothing

Usage

Creating Training Slices

  1. Place your MRI volume in the images/mri/ directory
  2. Place corresponding segmentation mask in images/segmentation/ directory
  3. Run the slice creator:
python slice_creator.py

The interactive interface allows you to:

  • View the current slice and its position in the axial plane
  • Drag the cyan rectangle to position the crop region
  • Press Enter to save the current slice
  • Press Space to skip the current slice
  • Press Escape to end processing

Slices will be saved in:

  • x_train/ for MRI slices
  • y_train/ for segmentation masks
  • Metadata will be saved in predictions/crop_metadata.json

Reconstructing 3D Volumes

After obtaining predictions for your 2D slices, use the reconstructor:

python seg_reconstructor.py

The script will:

  1. Load the 2D predictions
  2. Reconstruct them into a 3D volume
  3. Apply morphological cleaning
  4. Save the final volume in the specified medical image format

Visualization Examples

Slice Creation Process

The slice creator provides an interactive interface for extracting 2D slices:

Slice Creator Interface

  • Left: Current rotated slice view with adjustable crop region (cyan rectangle)
  • Right: Axial view showing the slice plane positions

Segmentation Results

Example of a reconstructed segmentation:

Segmentation Result

Notes

  • The repository includes sample data for demonstration purposes
  • The tools are designed to work with the nnU-Net framework for segmentation

License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.

About

This repository was developed as part of a bachelor thesis at the bfh.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages