本文介绍了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:

我还尝试添加 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.

看着您的问题,您的代码似乎未绑定到任何特定的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.

关于第一个错误:

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

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

关于第二个错误:

属性的值多重性是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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 18:17