以下两个函数读取并保存目录中的图像:
def resample(image, reference_image, def_value=0.0):
return sitk.Resample(image,
reference_image,
sitk.Transform(),
sitk.sitkLinear,
def_value,
reference_image.GetPixelID())
def ResampleNiiSave(dcmPath, nrdPath):
dcmItk = dicomread(dcmPath, imgitk=True) # contains one file multiple slice
nrdItk = nrrdread(nrdPath, imgitk=True) # contains single nrrd file
data = [dcmItk, None]
if len(nrdItk.GetSize()) == 4:
if nrdItk.GetSize()[3] == 3:
for ig in range(3):
imgnp = sitk.GetArrayFromImage(nrdItk)[ig, ...]
imgItk = sitk.GetImageFromArray(imgnp)
data[1] = imgItk
nrdItkAl = resample(data[1], data[0])
nrd2niiPath = os.path.splitext(nrdPath)[0]+'_{}'.format(ig)+'.nii.gz'
if os.path.isfile(nrd2niiPath) is None:
sitk.WriteImage(nrdItkAl, nrd2niiPath)
del imgnp, imgItk # necessary?
elif nrdItk.GetSize()[3] == 1:
imgnp = sitk.GetArrayFromImage(nrdItk)
imgnp = np.squeeze(imgnp)
imgItk = sitk.GetImageFromArray(imgnp)
data[1] = imgItk
nrdItkAl = resample(data[1], data[0])
nrd2niiPath = os.path.splitext(nrdPath)[0] + '.nii.gz'
if os.path.isfile(nrd2niiPath) is None:
sitk.WriteImage(nrdItkAl, nrd2niiPath)
del imgnp, imgItk
else:
raise ValueError("image has unknown dimension")
else:
data[1] = nrdItk
nrdItkAl = sitk.Resample(data[1], data[0], sitk.Transform(), sitk.sitkLinear,
np.min(sitk.GetArrayFromImage(data[1])).astype('double'), data[1].GetPixelID())
nrd2niiPath = os.path.splitext(nrdPath)[0] + '.nii.gz'
if os.path.isfile(nrd2niiPath) is None:
sitk.WriteImage(nrdItkAl, nrd2niiPath)
dcm2niiPath = dcmPath+'.nii.gz'
if os.path.isfile(dcm2niiPath) is None:
sitk.WriteImage(dcmItk, dcm2niiPath)
ResampleNiiSave
函数不一定返回任何值,而是执行对齐并将变量保存为另一种格式。
如何创建一个类来将两个函数组合为类方法。我尝试了以下几项,但运气不佳:
class prepSeg:
def __init__(self, dcmPath, nrdPath):
self.dcmPath = dcmPath
self.nrdPath = nrdPath
def resample(self, image, reference_image, def_value=0.0):
return sitk.Resample(image,
reference_image,
sitk.Transform(),
sitk.sitkLinear,
def_value,
reference_image.GetPixelID())
def ResampleNiiSave(self):
dcmItk = dicomread(self.dcmPath, imgitk=True) # contains one file multiple slice
nrdItk = nrrdread(self.nrdPath, imgitk=True) # contains single nrrd file
data = [dcmItk, None]
if len(nrdItk.GetSize()) == 4:
if nrdItk.GetSize()[3] == 3:
for ig in range(3):
imgnp = sitk.GetArrayFromImage(nrdItk)[ig, ...]
imgItk = sitk.GetImageFromArray(imgnp)
data[1] = imgItk
nrdItkAl = self.resample(data[1], data[0])
nrd2niiPath = os.path.splitext(self.nrdPath)[0]+'_{}'.format(ig)+'.nii.gz'
if os.path.isfile(nrd2niiPath) is None:
sitk.WriteImage(nrdItkAl, nrd2niiPath)
del imgnp, imgItk # necessary?
elif nrdItk.GetSize()[3] == 1:
imgnp = sitk.GetArrayFromImage(nrdItk)
imgnp = np.squeeze(imgnp)
imgItk = sitk.GetImageFromArray(imgnp)
data[1] = imgItk
nrdItkAl = self.resample(data[1], data[0])
nrd2niiPath = os.path.splitext(self.nrdPath)[0] + '.nii.gz'
if os.path.isfile(nrd2niiPath) is None:
sitk.WriteImage(nrdItkAl, nrd2niiPath)
del imgnp, imgItk
else:
raise ValueError("image has unknown dimension")
else:
data[1] = nrdItk
nrdItkAl = sitk.Resample(data[1], data[0], sitk.Transform(), sitk.sitkLinear,
np.min(sitk.GetArrayFromImage(data[1])).astype('double'), data[1].GetPixelID())
nrd2niiPath = os.path.splitext(self.nrdPath)[0] + '.nii.gz'
if os.path.isfile(nrd2niiPath) is None:
sitk.WriteImage(nrdItkAl, nrd2niiPath)
dcm2niiPath = self.dcmPath+'.nii.gz'
if os.path.isfile(dcm2niiPath) is None:
sitk.WriteImage(dcmItk, dcm2niiPath)
现在,prepSeg
类的return
应该是什么。然而,我使用两个文件路径输入运行prepSeg
,它只是创建类对象,但不执行resample
和save
中的任何任务。例如:
A = prepSeg(dcmFiles[-2],nrdFilesIdx[-2])
# .ResampleNiiSave()
# prepSeg.ResampleNiiSave
print(A)
A.ResampleNiiSave
退货:
prepSeg object at 0x0000025E6D96E710>
<bound method prepSeg.ResampleNiiSave of <prepSeg object at 0x0000025E6D96E710>>
好吧,您的IDE一团糟。
它在欺骗你发生了什么。
试试这个代码:
class prepSeg():
def __init__(self):
self.a = ''
def foo(self):
print(self)
print(self.a)
print(self.b)
A = prepSeg()
A.foo()
#prepSeg.foo(prepSeg)
这会产生输出:
<__main__.prepSeg object at 0x04267B70>
Traceback (most recent call last):
File "D:andrewPythonsoMissingAttribute.py", line 12, in <module>
A.foo()
File "D:andrewPythonsoMissingAttribute.py", line 8, in foo
print(self.b)
AttributeError: 'prepSeg' object has no attribute 'b'
注意object at 0x...
和AttributeError
的措辞。它还谈到了故意缺失的CCD_ 9。
如果我交换驱动程序代码回合:
A = prepSeg()
#A.foo()
prepSeg.foo(prepSeg)
我得到一个不同的输出:
<class '__main__.prepSeg'>
Traceback (most recent call last):
File "D:andrewPythonsoMissingAttribute.py", line 13, in <module>
prepSeg.foo(prepSeg)
File "D:andrewPythonsoMissingAttribute.py", line 7, in foo
print(self.a)
AttributeError: type object 'prepSeg' has no attribute 'a'
现在self
打印:<class '__main__.prepSeg'>
,而AttributeError
大约是不具有属性a
的type object 'prepSeg'
。