python - 无法访问pixel_array属性pydicom
问题描述
我正在尝试做一个项目。不幸的是,我可以读取 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
解决方案
我使用 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
推荐阅读
- java - BorderLayout 为较小的 JLabel 保留空间
- intellij-idea - JetBrains IntelliJ IDEA 删除歧管插件
- go - 将 golang 应用程序部署到 Heroku 时运行迁移
- c++ - How would I use type as a "variable"?
- javascript - AJAX/PHP - 以纯 JavaScript 分段上传大文件
- ios - UIView 和屏幕旋转上的对角渐变背景
- python - 展开嵌套的字符串列表并保存为 .txt
- visual-c++ - msbuild-bin/cl.exe 不知道 std::apply 是什么?
- java - Java Sockets:如何根据事件将多个文件从客户端发送到服务器
- selenium - Selenium 没有打开 Firefox