1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
############################################################################
#
# Program: GDCM (Grassroots DICOM). A DICOM library
#
# Copyright (c) 2006-2011 Mathieu Malaterre
# All rights reserved.
# See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even
# the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the above copyright notice for more information.
#
############################################################################
"""
This module add support for converting a gdcm.Image to a numpy array.
Caveats:
- Does not support UINT12/INT12
Removed:
- float16 is defined in GDCM API but no implementation exist for it ...
"""
import gdcm
import numpy
def get_gdcm_to_numpy_typemap():
"""Returns the GDCM Pixel Format to numpy array type mapping."""
_gdcm_np = {gdcm.PixelFormat.UINT8 :numpy.int8,
gdcm.PixelFormat.INT8 :numpy.uint8,
#gdcm.PixelFormat.UINT12 :numpy.uint12,
#gdcm.PixelFormat.INT12 :numpy.int12,
gdcm.PixelFormat.UINT16 :numpy.uint16,
gdcm.PixelFormat.INT16 :numpy.int16,
gdcm.PixelFormat.UINT32 :numpy.uint32,
gdcm.PixelFormat.INT32 :numpy.int32,
#gdcm.PixelFormat.FLOAT16:numpy.float16,
gdcm.PixelFormat.FLOAT32:numpy.float32,
gdcm.PixelFormat.FLOAT64:numpy.float64 }
return _gdcm_np
def get_numpy_array_type(gdcm_pixel_format):
"""Returns a numpy array typecode given a GDCM Pixel Format."""
return get_gdcm_to_numpy_typemap()[gdcm_pixel_format]
def gdcm_to_numpy(image):
"""Converts a GDCM image to a numpy array.
"""
pf = image.GetPixelFormat()
assert pf.GetScalarType() in get_gdcm_to_numpy_typemap().keys(), \
"Unsupported array type %s"%pf
shape = image.GetDimension(0) * image.GetDimension(1), pf.GetSamplesPerPixel()
if image.GetNumberOfDimensions() == 3:
shape = shape[0] * image.GetDimension(2), shape[1]
dtype = get_numpy_array_type(pf.GetScalarType())
gdcm_array = image.GetBuffer()
result = numpy.frombuffer(gdcm_array, dtype=dtype)
result.shape = shape
return result
if __name__ == "__main__":
import sys
r = gdcm.ImageReader()
filename = sys.argv[1]
r.SetFileName( filename )
if not r.Read():
sys.exit(1)
numpy_array = gdcm_to_numpy( r.GetImage() )
print numpy_array
|