matplotlibjupyter-notebookpydicommarching-cubes2d-3d-conversion

How to convert 2D DICOM slices to 3D image in Python


I am currently sitting on an task in which I need to plot DICOM slices into one 3D model using NumPy, Matplotlib, (Marchingcubes, Triangulation or Volumemodel)

I have tried the method from this website :

https://www.raddq.com/dicom-processing-segmentation-visualization-in-python/

but unfortunately it didn't worked out for me

import pydicom
import numpy as np 
import os
import matplotlib.pyplot as plt
import ipywidgets as widgets
from ipywidgets import interact, fixed


filesNew = []
datenSatz = []


output_path = './Head/'
print()

def load_scan(path):
    slices = [pydicom.read_file(path + '/' + s) for s in os.listdir(path)]
    slices.sort(key = lambda x: int(x.InstanceNumber))
    try:
        slice_thickness = np.abs(slices[0].ImagePositionPatient[2] - slices[1].ImagePositionPatient[2])
    except:
        slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation)

    for s in slices:
        s.SliceThickness = slice_thickness

    return slices

for s in load_scan('./Head/'):
    h = s.pixel_array
    datenSatz.append(s) #dataSet from the patient
    filesNew.append(h) #pixel_array





def show_image(image_stack, sliceNumber):
    pxl_ar = image_stack[sliceNumber]
    #print(np.array_equal(pxl_ar,filesNew[sliceNumber]))
    plt.imshow(pxl_ar, cmap= plt.cm.gray)
    plt.show()


slider = widgets.IntSlider(min=0,max=len(filesNew)-1,step=1,value = 0, continuous_update=False)
interact(show_image, image_stack = fixed(filesNew), sliceNumber = slider);

DICOM slices visualized


Solution

  • There is an example of loading a set of 2D CT slices and building a 3D array.

    https://github.com/pydicom/pydicom/blob/master/examples/image_processing/reslice.py

    It does not go on to construct the surface, but it should solve the first half of your problem.