0% found this document useful (0 votes)
50 views3 pages

Dicom Image 3D Mainpulation

The document discusses loading and processing DICOM data using Python. It populates a list with DICOM file names, reads metadata from the first file to get dimensions and spacing values, uses these to calculate coordinate axes, loads all pixel data into a NumPy array, and visualizes slices of the array. Key steps include reading metadata, calculating axes based on spacing and dimensions, loading pixel data into an array sized based on dimensions, and visualizing slices of the array.

Uploaded by

Ali Nawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views3 pages

Dicom Image 3D Mainpulation

The document discusses loading and processing DICOM data using Python. It populates a list with DICOM file names, reads metadata from the first file to get dimensions and spacing values, uses these to calculate coordinate axes, loads all pixel data into a NumPy array, and visualizes slices of the array. Key steps include reading metadata, calculating axes based on spacing and dimensions, loading pixel data into an array sized based on dimensions, and visualizing slices of the array.

Uploaded by

Ali Nawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1/1/2021 Jupyter Notebook Viewer

Load & Process DICOM data


In [2]: import dicom
import os
import numpy
from matplotlib import pyplot, cm
%pylab inline

Populating the interactive namespace from numpy and matplotlib

Populate lstFilesDCM with the filenames of the DICOM files

In [3]: PathDicom = "./MyHead/"


lstFilesDCM = [] # create an empty list
for dirName, subdirList, fileList in os.walk(PathDicom):
for filename in fileList:
if ".dcm" in filename.lower(): # check whether the file's
lstFilesDCM.append(os.path.join(dirName,filename))

Use the first of the DICOM files to read in some of the metadata, specifically the image
dimensions, the pixel-spacing, and the slice-thickness

In [4]: # Get ref file


RefDs = dicom.read_file(lstFilesDCM[0])

# Load dimensions based on the number of rows, columns, and slices


ConstPixelDims = (int(RefDs.Rows), int(RefDs.Columns), len(lstFiles

# Load spacing values (in mm)


ConstPixelSpacing = (float(RefDs.PixelSpacing[0]), float(RefDs.Pixe

Use ConstPixelDims and ConstPixelSpacing to calculate coordinate axes for this


array

In [5]: x = numpy.arange(0.0, (ConstPixelDims[0]+1)*ConstPixelSpacing[0], C


y = numpy.arange(0.0, (ConstPixelDims[1]+1)*ConstPixelSpacing[1], C
z = numpy.arange(0.0, (ConstPixelDims[2]+1)*ConstPixelSpacing[2], C

Load all the pixel data into an appropriate sized NumPy array named ArrayDicom

https://nbviewer.jupyter.org/urls/bitbucket.org/somada141/pyscience/raw/master/20140906_PythonDicom/Material/PythonDicomPy… 1/3
1/1/2021 Jupyter Notebook Viewer

In [6]: # The array is sized based on 'ConstPixelDims'


ArrayDicom = numpy.zeros(ConstPixelDims, dtype=RefDs.pixel_array.dt

# loop through all the DICOM files


for filenameDCM in lstFilesDCM:
# read the file
ds = dicom.read_file(filenameDCM)
# store the raw image data
ArrayDicom[:, :, lstFilesDCM.index(filenameDCM)] = ds.pixel_arr

Visualize

In [14]: pyplot.figure(dpi=300)
pyplot.axes().set_aspect('equal', 'datalim')
pyplot.set_cmap(pyplot.gray())
pyplot.pcolormesh(x, y, numpy.flipud(ArrayDicom[:, :, 80]))

Out[14]: <matplotlib.collections.QuadMesh at 0x113bb3dd0>

https://nbviewer.jupyter.org/urls/bitbucket.org/somada141/pyscience/raw/master/20140906_PythonDicom/Material/PythonDicomPy… 2/3
1/1/2021 Jupyter Notebook Viewer

In [15]: pyplot.figure(dpi=300)
pyplot.axes().set_aspect('equal', 'datalim')
pyplot.set_cmap(pyplot.gray())
pyplot.pcolormesh(z, x, numpy.flipud(ArrayDicom[:, 115, :]))

Out[15]: <matplotlib.collections.QuadMesh at 0x1140c7a10>

In [ ]:

https://nbviewer.jupyter.org/urls/bitbucket.org/somada141/pyscience/raw/master/20140906_PythonDicom/Material/PythonDicomPy… 3/3

You might also like