Python wrappers for PICSL Convert3D Tools
This project provides a Python interface for the Convert3D tool from the Penn Image Computing and Science Laboratory, developers of ITK-SNAP.
Convert3D is a format conversion and multi-functional processing tool for 3D (also 2D and 4D) medical images. Complex pipelines can be created by putting commands on Convert3D command line in the correct order. Please see Convert3D Reference for complete documentation.
This project makes it possible to interface with Convert3D from Python code. You can execute pipelines as you would on the command line, and you access images on the Convert3D stack as SimpleITK image objects.
Quick Start
Install the package:
pip install picsl_c3d
Download an image to play with (optional of course)
curl -L http://www.nitrc.org/frs/download.php/750/MRI-crop.zip -o MRI-crop.zip
unzip MRI-crop.zip
Do some processing in Python
from picsl_c3d import Convert3D
c = Convert3D()
c.execute('MRIcrop-orig.gipl MRIcrop-seg.gipl -lstat')
SimpleITK Interface
You can read/write images from disk using the command line options passed to the execute
command. But if you want to mix Python-based image processing pipelines and Convert3D pipelines, using the disk to store images creates unnecessary overhead. For the users of SimpleITK, additional commands are available to add images to the Convert3D stack and to read images from the stack.
from picsl_c3d import Convert3D
import SimpleITK as sitk
c = Convert3D()
img = sitk.ReadImage('MRIcrop-orig.gipl')
c.push(img)
c.execute('-smooth 1vox -resample 50% -slice z 50%')
img_slice = c.peek(-1)
Capturing and Parsing Text Output
Sometimes you want to capture parts of the output from a Convert3D. This example redirects the output of the execute
command to a StringIO
buffer, and then uses regular expressions to parse out a piece of information that we want:
from picsl_c3d import Convert3D
import io
import re
import numpy as np
c = Convert3D()
s = io.StringIO()
img='MRIcrop-orig.gipl'
c.execute(f'{img} -probe 50%', out=s)
print(f'c3d output: {s.getvalue()}')
line = s.getvalue().split('\n')[0]
matches = re.split('( at | is )', line)
pos = np.fromstring(matches[2], sep=' ')
val = float(matches[4])