JG98
JG98

Reputation: 43

Getting "Images Do Not Occupy Same Physical Space" Error When Trying to Multiply two .nii Files in Convert3d/ITK

I have to mask a .nii MRI image for an internship I'm doing. The method I'm using involves multiplying the proton density weighted version of the image (captured during the inversion pulse) by the original mp2rage image. In order to do this I'm using Convert3D, however I'm getting a weird error that says "Images Do Not Occupy Same Physical Space". I am really lost here, and don't have much experience with this software, so if anyone knows how to fix this, or a better way to multiply .nii/nifti files, it would be really helpful. I am completely stuck. It looks like their origins are slightly different, maybe this is causing the problem, however I still have no idea how to fix it. I have tried setting the origin on both images to the same value 100x100x100mm using -origin, and then multiplying. This allows multiplication, however, I'm not sure if the result is what I want or if this is the right way to go about multiplying these images. I've attached the result below, as well as the commands I used to get it. Original Commands with Error

Result of Multiplication after manually setting origins

New Commands that allowed multiplication

Upvotes: 1

Views: 739

Answers (2)

Dave Chen
Dave Chen

Reputation: 2085

In ITK the images have to have all the same parameters before it will allow you to edit them. I.e, the same dimensions, spacing, origin, and orientation. Do the origins of your images differ by a lot? Or is it just a slight numerical error?

If they do not have the spatial parameters, the proper thing to do is to resample one image on top of the other. Using SimpleITK-Python, you could do something like this:

import SimpleITK as sitk

mri_image = sitk.ReadImage("your_mri.nii")
mask_image = sitk.ReadImage("your_mask.nii")

resampled_mask = sitk.Resample(mask_image, mri_image, interpolator=sitk.sitkNearestNeighbor)

masked_mri_image = mri_image * resampled_mask

If you just have a slight numerical difference between the image origins, spacings or directions, you could force them to be the same like this:

mask_image.SetSpacing(mri_image.GetSpacing())
mask_image.SetOrigin(mri_image.GetOrigin())
mask_image.SetDirection(mri_image.GetDirection())

Upvotes: 2

Dženan
Dženan

Reputation: 3395

Doing pixel-wise multiplication requires the images to have the same size. To prevent errors, ITK's multiply image filter requires them to occupy the same physical space, too. For that they must have the same origin, spacing and orientation (direction), in addition to the same size. So indeed, it is a problem that their origins are slightly different.

You could open both in 3D Slicer, then modify the origin of one (e.g. the mask) to match the other, and save it. You can also use Slicer's Simple Filters module, and in there search for MultiplyImageFilter. That way you don't have to use Convert3D, and the results are visualized immediately.

enter image description here

Upvotes: 0

Related Questions