Rotate volume and get the Quaternion (or Euler)

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

You can convert using vtkQuaternion.

1 Like

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.

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