Converting DICOM sequence and its corresponding segmentation into world coordinates

Operating system: Windows 10
Slicer version: 4.11.20210226 r29738 / 7a593c8

Dear Community,

I need some help with Python coding in the 3DSlicer environment.

The task is relatively simple, but I got lost in numerous methods Slicer provides.

By having a path to a folder with DICOM sequence and a path to a segmentation file. I need to read both, then bring them into a world coordinates and save both volumes as numpy arrays.

I figured out how to read a dicom folder:

from DICOMLib import DICOMUtils
dicomDataDir = ‘path\to\DICOM\’ # input folder with DICOM files
with DICOMUtils.TemporaryDICOMDatabase() as db:
DICOMUtils.importDicom(dicomDataDir, db)
patientUIDs = db.patients()
for patientUID in patientUIDs:
t = DICOMUtils.loadPatientByUID(patientUID)
s = slicer.util.array(t[0])

I figured out how to read a segmentation file:

loadedNodeIDs = ‘path\to\segmentation’ # this list will contain the list of all loaded node IDs
sv = slicer.util.loadVolume(loadedNodeIDs)
svv = slicer.util.array(sv.GetID())

Now, can someone please show me how to convert both volumes into world coordinates?


Both volumes are already in RAS coordinate system. Does something suggest that they are not? Do they appear misaligned?

To get them as numpy array, you can use arrayFromVolume and arrayFromSegmentBinaryLabelmap functions. Use a recent Slicer Preview Release, because arrayFromSegmentBinaryLabelmap in Slicer-4.11 returned a numpy array that was cropped to th minimum necessary size, so you had to do extra work to get the voxels in the same geometry as the reference volume.

This is correct, 3D Slicer reads volumes in the RAS coordinate system. However, here is the trick - the origin of coordinates could be different. Simple overlapping of IJK coordinates for both files will lead to misalignment of volumetric data. Data should be converted to XYZ coordinate system and then the data will be aligned.

In particular, what problem I have. I want to write a code in Slicer so that given two paths - DICOM folder and segmentation file (NIFTI, NRRD, MHD, etc) it opens both volumes and aligns them in world (XYZ) coordinates.

I know how to read volumes, but do not know how to bring them to world coordinates and then iterate data.

What you describe is correct and it is a good practice to check image geometry (origin, spacing, axis directions, and extent) when working with images and resample them as needed.

If you want to get the voxel array in a specific geometry (not the current one) in Slicer then you can specify a reference volume node in arrayFromSegmentBinaryLabelmap. You can also specify reference geometry on most other export and conversion methods.