【问题标题】:Can not access pixel_array attribute pydicom无法访问pixel_array属性pydicom
【发布时间】:2018-11-20 08:51:51
【问题描述】:

我正在尝试做一个项目。不幸的是,我可以读取 dicom 文件,但是当我尝试访问 pixel_array 属性并绘制它时,它会抛出错误。

def readDcm(self):
        #path of the decomFolder
        print("Path to the DICOM directory is: ",self.dcmPath)
        PathDicom = self.dcmPath
        lstFilesDCM = []  # creating an empty list
        for dirName, subdirList, fileList in os.walk(PathDicom):
            for filename in fileList:
                if ".dcm" in filename.lower():  # checking whether the file's DICOM
                    lstFilesDCM.append(os.path.join(dirName,filename))


        print(len(lstFilesDCM))

        for filename in lstFilesDCM:
            currentDcm = pydicom.read_file(lstFilesDCM[0])
            dcm_data = currentDcm.PixelData
            pixeldata= currentDcm.pixel_array

错误是:

File "C:\Anaconda3\lib\site-packages\pydicom\pixel_data_handlers\pillow_handler.py", line 199, in get_pixeldata
    raise NotImplementedError(e.strerror)

NotImplementedError: None

任何建议都会很好。提前谢谢你。

已解决的解决方案:

def load_scan(path):
    slices = [dicom.read_file(path + '/' + s) for s in os.listdir(path)]
    slices.sort(key = lambda x: float(x.ImagePositionPatient[2]))
    pos1 = slices[int(len(slices)/2)].ImagePositionPatient[2]
    pos2 = slices[(int(len(slices)/2)) + 1].ImagePositionPatient[2]
    diff = pos2 - pos1
    if diff > 0:
        slices = np.flipud(slices)
    try:
        slice_thickness = np.abs(slices[0].ImagePositionPatient[2] - slices[1].ImagePositionPatient[2])
    except:
        slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation)

    for s in slices:
        s.SliceThickness = slice_thickness
    return slices 

【问题讨论】:

    标签: python pydicom


    【解决方案1】:

    我使用 ImagePositionPatient 属性根据轴的递增顺序对 dicom 文件进行了排序。这解决了我的问题。我能够访问 pixel_array 属性和绘图。该代码已添加到原始问题中。我也在下面添加它。

    def load_scan(path):
        slices = [dicom.read_file(path + '/' + s) for s in os.listdir(path)]
        slices.sort(key = lambda x: float(x.ImagePositionPatient[2]))
        pos1 = slices[int(len(slices)/2)].ImagePositionPatient[2]
        pos2 = slices[(int(len(slices)/2)) + 1].ImagePositionPatient[2]
        diff = pos2 - pos1
        if diff > 0:
            slices = np.flipud(slices)
        try:
            slice_thickness = np.abs(slices[0].ImagePositionPatient[2] - slices[1].ImagePositionPatient[2])
        except:
            slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation)
    
        for s in slices:
            s.SliceThickness = slice_thickness
        return slices 
    

    代码来源:https://www.programcreek.com/python/example/97517/dicom.read_file

    【讨论】:

      【解决方案2】:

      没有任何进一步的上下文,很难断言任何事情。但是,当我查看您收集潜在 DICOM 文件的方式时,您可能正在构建不存在的路径(看起来您在连接路径部分时跳过了一个层次结构级别;您可能想要检查构建的路径是否实际指向使用 os.path.exists()) 的现有文件。

      您可能想要更改迭代部分:

          for dirName, subdirList, fileList in os.walk(PathDicom):
              for filename in fileList:
                  if ".dcm" in filename.lower():  # checking whether the file's DICOM
                      lstFilesDCM.append(os.path.join(dirName,filename)) # <-you cut one potential hierarchy level
      

      类似:

      dicom_files = []
      for d, dirs, files in os.walk('./data/'):
          for x in files:
              if x.endswith(".png"):
                  dicom_path = os.path.join(d, x)
                  if os.path.exists(dicom_path):
                      dicom_files.append(dicom_path)
      

      【讨论】:

      • 只是告诉你,我可以访问其他属性,但不能访问 pixel_array
      • 你能用 Matplotlib 显示数组的方式重现错误吗? pydicom.github.io/pydicom/stable/viewing_images.html
      • 是的,我使用的是完全相同的语法。使用测试数据文件,没问题。但是用我的数据。我得到了错误。
      • 查看pillow_handler.py的源代码,看起来你的错误是检查jpeg插件时的管道。您是否检查过您的环境中是否安装了 jpeg 插件?也许有用:stackoverflow.com/a/10109941/5202571
      • 感谢您的帮助。 jepg 插件已经安装。我可以在 Radiant 或 3D 切片器软件中看到 dcm 文件。那么为什么pydicom没有找到像素阵列很奇怪..我什至卸载并重新安装了pil。
      猜你喜欢
      • 1970-01-01
      • 2014-09-19
      • 2018-04-24
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 2016-04-10
      相关资源
      最近更新 更多