Niels
(nLs)
September 25, 2018, 2:12pm
1
Hello all,
In the transform hierarchy of my scene I added a LinearTransfromNode to rotate my volume to its correct pose. In my Python script I would like to get the value of the rotation in quaternion format. I need the value to save it to file and use it in another program.
I added the following code in python to get the matrix:
transformNodeID = inputVolume.GetTransformNodeID()
if transformNodeID :
transformNode = slicer.mrmlScene.GetNodeByID(transformNodeID )
matrix = transformNode .GetMatrixTransformToParent()
Now I have the vtkMatrix4x4, but I can not find any feature to convert it to a rotation I can use. I prefer quaternions.
1 Like
lassoan
(Andras Lasso)
September 25, 2018, 5:33pm
2
You can convert using vtkQuaternion .
1 Like
Niels
(nLs)
September 26, 2018, 9:39am
3
Thanks Andras, I will get the rotation part of the 4x4 and place them in a 3x3 and to see if I can get the quaternion from it.
Niels
(nLs)
September 27, 2018, 10:08am
4
For those interested, this is what I now use to get the quaternion from the volume transformNode.
if inputVolume.GetTransformNodeID():
transformNode = slicer.mrmlScene.GetNodeByID(inputVolume.GetTransformNodeID())
transfromMatrix = vtk.vtkMatrix4x4()
transformNode.GetMatrixTransformToWorld(transfromMatrix)
rotationMatrix = [[1,0,0],[0,1,0],[0,0,1]]
for i in range(3):
for j in range(3):
rotationMatrix[i][j] = transfromMatrix.GetElement(i,j)
rotationQuat = list(imageRotation)
vtk.vtkMath.Matrix3x3ToQuaternion(rotationMatrix, rotationQuat);
imageRotation = tuple( rotationQuat )
2 Likes