我正在尝试使用 GDCM 从 dicom 文件中仅读取一个切片。数据是多切片dicom。
r = gdcm.ImageRegionReader()
r.SetFileName( 'test.dcm' )
r.ReadInformation()
image = r.GetImage()
dimensions = image.GetDimensions()
pixelformat = image.GetPixelFormat()
pixelsize = pixelformat.GetPixelSize()
bytes_per_slice = dimensions[0] * dimensions[1] * pixelsize
box = gdcm.BoxRegion()
box.SetDomain(0, dimensions[0] - 1, 0, dimensions[1] - 1, 1, 1)
r.SetRegion(box)
这一切都有效,但我怎样才能读取像素?
nptypes = {gdcm.PixelFormat.UINT8 :numpy.int8,
gdcm.PixelFormat.INT8 :numpy.uint8,
gdcm.PixelFormat.UINT16 :numpy.uint16,
gdcm.PixelFormat.INT16 :numpy.int16,
gdcm.PixelFormat.UINT32 :numpy.uint32,
gdcm.PixelFormat.INT32 :numpy.int32,
gdcm.PixelFormat.FLOAT32:numpy.float32,
gdcm.PixelFormat.FLOAT64:numpy.float64 }
arr = np.array(bytes_per_slice, dtype=pixelformat.GetScalarType())
# also tried np.uint8 as dtype
buffer = np.getbuffer(arr)
#
# now this fails
#
r.ReadIntoBuffer(buffer, bytes_per_slice)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-95-a8022420e1c1> in <module>()
----> 1 r.ReadIntoBuffer(buffer, bytes_per_slice)
lib/python2.7/site-packages/gdcmswig.pyc in ReadIntoBuffer(self, inreadbuffer, buflen)
20678 Read into buffer: false upon error
20679 """
> 20680 return _gdcmswig.ImageRegionReader_ReadIntoBuffer(self, inreadbuffer, buflen)
20681
20682 ImageRegionReader_swigregister = _gdcmswig.ImageRegionReader_swigregister
TypeError: in method 'ImageRegionReader_ReadIntoBuffer', argument 2 of type 'char *'
如何传递 numpy 缓冲区?我尝试了一些 ctype 的东西,但都没有奏效。