Replies: 1 comment
-
the spatial resample transform should work in this case, script would be something like the one I attached (this is using monai v0.9.1 and it's not tested with 3d slicer) it generates a new nifti file with the updated affine. depending on your linear transform's convention I think you can set import torch
import monai
from monai.data.utils import orientation_ras_lps
# download an exmaple
monai.apps.download_url(
"https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/avg152T1_RL_nifti.nii.gz"
)
filename = "avg152T1_RL_nifti.nii.gz"
img = monai.transforms.LoadImage(image_only=True, ensure_channel_first=True)(filename)
# linear transform to apply
xform_linear = torch.as_tensor(
[[0.5, 0, 0, -45], [0, 0.5, 0, -45], [0, 0, 0.5, -50], [0, 0, 0, 1]], dtype=torch.float64
)
lps_xform = True
if lps_xform:
dst_affine = orientation_ras_lps(xform_linear @ orientation_ras_lps(img.affine))
else:
dst_affine = xform_linear @ img.affine
new_img = monai.transforms.SpatialResample(mode="nearest", padding_mode="zeros")(img, dst_affine=dst_affine)
monai.transforms.SaveImage(resample=False)(new_img) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
With 3D Slicer, I can apply a 4*4 linear transformation matrix (

), and create a new one with the BRAINSResample module of 3D Slicer (


) to a nifty feature image (
), which I can then save. The new one has the characteristics:
(note: the spacing and affine have varied). I want to write a python script based on monai to reproduce the same processing, but completely outside of 3D Slicer.
I've tried a few combinations, including:
1- changing the affine, header sform or header qform parameter values of the original nifty image
2- also by trying monai.transforms.SpatialResampling
With the first option, if I pass an affine that is not a diagonal matrix, it is not possible to display the transform on 3D Slicer. Moreover, I don't think the affine changes. The software can no longer read it. But I don't know.
As for the second option, I can't even get it to work
Beta Was this translation helpful? Give feedback.
All reactions