Save itk image with custom voxel spacing

Hello everyone!
I got output from the model which is a NumPy array of shape [128,448,448 ] .I converted to itkobject using itk.image_view_from_array(predicted_label) and save the image with itkwrite .But shape of input image was [448 448 128] with voxel spacing[0.5134 0.5134 0.8] but itk.write gives output[448 448 128] with voxel spacing [1 1 1 ].Any solution for this problem as I want output image with custom voxel spacing as shape([448 448 128],[0.5134 0.5134 0.8])
I tried to use nibabel but reshape the image distorts the result.

@matt.mccormick, @Stephen_Aylward

You could use ChangeInformationImageFilter. That is especially easy if you have the original image in ITK format. Example.

Or SetSpacing in your itk python image.

1 Like
    filter = itk.ChangeInformationImageFilter.New(___) # itk image
    filter.SetUseReferenceImage(True)
    filter.SetChangeSpacing(True)
    output_spacing = [0.5134, 0.5134 0.8]
    filter.SetOutputSpacing(output_spacing)
    writerInput =filter.GetOutput()
    writer = itk.ImageFileWriter[ImageType].New()
    writer.SetFileName(outputFilePath)
    writer.SetInput(writerInput)
    writer.Update()

is this will work ?

This should be false, as you are not using a reference image. The rest looks good.

But if you are only looking to change spacing, Pablo’s suggestion is probably simpler.

output_spacing = [0.5134, 0.5134 0.8]
image.SetSpacing(output_spacing)
itk.imwrite(image, outputFilePath)
1 Like

Thank you, sir @dzenanz @phcerdan, it worked.

1 Like