{
  "id": 110884,
  "title": "Conversion between quaternions and Euler angles (yaw/pitch/roll)",
  "url": "/competitions/3d-object-detection-for-autonomous-vehicles/discussion/110884",
  "author_name": "",
  "post_date": "2019-10-01T21:11:08.056818500Z",
  "votes": 5,
  "comment_count": 3,
  "views": 0,
  "content": "<p>Hello, </p>\n\n<p>As we have yaw values in the csv file, and we have \"rotation\" (quaternion) in the sample_annotation.json file. The following <a href=\"https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles\">wiki </a>is helpful to convert between these two.</p>",
  "messages": [
    {
      "id": "638388",
      "postDate": "10/01/2019 21:11:08",
      "content": "<p>Hello, </p>\n\n<p>As we have yaw values in the csv file, and we have \"rotation\" (quaternion) in the sample_annotation.json file. The following <a href=\"https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles\">wiki </a>is helpful to convert between these two.</p>",
      "rawMarkdown": "Hello, \n\nAs we have yaw values in the csv file, and we have \"rotation\" (quaternion) in the sample_annotation.json file. The following [wiki ](https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles)is helpful to convert between these two.",
      "votes": null
    },
    {
      "id": "638390",
      "postDate": "10/01/2019 21:21:43",
      "content": "<p>Thanks, here's how I'm doing the conversions:</p>\n\n<p>from yaw to quaternion:</p>\n\n<p><code>from pyquaternion import Quaternion</code>\n<code>quat = Quaternion(scalar=np.cos(yaw / 2), vector=[0, 0, np.sin(yaw / 2)])</code></p>\n\n<p>or </p>\n\n<p><code>quat = Quaternion(axis=[0, 0, 1], radians=yaw)</code></p>\n\n<p>from quaternion to yaw angle (in radians):</p>\n\n<p>```\ndef quaternion_yaw(q: Quaternion) -&gt; float:\n    \"\"\"\n    Calculate the yaw angle from a quaternion.\n    Note that this only works for a quaternion that represents a box in lidar or global coordinate frame.\n    It does not work for a box in the camera frame.\n    :param q: Quaternion of interest.\n    :return: Yaw angle in radians.\n    \"\"\"</p>\n\n<pre><code># Project into xy plane.\nv = np.dot(q.rotation_matrix, np.array([1, 0, 0]))\n\n# Measure yaw using arctan.\nyaw = np.arctan2(v[1], v[0])\n\nreturn yaw\n</code></pre>\n\n<p>```\n<a href=\"https://github.com/nutonomy/nuscenes-devkit/blob/master/python-sdk/nuscenes/eval/detection/utils.py#L162\">quaternion_yaw</a></p>",
      "rawMarkdown": "Thanks, here's how I'm doing the conversions:\n\nfrom yaw to quaternion:\n\n`from pyquaternion import Quaternion`\n`quat = Quaternion(scalar=np.cos(yaw / 2), vector=[0, 0, np.sin(yaw / 2)])`\n\nor \n\n`quat = Quaternion(axis=[0, 0, 1], radians=yaw)`\n\nfrom quaternion to yaw angle (in radians):\n\n```\ndef quaternion_yaw(q: Quaternion) -&gt; float:\n    \"\"\"\n    Calculate the yaw angle from a quaternion.\n    Note that this only works for a quaternion that represents a box in lidar or global coordinate frame.\n    It does not work for a box in the camera frame.\n    :param q: Quaternion of interest.\n    :return: Yaw angle in radians.\n    \"\"\"\n\n    # Project into xy plane.\n    v = np.dot(q.rotation_matrix, np.array([1, 0, 0]))\n\n    # Measure yaw using arctan.\n    yaw = np.arctan2(v[1], v[0])\n\n    return yaw\n```\n[quaternion_yaw](https://github.com/nutonomy/nuscenes-devkit/blob/master/python-sdk/nuscenes/eval/detection/utils.py#L162)",
      "votes": null
    },
    {
      "id": "638393",
      "postDate": "10/01/2019 21:22:52",
      "content": "<p>Yes, I just want to add this! **pyquaternion  **already does this for us.</p>",
      "rawMarkdown": "Yes, I just want to add this! **pyquaternion  **already does this for us.",
      "votes": null
    },
    {
      "id": "667364",
      "postDate": "11/07/2019 06:37:50",
      "content": "<p>Thanks for sharing this, your code saved me.</p>",
      "rawMarkdown": "Thanks for sharing this, your code saved me.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 638390,
      "author_name": "rishabhiitbhu",
      "author_url": "",
      "post_date": "10/01/2019 21:21:43",
      "content": "<p>Thanks, here's how I'm doing the conversions:</p>\n\n<p>from yaw to quaternion:</p>\n\n<p><code>from pyquaternion import Quaternion</code>\n<code>quat = Quaternion(scalar=np.cos(yaw / 2), vector=[0, 0, np.sin(yaw / 2)])</code></p>\n\n<p>or </p>\n\n<p><code>quat = Quaternion(axis=[0, 0, 1], radians=yaw)</code></p>\n\n<p>from quaternion to yaw angle (in radians):</p>\n\n<p>```\ndef quaternion_yaw(q: Quaternion) -&gt; float:\n    \"\"\"\n    Calculate the yaw angle from a quaternion.\n    Note that this only works for a quaternion that represents a box in lidar or global coordinate frame.\n    It does not work for a box in the camera frame.\n    :param q: Quaternion of interest.\n    :return: Yaw angle in radians.\n    \"\"\"</p>\n\n<pre><code># Project into xy plane.\nv = np.dot(q.rotation_matrix, np.array([1, 0, 0]))\n\n# Measure yaw using arctan.\nyaw = np.arctan2(v[1], v[0])\n\nreturn yaw\n</code></pre>\n\n<p>```\n<a href=\"https://github.com/nutonomy/nuscenes-devkit/blob/master/python-sdk/nuscenes/eval/detection/utils.py#L162\">quaternion_yaw</a></p>",
      "votes": null,
      "replies": [
        {
          "id": 638393,
          "author_name": "yi233333",
          "author_url": "",
          "post_date": "10/01/2019 21:22:52",
          "content": "<p>Yes, I just want to add this! **pyquaternion  **already does this for us.</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 667364,
          "author_name": "usherbob",
          "author_url": "",
          "post_date": "11/07/2019 06:37:50",
          "content": "<p>Thanks for sharing this, your code saved me.</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "638388": "Hello, \n\nAs we have yaw values in the csv file, and we have \"rotation\" (quaternion) in the sample_annotation.json file. The following [wiki ](https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles)is helpful to convert between these two.",
    "638390": "Thanks, here's how I'm doing the conversions:\n\nfrom yaw to quaternion:\n\n`from pyquaternion import Quaternion`\n`quat = Quaternion(scalar=np.cos(yaw / 2), vector=[0, 0, np.sin(yaw / 2)])`\n\nor \n\n`quat = Quaternion(axis=[0, 0, 1], radians=yaw)`\n\nfrom quaternion to yaw angle (in radians):\n\n```\ndef quaternion_yaw(q: Quaternion) -&gt; float:\n    \"\"\"\n    Calculate the yaw angle from a quaternion.\n    Note that this only works for a quaternion that represents a box in lidar or global coordinate frame.\n    It does not work for a box in the camera frame.\n    :param q: Quaternion of interest.\n    :return: Yaw angle in radians.\n    \"\"\"\n\n    # Project into xy plane.\n    v = np.dot(q.rotation_matrix, np.array([1, 0, 0]))\n\n    # Measure yaw using arctan.\n    yaw = np.arctan2(v[1], v[0])\n\n    return yaw\n```\n[quaternion_yaw](https://github.com/nutonomy/nuscenes-devkit/blob/master/python-sdk/nuscenes/eval/detection/utils.py#L162)",
    "638393": "Yes, I just want to add this! **pyquaternion  **already does this for us.",
    "667364": "Thanks for sharing this, your code saved me."
  },
  "source": "meta"
}