title: How to convert 2D DICOM slices to 3D image in Python
body: <p>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)</p>
  
  <p>I have tried the method from this website : </p>
  
  <p><a href="https://www.raddq.com/dicom-processing-segmentation-visualization-in-python/" rel="nofollow noreferrer">https://www.raddq.com/dicom-processing-segmentation-visualization-in-python/</a></p>
  
  <p>but unfortunately it didn't worked out for me </p>
  
  <pre><code>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);
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/U4Mqk.png" rel="nofollow noreferrer">DICOM slices visualized</a></p>