Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IXI Dataset #78

Open
btthe opened this issue Jul 8, 2024 · 0 comments
Open

IXI Dataset #78

btthe opened this issue Jul 8, 2024 · 0 comments

Comments

@btthe
Copy link

btthe commented Jul 8, 2024

Hello, author! The IXI dataset you processed is in .pkl format. Has it been converted to .pkl after operations such as skull stripping? Can this format be converted back to .nii.gz format?Thank you very much!
Also,I don't quite understand the data[0] here. Doesn't val_dataloader only provide one data? What are data[0][1][2][3] respectively.

'''
Validation
'''
eval_dsc = AverageMeter()
with torch.no_grad():
for data in val_loader:
model.eval()
data = [t.cuda() for t in data]
x = data[0]
y = data[1]
x_seg = data[2]
y_seg = data[3]
# x = x.squeeze(0).permute(1, 0, 2, 3)
# y = y.squeeze(0).permute(1, 0, 2, 3)

'''
nii转pkl
'''

读取.nii文件

nii_file = 'data/LPBA40/test/S01.delineation.skullstripped.nii.gz'
img = nib.load(nii_file)

获取图像数据

img_data = img.get_fdata()
img_affine = img.affine # 获取affine矩阵

序列化到.pkl文件

with open('data/LPBA40/output.pkl', 'wb') as f:
pickle.dump(img_data, f)

I tried to convert the nii file to pkl, but pkl to nii failed.
Uploading 微信图片_20240708212116.png…

'''
pkl转nii
'''
with open('data/LPBA40/output.pkl', 'rb') as f:
loaded_data = pickle.load(f)
print(loaded_data) # 查看加载的数据结构

从.pkl文件反序列化图像数据和affine矩阵

with open('data/LPBA40/output.pkl', 'rb') as f:
img_data, affine = pickle.load(f) # 同时加载图像数据和affine矩阵

创建一个新的Nifti1Image,使用加载的affine矩阵

new_img = nib.Nifti1Image(img_data, affine=affine)

保存为.nii文件

new_img_file = 'data/LPBA40/restored_image.nii.gz'
new_img.to_filename(new_img_file)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant