Skip to content

Instantly share code, notes, and snippets.

@reesehaywood
Created August 5, 2021 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reesehaywood/6094543fec3dff375e6840ab72c27af6 to your computer and use it in GitHub Desktop.
Save reesehaywood/6094543fec3dff375e6840ab72c27af6 to your computer and use it in GitHub Desktop.
Example of reading multiple images into numpy 3d array.
# NOTE original code uses previous version of pydicom
# Below may not be exact in newer versions i.e. 2.0 and above
# read in the first CT file to define arrays
ds=dicom.read_file(CTfiles[0])
npix3d=np.zeros((ds.Columns,ds.Rows,len(CTfiles)),dtype=np.intc)
#loop over all images and add to numpy array
for i in xrange(len(CTfiles)):
#read in all files to the array
ds=dicom.read_file(CTfiles[i])
npix3d[:,:,i]=ds.pixel_array*int(ds.RescaleSlope)+int(ds.RescaleIntercept)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment