# Converting DICOM sequence and its corresponding segmentation into world coordinates

**URL:** <https://discourse.slicer.org/t/converting-dicom-sequence-and-its-corresponding-segmentation-into-world-coordinates/21644>\
**Category:** Development\
**Tags:** segmentation, dicom, python\
**Created:** [January 26, 2022, 10:36pm UTC](https://discourse.slicer.org/t/converting-dicom-sequence-and-its-corresponding-segmentation-into-world-coordinates/21644 "2022-01-26T22:36:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dmitry\_Cherezov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dmitry_cherezov/32/13950_2.png) [@Dmitry\_Cherezov](https://discourse.slicer.org/u/Dmitry_Cherezov)\
**Post date:** [January 26, 2022, 10:36pm UTC](https://discourse.slicer.org/t/converting-dicom-sequence-and-its-corresponding-segmentation-into-world-coordinates/21644/1 "2022-01-26T22:36:35Z")

</div>

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])  
> loadedNodeIDs.extend(t)

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?

Thanks!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 27, 2022, 3:04am UTC](https://discourse.slicer.org/t/converting-dicom-sequence-and-its-corresponding-segmentation-into-world-coordinates/21644/2 "2022-01-27T03:04:08Z")

</div>

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`](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#slicer.util.arrayFromVolume) and [`arrayFromSegmentBinaryLabelmap`](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#slicer.util.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.

---

<div class="post-metadata">

**Author:** ![Dmitry\_Cherezov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dmitry_cherezov/32/13950_2.png) [@Dmitry\_Cherezov](https://discourse.slicer.org/u/Dmitry_Cherezov)\
**Post date:** [August 2, 2022, 6:07pm UTC](https://discourse.slicer.org/t/converting-dicom-sequence-and-its-corresponding-segmentation-into-world-coordinates/21644/3 "2022-08-02T18:07:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 9, 2022, 10:12am UTC](https://discourse.slicer.org/t/converting-dicom-sequence-and-its-corresponding-segmentation-into-world-coordinates/21644/4 "2022-08-09T10:12:40Z")

</div>

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.
