mhe / pynrrd

Simple pure-python module for reading and writing nrrd files.
https://pynrrd.readthedocs.io/
MIT License
115 stars 51 forks source link

Example to use this library for Slicer3D? #111

Open Optimox opened 4 years ago

Optimox commented 4 years ago

Hi,

I'm complete novice in 3D visulization. I have some 3D stacks in Dicom format that I read and use with python to create segmentations based on Machine Learning algorithm.

I'd like to export those segmentations (there are many of them) into a compatible format for 3D slicer. I thought nrrd format would be good but I can't find any tutorial on what 3D slicer is expecting.

Would you, by chance, have any idea on where I can find examples?

Thank you!

lassoan commented 1 year ago

3D Slicer can load any 3D nrrd file. There is no special requirement.

Example:

import numpy as np
import nrrd

filename = 'c:/tmp/testdata.nrrd'
data = np.random.randint(100, size=[20,40,30])
header = {
    'space': 'left-posterior-superior',
    'space directions': [[0., 1., 0.], [ 0., 0., -1.], [-1.3, 0., 0.]],
    'space origin': [  86.64489746, -133.92860413,  116.78569794]
}

nrrd.write(filename, data, header)

You can load this into Slicer:

image