我想做一个项目。不幸的是,我可以读取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
错误是:
^{pr2}$有什么建议都好。提前谢谢你。在
解决方案:
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
通过使用ImagePositionPatient属性,按照轴的顺序对dicom文件进行排序。解决了我的问题。我可以访问pixel_数组属性和绘图。代码被添加到原始问题中。我也加在下面。在
代码源:https://www.programcreek.com/python/example/97517/dicom.read_file
没有任何背景,很难断言。但是,当我看一下你收集潜在DICOM文件的方式时,你可能正在构建一个不存在的路径(当把路径部分连接回去时,你似乎跳过了一个层次结构级别;你可能需要检查构建的路径是否真的指向了一个现有的文件,使用os.path.exists()). 在
您可能需要更改迭代部分:
比如说:
^{pr2}$相关问题 更多 >
编程相关推荐