import os import numpy as np import nibabel as nib def read_nii_and_save_as_npy(folder_path, output_folder): for root, dirs, files in os.walk(folder_path): for file in files: if file.endswith('.nii.gz'): file_path = os.path.join(root, file) nii_img = nib.load(file_path) data = nii_img.get_fdata() output_file_path = os.path.join(output_folder, file.replace('.nii.gz', '.npy')) assert isinstance(data, object) np.save(output_file_path, data) # 替换为您的文件夹路径 folder_path = 'database/training' output_folder = 'database/biaoqian' read_nii_and_save_as_npy(folder_path, output_folder)