首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pydicom函数中的缺陷

pydicom函数中的缺陷
EN

Stack Overflow用户
提问于 2015-01-21 20:34:59
回答 2查看 1.3K关注 0票数 0

我想和pydicom一起处理dicom图像。不幸的是,我得到了错误

代码语言:javascript
复制
File "/usr/local/lib/python2.7/dist-packages/dicom/dataset.py", line 372, in _pixel_data_numpy
    raise TypeError(msg % (numpy_format, self.PixelRepresentation,
UnboundLocalError: local variable 'numpy_format' referenced before assignment

在函数中

代码语言:javascript
复制
def _pixel_data_numpy(self):
"""Return a NumPy array of the pixel data.

NumPy is a numerical package for python. It is used if available.

:raises TypeError: if no pixel data in this dataset.
:raises ImportError: if cannot import numpy.

"""
if 'PixelData' not in self:
    raise TypeError("No pixel data found in this dataset.")

if not have_numpy:
    msg = "The Numpy package is required to use pixel_array, and numpy could not be imported.\n"
    raise ImportError(msg)

# determine the type used for the array
need_byteswap = (self.is_little_endian != sys_is_little_endian)

# Make NumPy format code, e.g. "uint16", "int32" etc
# from two pieces of info:
#    self.PixelRepresentation -- 0 for unsigned, 1 for signed;
#    self.BitsAllocated -- 8, 16, or 32
format_str = '%sint%d' % (('u', '')[self.PixelRepresentation],
                          self.BitsAllocated)
try:
    numpy_format = numpy.dtype(format_str)
print numpy_format
except TypeError:
print "Data type not understood by NumPy!"
print format_str
    msg = ("Data type not understood by NumPy: "
           "format='%s', PixelRepresentation=%d, BitsAllocated=%d")
    raise TypeError(msg % (numpy_format, self.PixelRepresentation,
                    self.BitsAllocated))

# Have correct Numpy format, so create the NumPy array
arr = numpy.fromstring(self.PixelData, numpy_format)

# XXX byte swap - may later handle this in read_file!!?
if need_byteswap:
    arr.byteswap(True)  # True means swap in-place, don't make a new copy
# Note the following reshape operations return a new *view* onto arr, but don't copy the data
if 'NumberOfFrames' in self and self.NumberOfFrames > 1:
    if self.SamplesPerPixel > 1:
        arr = arr.reshape(self.SamplesPerPixel, self.NumberOfFrames, self.Rows, self.Columns)
    else:
        arr = arr.reshape(self.NumberOfFrames, self.Rows, self.Columns)
else:
    if self.SamplesPerPixel > 1:
        if self.BitsAllocated == 8:
            arr = arr.reshape(self.SamplesPerPixel, self.Rows, self.Columns)
        else:
            raise NotImplementedError("This code only handles SamplesPerPixel > 1 if Bits Allocated = 8")
    else:
        arr = arr.reshape(self.Rows, self.Columns)
return arr

当我打印变量format_str时,我得到了uint12。不幸的是,我无法解决这个错误。我能做些什么来解决这个问题吗?

即使我删除了所有的print命令(我为调试添加了它们),我也会得到相同的错误。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-22 13:33:01

这里有两个问题。首先,pydicom中的一个bug :在引发错误时,它错误地使用了未定义的numpy_format。它应该使用format_str。我将添加一个问题来纠正这个问题。

第二个问题是,该文件声称BitsAllocated为12,这是非常不寻常的。DICOM文件通常有12位像素值,但在文件中每像素使用16位。

您可以通过比较RowsColumns与像素数据的大小来检查它实际分配的内容。对于单个帧映像(下面的示例使用pydicom的CT_small.dcm示例文件):

代码语言:javascript
复制
>>> len(ds.PixelData)
32768
>>> ds.Rows * ds.Columns * 2  # 2 bytes for 16-bit allocation
32768

如果图像有多个帧,那么您也必须将帧数乘以。同样适用于SamplesPerPixel

如果像素数据的大小与上面使用的每个像素的2个字节相匹配,那么在请求ds.BitsAllocated之前,您可以通过将pixel_array设置为16来纠正问题。

票数 2
EN

Stack Overflow用户

发布于 2015-01-21 21:11:41

我假设代码的格式如下:

代码语言:javascript
复制
try:
    numpy_format = numpy.dtype(format_str)
    print numpy_format
except TypeError:
    print "Data type not understood by NumPy!"
    print format_str
    msg = ("Data type not understood by NumPy: "
           "format='%s', PixelRepresentation=%d, BitsAllocated=%d")
    raise TypeError(msg % (numpy_format, self.PixelRepresentation,
                    self.BitsAllocated))

try块中,为numpy_format分配了一个值。如果numpy.dtype(format_str)抛出一个TypeError,它直接进入except块,而不将值绑定到numpy_format。这意味着当numpy_format在引发TypeError时再次被引用时,它会抛出一个UnboundLocalError,因为它从一开始就没有绑定过。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28076321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档