pythonloopsimage-preprocessingniftimedical-imaging

Conversion of .mhd format to .nii format for multiple files code runs without error, but no conversion done


I am using the sliver07 dataset and my task is to convert.mhd format images to .nii format. Following the answer of Medical Imaging Data - how to convert .raw/mhd to Nifti/nii, I was able to convert it for 1 image but now as I apply it for multiple images, it is not converting.

File module structure is this way:

My code:

import glob
my_path='/content/drive/MyDrive/Datasets/sliver07/sliver07-training-scans/scan/'
files=glob.glob(my_path+'*.mhd')

# Scan from train
import SimpleITK as sitk
out = '/content/drive/MyDrive/Datasets/sliver07/sliver07-training-scans/trial/'

for i in range(len(files)):
        img = sitk.ReadImage(files[i]) 
        sitk.WriteImage(img, out + files[i] + '.nii')

But the code is executing without error but conversion is not done yet. What must be wrong?


Solution

  • Got it! The path was wrong

    # Scan from train
      import SimpleITK as sitk
       out = '/content/drive/MyDrive/Datasets/sliver07/sliver07-training-scans/trial/'
    
      for i in range(len(files)):
           img = sitk.ReadImage(files[i]) 
           sitk.WriteImage(img,  files[i].replace('.mhd','.nii'))