AttributeError:"FileDataset"对象没有属性"PixelSpacing" [英] AttributeError: 'FileDataset' object has no attribute 'PixelSpacing'

查看:149
本文介绍了AttributeError:"FileDataset"对象没有属性"PixelSpacing"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一系列图像的文件类型从.dcm转换为.mha.以下是我的代码:

I want to convert the filetype of a series of images from .dcm to .mha. Following is my code:

import numpy
import pydicom
import os
PathDicom ='./DicomResource'
lstFilesDCM = []
for dirName, subdirList, fileList in os.walk(PathDicom):
for filename in fileList:
    if '.dcm' in filename.lower():
        lstFilesDCM.append(os.path.join(dirName, filename))

RefDs = pydicom.read_file(lstFilesDCM[0])
ConstPixelDims = (int(RefDs.Rows), int(RefDs.Columns), len(lstFilesDCM))
#RefDs.PixelSpacing = 0
ConstPixelSpacing = (float(RefDs.PixelSpacing[0]), 
float(RefDs.PixelSpacing[1]), float(RefDs.SliceThickness))
info = ConstPixelDims + ConstPixelSpacing
f = open('info.txt', 'w')
for n in info:
    f.write(str(n)+' ')
f.close()
location = []
for i in range(len(lstFilesDCM)):
    ds = pydicom.read_file(lstFilesDCM[i])
    location.append(ds.SliceLocation)
location.sort()
ArrayDicom = numpy.zeros((len(lstFilesDCM), RefDs.Rows, RefDs.Columns), 
dtype=RefDs.pixel_array.dtype)
for filenameDCM in lstFilesDCM:
    ds = pydicom.read_file(filenameDCM)
    ArrayDicom[location.index(ds.SliceLocation), :, :] = ds.pixel_array
ds = ArrayDicom.tostring()
f = open('1.mha', 'wb')
f.write(ds)
f.close()

有了这个,我得到以下错误:

With this, I am getting following error:

AttributeError:"FileDataset"对象没有属性"PixelSpacing"

AttributeError: 'FileDataset' object has no attribute 'PixelSpacing'

我还尝试添加 RefDs.PixelSpacing = 0 .它引发下一个错误.

I also tried adding RefDs.PixelSpacing = 0. It throws next error.

有没有人可以帮助我解决问题?

Is there anyone can help me to solve the problem?

推荐答案

并非所有SOP类都必须具有属性"PixelSpacing"(0028,0030).对于某些SOP类,如"CT图像存储"(模态CT),此类型为"1".与许多其他类型一样,它是类型"1C".在SOP类中,例如计算机射线照相图像存储"(模态CR),等效属性成像器像素间距"(0018,1164)包含在数据集中.以下 2017a第3部分-信息对象定义的引文对此进行了一些解释.

Attribute "PixelSpacing" (0028,0030) is not mandatory in all SOP Classes. With some SOP Classes like "CT Image Storage" (Modality CT), this is Type "1". With many others, it is Type "1C". In SOP Classes like "Computed Radiography Image Storage" (Modality CR), equivalent attribute "Imager Pixel Spacing" (0018,1164) is included in dataset. Following quote from 2017a Part 3 - Information Object Definitions explains this a bit.

10.7.1.1像素间距
像素间距(0028,0030)指定患者在每个像素中心之间的物理距离.
如果存在像素间距(0028,0030),并且尚未校准图像以校正几何放大效果,则此属性的值应与成像仪像素间距"(0018,1164)或名义扫描像素间距"相同(0018,2010),如果存在这些属性中的任何一个.
如果存在像素间距(0028,0030),并且其值与Imager像素间距(0018,1164)或标称扫描像素间距(0018,2010)中的值不同,则说明该图像已针对已知或假定的几何放大倍率进行了校正,或者已针对患者体内已知深度的已知大小的某些物体进行了校准.
如果缺少像素间距校准类型(0028,0A02)和成像仪像素间距(0018,1164)和标称扫描像素间距(0018,2010),则无法确定是否已执行校正或校准.
注意
1. DX系列IOD中必须使用成像器像素间距(0018,1164)".
2.标称扫描像素间距(0018,2010)是多帧SC系列IOD中的必填属性

10.7.1.1 Pixel Spacing
Pixel Spacing (0028,0030) specifies the physical distance in the patient between the center of each pixel.
If Pixel Spacing (0028,0030) is present and the image has not been calibrated to correct for the effect of geometric magnification, the values of this attribute shall be the same as in Imager Pixel Spacing (0018,1164) or Nominal Scanned Pixel Spacing (0018,2010), if either of those attributes are present.
If Pixel Spacing (0028,0030) is present and the values are different from those in Imager Pixel Spacing (0018,1164) or Nominal Scanned Pixel Spacing (0018,2010), then the image has been corrected for known or assumed geometric magnification or calibrated with respect to some object of known size at known depth within the patient.
If Pixel Spacing Calibration Type (0028,0A02) and Imager Pixel Spacing (0018,1164) and Nominal Scanned Pixel Spacing (0018,2010) are absent, then it cannot be determined whether or not correction or calibration have been performed.
Note
1. Imager Pixel Spacing (0018,1164) is a required attribute in DX family IODs.
2. Nominal Scanned Pixel Spacing (0018,2010) is a required attribute in Multi-frame SC family IODs

看着您的问题,您的代码似乎未绑定到任何特定的Modality/SOP类.考虑到这一点,您的循环很有可能会遇到一些缺少此属性的实例.

Looking at your question, it seems that your code is not bound to any specific Modality/SOP Class. Considering this, it is quite possible your loop encounter some instances those are missing this attribute.

关于第一个错误:

AttributeError:"FileDataset"对象没有属性"PixelSpacing"

AttributeError: 'FileDataset' object has no attribute 'PixelSpacing'

错误非常明显.DICOM数据集不包含您要查找的属性.

Error is very clear. The DICOM dataset does not contain the attribute you are looking for.

关于第二个错误:

TypeError:"DSfloat"对象不支持索引

TypeError: 'DSfloat' object does not support indexing

属性的值多重性是2.

Pixel Spacing = Row Spacing\Column Spacing = 0.30 mm\0.25 mm

因此,您应该能够使用索引访问该值.第一个值应用于行,第二个值应用于列.但这取决于您的工具箱/技术的实现.我在这里都不知道,所以我不能说.

So, you should be able to access the value using index. First value should be for row and second should be column. But this depends on the implementation of your toolkit/technology. I am not aware about both here so I cannot say.

可能是,您的工具箱返回了单个值(没有数组;因此没有索引),您应该在分隔符('\')上进一步拆分然后使用它.

May be, your toolkit returns single value (no array; hence no index) which you should further split on separator ('\') and then use it.

或者可能是因为该属性不存在,所以变量的值是 null (或您技术中的类似值),这就是为什么索引无法正常工作的原因.

Or may be that as the attribute does not present, the value of the variable is null (or whatever similar in your technology) and that is why indexing is not working.

这篇关于AttributeError:"FileDataset"对象没有属性"PixelSpacing"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆