1

I know SimpleITK can read 3d images in Nifty (.nii) format, and also that it supports nii.gz compressed version. However, I am not able to load nii.zip files. These files throw error if I try to unzip/extract them directly, and also not recognized as one of the readable formats by SimpleITK.

Dataset for reference can be found in: https://academictorrents.com/details/27772adef6f563a1ecc0ae19a528b956e6c803ce

Sai
  • 127
  • 1
  • 2
  • 9

2 Answers2

2

I downloaded the same dataset a while ago, I did not experiment with it thoroughly, but here is my experience:

  1. I am able to unzip the nii.zip files succesfully

    • I am using Linux Ubuntu 18.04 and preinstalled unzipping utility from GUI
    • The problems you are experiencing could be happening due to using different archive manager or maybe the dataset could have been damaged during download
    • solution - try different archive manager and download the dataset again
  2. I am using Aliza to view the unzipped scans

    • from my experience for quick checking of the medical image formats Aliza viewer is the best program available with possible exports to different formats
  3. Solution for reading zipped files

mrkolarik
  • 36
  • 5
0

I was never able to use nibabel with a zip file, it kept complaining. Instead, I wrote each file out to a temp directory so it would be a real file on the file system. Not graceful, but it works.

# import required modules
import nibabel as nib
import numpy as np
import zipfile
import shutil

working_dir = '/work/nifti_volume/'

my_zip_path = working_dir + 'raw.zip'
temp_dir = working_dir + 'temp'

z = zipfile.ZipFile(my_zip_path, "r")

for filename in z.namelist(  ):
    if not filename[-1] == '/': # skip over folders in zip file
        
        print('File:', filename)        
        z.extract(filename, path=temp_dir)
        
        nii = nib.load(temp_dir + '/' + filename)
        img = nii.get_fdata()

        # Get voxel dimensions
        voxel_dims = (nii.header["pixdim"])[1:4]
        
        # Compute volume
        nonzero_voxel_count = np.count_nonzero(img)
        voxel_volume = np.prod(voxel_dims)
        nonzero_voxel_volume = nonzero_voxel_count * voxel_volume

        # https://neurostars.org/t/calculate-volumes-counting-voxels-is-a-correct-approach/1433/8
        
        print("Number of non-zero voxels = {}".format(nonzero_voxel_count))
        print("Volume of non-zero voxels = {} mm^3".format(nonzero_voxel_volume))

        shutil.rmtree(temp_dir)
Oleg
  • 303
  • 2
  • 14