此代码import os import numpy as np import nibabel as nib import imageio from PIL import Image def read_niifile(niifilepath): # 读取niifile文件 img = nib.load(niifilepath) # 提取niifile文件 img_fdata = img.get_fdata(dtype='float32') return img_fdata def save_fig(niifilepath, savepath, num, name): # 保存为图片 name = name.split('-')[1] filepath_seg = niifilepath + "segmentation\\" + "segmentation-" + name filepath_vol = niifilepath + "volume\\" + "volume-" + name savepath_seg = savepath + "segmentation\\" savepath_vol = savepath + "volume\\" if not os.path.exists(savepath_seg): os.makedirs(savepath_seg) if not os.path.exists(savepath_vol): os.makedirs(savepath_vol) fdata_vol = read_niifile(filepath_vol) fdata_seg = read_niifile(filepath_seg) (x, y, z) = fdata_seg.shape total = x * y for k in range(z): silce_seg = fdata_seg[:, :, k] if silce_seg.max() == 0: continue else: silce_seg = (silce_seg - silce_seg.min()) / (silce_seg.max() - silce_seg.min()) * 255 silce_seg = np.uint8(Image.fromarray(silce_seg).convert('L')) silce_seg = cv2.threshold(silce_seg, 1, 255, cv2.THRESH_BINARY)[1] if (np.sum(silce_seg == 255) / total) > 0.015: silce_vol = fdata_vol[:, :, k] silce_vol = (silce_vol - silce_vol.min()) / (silce_vol.max() - silce_vol.min()) * 255 silce_vol = np.uint8(Image.fromarray(silce_vol).convert('L')) imageio.imwrite(os.path.join(savepath_seg, '{}.png'.format(num)), silce_seg) imageio.imwrite(os.path.join(savepath_vol, '{}.png'.format(num)), silce_vol) num += 1 return num if __name__ == '__main__': path = 'C:\Users\Administrator\Desktop\LiTS2017' savepath = 'C:\Users\Administrator\Desktop\2D-LiTS2017' filenames = os.listdir(path + "segmentation") num = 0 for filename in filenames: num = save_fig(path, savepath, num, filename)出现SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape,请修改它,给出完整代码
时间: 2023-12-16 15:05:58 浏览: 164
vim-autoimport:Vim插件,用于在不使用LSP的情况下添加导入语句(适用于python等)
你的代码中出现了unicode编码错误,可能是由于路径中反斜杠被解释为转义字符引起的。解决方法是将路径中的反斜杠改为双反斜杠或正斜杠。以下是修改后的完整代码:
```python
import os
import numpy as np
import nibabel as nib
import imageio
from PIL import Image
def read_niifile(niifilepath):
# 读取niifile文件
img = nib.load(niifilepath)
# 提取niifile文件
img_fdata = img.get_fdata(dtype='float32')
return img_fdata
def save_fig(niifilepath, savepath, num, name):
# 保存为图片
name = name.split('-')[1]
filepath_seg = niifilepath + "segmentation\\" + "segmentation-" + name
filepath_vol = niifilepath + "volume\\" + "volume-" + name
savepath_seg = savepath + "segmentation\\"
savepath_vol = savepath + "volume\\"
if not os.path.exists(savepath_seg):
os.makedirs(savepath_seg)
if not os.path.exists(savepath_vol):
os.makedirs(savepath_vol)
fdata_vol = read_niifile(filepath_vol)
fdata_seg = read_niifile(filepath_seg)
(x, y, z) = fdata_seg.shape
total = x * y
for k in range(z):
silce_seg = fdata_seg[:, :, k]
if silce_seg.max() == 0:
continue
else:
silce_seg = (silce_seg - silce_seg.min()) / (silce_seg.max() - silce_seg.min()) * 255
silce_seg = np.uint8(Image.fromarray(silce_seg).convert('L'))
silce_seg = cv2.threshold(silce_seg, 1, 255, cv2.THRESH_BINARY)[1]
if (np.sum(silce_seg == 255) / total) > 0.015:
silce_vol = fdata_vol[:, :, k]
silce_vol = (silce_vol - silce_vol.min()) / (silce_vol.max() - silce_vol.min()) * 255
silce_vol = np.uint8(Image.fromarray(silce_vol).convert('L'))
imageio.imwrite(os.path.join(savepath_seg, '{}.png'.format(num)), silce_seg)
imageio.imwrite(os.path.join(savepath_vol, '{}.png'.format(num)), silce_vol)
num += 1
return num
if __name__ == '__main__':
path = r'C:\Users\Administrator\Desktop\LiTS2017'
savepath = r'C:\Users\Administrator\Desktop\2D-LiTS2017'
filenames = os.listdir(path + "segmentation")
num = 0
for filename in filenames:
num = save_fig(path, savepath, num, filename)
```
阅读全文