I'm changing the voxel size of some 3D volumes. How do I edit the pixdim
field and calculate the new affine
?
I have some MR volumes which are anisotropic, the voxel sizes are, say, 0.5 x 0.5 x 3
mm. And I've some code to interpolate them into isotropic (like 0.5 x 0.5 x 0.5
mm voxel size) volumes.
The problem is when I need to save the file, I have to calculate the affine to map the now denser voxels in ijk space to reference xyz space. How do I do that?
First, my thought is to use the old affine and calculate the new affine.
For example, if volume V has a shape of 256 x 256 x 20
voxels with 0.5 x 0.5 x 3
voxel size and interpolated into volume U 256 x 256 x 120
voxels with 0.5 x 0.5 x 0.5
voxelsize.
The old affine will do [255 255 19]OA = [X Y Z]
and the new affine should do [255 255 119]NA = [X Y Z]
and we know AX=B X=inverse(A)B
.
So the new affine should be inverse([255 255 119])[X Y Z]
.
However, the inverse matrix only exists for a square matrix. There will be no such thing of inverse([255 255 119])
.
And it seems there is no set_voxel_size
function in python-nibabel matlab-nifti-toolbox and so. How does that a case?
How do I change the voxel size explicitly?