import h5py
import numpy as np
import nibabel as nib
from pathlib import Path
def convert_nii_to_h5(nii_folder, h5_folder):
nii_folder = Path(nii_folder)
h5_folder = Path(h5_folder)
h5_folder.mkdir(exist_ok=True)
nii_files = list(nii_folder.glob("*.nii.gz"))
for nii_file in nii_files:
file_name = nii_file.stem
# Read NIfTI file
nii_img = nib.load(nii_file)
nii_data = nii_img.get_fdata()
# Create HDF5 file
h5_file_path = h5_folder / f'{file_name}.h5'
with h5py.File(h5_file_path, 'w') as hf:
hf.create_dataset('image', data=nii_data)
if __name__ == '__main__':
nii_folder_path = 'path/to/nii_folder'
h5_folder_path = 'path/to/h5_folder'
convert_nii_to_h5(nii_folder_path, h5_folder_path)
深度学习医学图像处理格式转换(nii.gz格式转换成h5格式!!)
最新推荐文章于 2024-01-21 12:32:20 发布