I want to write the image [that I have loaded into array] after changing the values of array into a SimpleITK Image. How can I set the spacing and origin?
[res_file_dir
is the path to the nii.gz
file]
segmentation_result=sitk.ReadImage(res_file_dir,sitk.sitkUInt8) #segmentation_result.GetSpacing() (0.3645833432674408, 0.3645833432674408, 0.699999988079071)
seg=sitk.GetArrayFromImage(segmentation_result) #shape (105, 242, 176)
seg[seg!=obj_label]=0
#segmentation=sitk.Image(segmentation_result.GetSize(),sitk.sitkUInt8)
numpyOrigin = np.array(list(reversed(segmentation_result.GetOrigin())))
numpySpacing = np.array(list(reversed(segmentation_result.GetSpacing())))
segmentation = sitk.GetImageFromArray(seg, sitk.sitkVectorUInt8).SetSpacing(numpySpacing)
once I check the spacing of new image, it shows the following:
segmentation.GetSpacing()
*** AttributeError: 'NoneType' object has no attribute 'GetSpacing'