69. plugins.dicomstack — Convert bitmap images into numpy arrays.

This module contains functions to convert bitmap images into numpy arrays and vice versa. There are functions to read image from file into arrays, and to save image arrays to files. There is even a class that reads a full stack of Dicom images into a 3D numpy array.

Some of this code was based on ideas found on the PyQwt mailing list.

69.1. Classes defined in module plugins.dicomstack

class plugins.dicomstack.DicomStack(files, zsort=True, reverse=False)[source]

A stack of DICOM images.

Note

This class is only available if you have pydicom installed.

The DicomStack class stores a collection of DICOM images and provides conversion to ndarray.

The DicomStack is initialized by a list of file names. All input files are scanned for DICOM image data and non-DICOM files are skipped. From the DICOM files, the pixel and slice spacing are read, as well as the origin. The DICOM files are sorted in order of ascending origin_z value.

While reading the DICOM files, the following attributes are set:

  • files: a list of the valid DICOM files, in order of ascending z-value.

  • pixel_spacing: a dict with an (x,y) pixel spacing tuple as key and a list of indices of the matching images as value.

  • slice_thickness: a list of the slice thicknesses of the images, in the same order as files.

  • xy_origin: a dict with an (x,y) position the pixel (0,0) as key and a list of indices of the matching images as value.

  • z_origin: a list of the z positions of the images, in the same order as files.

  • rejected: list of rejected files.

class Object[source]

_A dummy object to allow attributes

nfiles()[source]

Return the number of accepted files.

files()[source]

Return the list of accepted filenames

rejectedFiles()[source]

Return the list of rejected filenames

pspacing()[source]

Return the pixel spacing and slice thickness.

Return the pixel spacing (x,y) and the slice thickness (z) in the accepted images.

Returns: a float array of shape (nfiles,3).

origin()[source]

Return the origin of all accepted images.

Return the world position of the pixel (0,0) in all accepted images.

Returns: a float array of shape (nfiles,3).

zvalues()[source]

Return the zvalue of all accepted images.

The zvalue is the z value of the origin of the image.

Returns: a float array of shape (nfiles).

zspacing()[source]

Check for constant slice spacing.

spacing()[source]

Return uniform spacing parameters, if uniform.

image(i)[source]

Return the image at index i

pixar(raw=False)[source]

Return the DicomStack as an array

Returns all images in a single array. This is only successful if all accepted images have the same size.

69.2. Functions defined in module plugins.dicomstack

plugins.dicomstack.dicom2numpy(files)[source]

Easy conversion of a set of dicom images to a numpy array.

Parameters:

files (list of path_like) – List of file names containing the subsequent DICOM images in the stack. The file names should be in the correct order.

Returns:

  • pixar (int array (nfiles, height, width)) – Pixel array with the nfiles images.

  • spacing