I'm trying to reproduce the scaling matrix of ITK-SNAP on python but I have failed. I have a fixed image and a moving matrix. I tried to set the scale factors of GUI as Sx = 2; Sy = 1; Sz = 1. When I save the transformation matrix I obtained:
1.99705 -0.050182 0.0206233 4.81584
-0.050182 1.00253 -0.00103798 -0.242384
0.0206233 -0.00103798 1.00043 0.0996128
0 0 0 1
Trying to reproduce it I try:
import numpy as np
import nibabel as nib
img1 = nib.load('moving.nii')
img2 = nib.load('fixed.nii')
data1 = img1.get_fdata()
data2 = img2.get_fdata()
affine1 = img1.affine
affine2 = img2.affine
xs = 2.0
ys = 1.0
zs = 1.0
SCAL=np.array([[1*xs,0,0,0],[0,1*ys,0,0],[0,0,1*zs,0],[0,0,0,1]])
SCAL = np.linalg.inv(affine1)@SCAL@affine1
FLIPXY_44 = np.diag([-1, -1, 1, 1])
SCAL = FLIPXY_44 @ SCAL @ np.linalg.inv(FLIPXY_44)
print(SCAL)
The result is bellow:
SCAL=
1.98161416 -0.132748624 0.0206291961 104.125084
-0.132748922 1.01795230 -0.00278979630 -14.0813909
0.0206292419 -0.00278979623 1.00043354 2.18825444
0 0 0 1
Without thinking on translation of center of scaling on center of image, I'm trying to reproduce only the rotation matrix, i.e., SCAL[:3,:3] and as you can see It is not exactly the same. Anybody knows how to calculate it exactly?
affine1 =
0.743074 -0.100490 -0.015616 -78.821861
0.100468 0.743237 -0.002111 -95.039352
0.015758 0.000000 0.749834 -163.115112
0.000000 0.000000 0.000000 1.000000
affine2 =
0.320133 -0.189304 0.056924 -66.191055
0.189440 0.274100 -0.177681 4.535652
0.047455 0.182846 0.325291 -103.358688
0.000000 0.000000 0.000000 1.000000
I could reproduce simulations of rotation and translation from GUI of ITK-SNAP, but Scaling I couldn't, Anyone could help me? Thanks in advance
PS: I based my trying in this post: How to factor the ITK CenterOfRotationPoint in an affine transformation matrix?
I'm trying to reproduce the scaling matrix of ITK-SNAP on python but I have failed. I have a fixed image and a moving matrix. I tried to set the scale factors of GUI as Sx = 2; Sy = 1; Sz = 1. When I save the transformation matrix I obtained:
1.99705 -0.050182 0.0206233 4.81584
-0.050182 1.00253 -0.00103798 -0.242384
0.0206233 -0.00103798 1.00043 0.0996128
0 0 0 1
Trying to reproduce it I try:
import numpy as np
import nibabel as nib
img1 = nib.load('moving.nii')
img2 = nib.load('fixed.nii')
data1 = img1.get_fdata()
data2 = img2.get_fdata()
affine1 = img1.affine
affine2 = img2.affine
xs = 2.0
ys = 1.0
zs = 1.0
SCAL=np.array([[1*xs,0,0,0],[0,1*ys,0,0],[0,0,1*zs,0],[0,0,0,1]])
SCAL = np.linalg.inv(affine1)@SCAL@affine1
FLIPXY_44 = np.diag([-1, -1, 1, 1])
SCAL = FLIPXY_44 @ SCAL @ np.linalg.inv(FLIPXY_44)
print(SCAL)
The result is bellow:
SCAL=
1.98161416 -0.132748624 0.0206291961 104.125084
-0.132748922 1.01795230 -0.00278979630 -14.0813909
0.0206292419 -0.00278979623 1.00043354 2.18825444
0 0 0 1
Without thinking on translation of center of scaling on center of image, I'm trying to reproduce only the rotation matrix, i.e., SCAL[:3,:3] and as you can see It is not exactly the same. Anybody knows how to calculate it exactly?
affine1 =
0.743074 -0.100490 -0.015616 -78.821861
0.100468 0.743237 -0.002111 -95.039352
0.015758 0.000000 0.749834 -163.115112
0.000000 0.000000 0.000000 1.000000
affine2 =
0.320133 -0.189304 0.056924 -66.191055
0.189440 0.274100 -0.177681 4.535652
0.047455 0.182846 0.325291 -103.358688
0.000000 0.000000 0.000000 1.000000
I could reproduce simulations of rotation and translation from GUI of ITK-SNAP, but Scaling I couldn't, Anyone could help me? Thanks in advance
PS: I based my trying in this post: How to factor the ITK CenterOfRotationPoint in an affine transformation matrix?
Share Improve this question asked Mar 17 at 14:03 BrunoPBrunoP 11 bronze badge1 Answer
Reset to default 0For ITK-NiBabel conversions, you might want to take a look at this Jupyter notebook.