【问题标题】:Bug in pydicom functionpydicom 函数中的错误
【发布时间】:2023-04-10 14:57:01
【问题描述】:

我想使用 pydicom 来处理 dicom 图像。不幸的是我得到了错误

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

在函数中

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 命令(我添加了它们以进行调试),我也会得到同样的错误。

【问题讨论】:

    标签: python pydicom


    【解决方案1】:

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

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

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

    >>> len(ds.PixelData)
    32768
    >>> ds.Rows * ds.Columns * 2  # 2 bytes for 16-bit allocation
    32768
    

    如果图像有多个帧,那么您还必须乘以帧数。 SamplesPerPixel 也是如此。

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

    【讨论】:

      【解决方案2】:

      我假设代码的格式是这样的:

      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。这意味着当提升TypeError 时再次引用numpy_format 时,它会抛出一个UnboundLocalError,因为它从一开始就没有被绑定。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-13
        • 2017-05-23
        相关资源
        最近更新 更多