0

I'm interested in comparing the quaternions of an object presented in the real-world (with ArUco marker on top of it) and its simulated version in Unity3D.

To do this, I generated different scenes in Unity with the object in different locations. I stored its position and orientation relative to the camera in a csv file. where quaternions is looking something like this (for one example):

[-0.492555320262909 -0.00628990028053522 0.00224017538130283 0.870255589485168]

In ArUco, after using estimatePoseSingleMarkers I got a compact version of Angle-Axis, and I converted it to Quaternion using the following function:

def find_quat(rvecs):
  a = np.array(rvecs[0][0])
  theta = math.sqrt(a[0]**2 + a[1]**2 + a[2]**2) 
  b = a/theta 
  qx = b[0] * math.sin(theta/2)
  qy = -b[1] * math.sin(theta/2) # left-handed vs right handed
  qz = b[2] * math.sin(theta/2)
  qw = math.cos(theta/2)
  print(qx, qy, qz, qw)

where rvecs is the return value of ArUco

However, after doing this I'm still getting way different results, example of the same scene:

[0.9464098048208864 -0.02661258975275046 -0.009733748408866453 0.321722715311581] << aruco result

[-0.492555320262909 -0.00628990028053522 0.00224017538130283 0.870255589485168] << Unity's result

Sample input to find_quat:

[[[ 2.4849011 0.04546755 -0.030406 ]]] which is the output of estimatePoseSingleMarkers function

Unity's Quaternion is found as follows:

GameObject.Find("Cube").transform.localRotation;

Am I missing something?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Alice J
  • 59
  • 1
  • 7
  • Please include sample input to `find_quat` and how you are calculating "Unity's result" – Ruzihm Jul 09 '20 at 21:58
  • I don't know about aruco but it's possible that `localRotation` isn't an appropriate quaternion to compare with the output of `find_quat`. In unity, what are the angle and axis you get for [`float angle; Vector3 axis; GameObject.Find("Cube").transform.localRotation.ToAngleAxis(out angle, out axis);`](https://docs.unity3d.com/ScriptReference/Quaternion.ToAngleAxis.html) ? By the way, I don't know where you're calling `GameObject.Find` but avoid calling it frequently (e.g., in `Update`) - it's not a cheap operation. – Ruzihm Jul 09 '20 at 22:35
  • Thanks for the tip! this is the output for the angle:50.42004 and for the axis it is (-1,0,0) – Alice J Jul 09 '20 at 22:41
  • Yeah, that's definitely different - negative 50 degrees around the x axis is nowhere close to 2.5 radians around the x axis... Are any of the cube's ancestors rotated? Maybe try `cube.transform.rotation` instead – Ruzihm Jul 09 '20 at 22:48
  • it's (0,0,0,1) and yes the camera (parent of the cube) is rotated by 50. I think this has to do with the frames difference between openCV(right-handed) and Unity (left-handed) but not exactly sure,. :( – Alice J Jul 09 '20 at 23:05

1 Answers1

0

For anyone coming here trying to find an answer.

My problem was that I was having the marker on top of the cube (so rotated by -90) which made converting the orientation impossible.

Change your pivot point in Unity and rotate it by -90. Then convert by

(x,y,z,w) = (-x,y,-z,w)

Alice J
  • 59
  • 1
  • 7