This repository provides a computational pipeline for converting CT DICOM series into three-dimensional skull mesh representations (STL format) using the Visualization Toolkit (VTK) and NumPy. The implementation is designed for medical imaging applications, including surgical planning and anatomical modeling.
The 3Drecon.py script implements a streamlined workflow that:
- Reads CT DICOM series data
- Segments bone tissue using Hounsfield unit thresholding
- Generates surface meshes via Discrete Marching Cubes algorithm
- Renders visualizations using VTK's rendering pipeline
- Exports STL files (
craneo.stl) suitable for 3D printing and further analysis
This documentation provides comprehensive instructions for reproducing results on Windows systems using PowerShell. The software was developed and tested with Python 3.11.
- Operating System: Windows (PowerShell environment)
- Python Version: 3.11 (development and testing baseline)
- Package Manager: Conda (recommended for stable VTK installations)
- Core Dependencies:
vtk,numpy - Optional Dependencies:
plotly(for 2D visualization and heatmaps)
- Create a dedicated conda environment with Python 3.11:
conda create -n skull python=3.11 -y
conda activate skull- Install required packages from conda-forge:
conda install -n skull -c conda-forge vtk numpy plotly -yWithin an activated conda environment:
pip install vtk numpy plotlyAlternatively, use the provided requirements file:
pip install -r requirements.txtNote: On Windows platforms, conda-forge distributions of VTK typically provide superior stability and compatibility.
3Drecon.py— Primary reconstruction script implementing the VTK pipeline- Automatic DICOM directory detection (searches
./CTDataset,./CTDataset/CTDataset,./dcmfolder) - Reader output validation with diagnostic reporting
- Dynamic middle-slice selection
- STL export via
vtkSTLWriter
- Automatic DICOM directory detection (searches
CTDataset/— Sample DICOM series directory (if provided)requirements.txt— Python package dependency specification
From the repository root directory in PowerShell:
conda activate skull
python 3Drecon.pyIf conda activation is unavailable in your shell session:
conda run -n skull python 3Drecon.pyUpon successful execution, the script will:
- Locate and validate DICOM series data from candidate directories
- Display pixel spacing and volumetric extent information
- Generate 2D slice visualizations (original and thresholded) using Plotly (if installed)
- Launch an interactive VTK rendering window displaying the reconstructed mesh and bounding box
- Export the mesh to
craneo.stlin the working directory
conda install -n skull -c conda-forge vtkor
pip install vtkThis error from vtkDICOMImageReader indicates:
- The target directory lacks
.dcmfiles - The DICOM series is incomplete or corrupted
Resolution: Verify DICOM file presence and integrity. Relocate files to a supported candidate directory or modify the directory path in 3Drecon.py.
The script automatically selects the middle slice from the dataset. If an IndexError occurs:
- The reader returned zero-dimensional data
- Review printed data extent diagnostics
- Verify DICOM series consistency and completeness
The script applies a default threshold value for bone segmentation. This parameter should be adjusted based on the specific CT scan characteristics and desired tissue isolation.
The Discrete Marching Cubes algorithm may expose additional parameters for multi-threshold extraction and surface generation refinement.
- VTK Documentation: https://vtk.org/documentation/
- vtkDICOMImageReader API: https://vtk.org/doc/nightly/html/classvtkDICOMImageReader.html
- Marching Cubes Algorithm: Lorensen, W.E., & Cline, H.E. (1987). Marching cubes: A high resolution 3D surface construction algorithm. ACM SIGGRAPH Computer Graphics, 21(4), 163-169.
- Hounsfield Scale Reference: https://en.wikipedia.org/wiki/Hounsfield_scale
- VTK-NumPy Integration: Consult VTK documentation for
vtk_to_numpyutilities - Tutorial Reference: https://pyscience.wordpress.com/2014/09/11/surface-extraction-creating-a-mesh-from-pixel-data-using-python-and-vtk/
Potential enhancements for extended functionality:
- Command-line Interface: Implement
--dicom-dirargument for dynamic input path specification - Headless Mode: Add non-interactive export functionality for server environments
- Post-processing Pipeline: Integrate mesh smoothing, decimation, and validation operations prior to STL export
- Multi-threshold Segmentation: Support for extracting multiple tissue types simultaneously
This implementation builds upon established VTK and NumPy methodologies within the medical imaging community. Primary technical guidance was derived from VTK project documentation and community tutorials.
Last Updated: 21 Oct, 2025