我正在尝试做一个项目。不幸的是,我可以读取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
答案 0 :(得分:0)
没有任何进一步的上下文,很难断言任何事情。但是,当我查看收集潜在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)
答案 1 :(得分:0)
我通过使用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