1

I have 191 different png images.

How can I convert them into one 3d nifti image?

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
Neamul
  • 31
  • 5
  • 3
    What have you tried? Can you find an applicable question on SO? – joshvito Jul 14 '20 at 14:07
  • @Naemul please explain more, in many cases you don't have to pack the images into nifti ! also you need the header and the affine transformation matrix which may change after prediction. – Bilal Jul 15 '20 at 18:04
  • @Belal I have the information (Metafile) as well as the affine matrix from the original nifti image from where I get 191 png images , after some analysis I want to convert them into nifti again with all the same information. – Neamul Jul 16 '20 at 10:49
  • @Naemul please see my answer here [Answer](https://stackoverflow.com/a/60495310/) – Bilal Jul 16 '20 at 17:47

1 Answers1

4

Here's a SimpleITK python script that can read in a stack of PNG images and output a 3d Nifti image:

import SimpleITK as sitk
import glob

file_names = glob.glob('*.png')
reader = sitk.ImageSeriesReader()
reader.SetFileNames(file_names)
vol = reader.Execute()
sitk.WriteImage(vol, 'volume.nii.gz')

The script assumes that the glob gets the file names in the proper order. Also the 3-d volume created will have uniform spacing in X, Y, and Z. If the Z spacing is not the same as X and Y, you can call vol.SetSpacing with whatever the spacing values should be.

Dave Chen
  • 1,905
  • 1
  • 12
  • 18