Open sebyo opened 4 years ago
Hi sebyo: I suggest you convert your PNG files to mhd format. You can do it using SimpleITK like this:
load each PNG file using sitk.ImageFileReader() and append each file to a list
image_list = [] for filename in filename_list: fullpath = os.path.join(input_dir, filename) reader = sitk.ImageFileReader() reader.SetImageIO('PNGImageIO') reader.SetFileName(fullpath) image = reader.Execute() image_list.append(image)
convert the list to a 3d numpy array (note that the coordinate standard for numpy is [z,y,x] while for itk is [x,y,z]
slices_size = np.shape(sitk.GetArrayFromImage(itkimages[0])) number_of_slices = len(itkimages) npa = np.zeros((number_of_slices, slices_size[0], slices_size[1])) for slice_index, image in enumerate(itkimages): # only keep one channel (expected that greyscale image has identical channels) if image.GetNumberOfComponentsPerPixel() > 1: npa[slice_index, :, :] = sitk.GetArrayFromImage(image)[:, :, 0] else: npa[slice_index, :, :] = sitk.GetArrayFromImage(image) itk_image_stack = sitk.GetImageFromArray(npa)
save it as mhd using sitk.WriteImage()
outpath = os.path.join(output_dir, your_filename + '.mhd') sitk.WriteImage(itk_image_stack, outpath)
Hi sebyo: I suggest you convert your PNG files to mhd format. You can do it using SimpleITK like this:
load each PNG file using sitk.ImageFileReader() and append each file to a list
convert the list to a 3d numpy array (note that the coordinate standard for numpy is [z,y,x] while for itk is [x,y,z]
save it as mhd using sitk.WriteImage()