A repository explaining how to find the average quaternion given several weighted quaternions. As I had trouble finding example code that solves this problem, I think that this will be helpful to others. I worked on that problem in a robot related project, therefore I used datatypes like tf::Quaternion
or tf::Point
which are taken from the tf
package associated with ROS (Robot Operating System). I am pretty sure you can easily replace them with the respective Eigen
-versions. Also I am not sure whether I added all necessary includes, so bear with me. Nevertheless, I hope this will help people with the same problem!
Credits go to this answer on SO which basically sums up steps given in this paper. Also there is a python implementation that is doing the same.
Given a list of quaternions with corresponding weights we want to find a single quaternion that represents their weighted average.
First we construct a matrix which contains the weighted quaternions (represented as column vectors) as outlined below:
Then we multiply this matrix with its transposed version resulting in a Matrix . Then we have to find the largest Eigenvalue of and take the corresponding Eigenvector:
Then our averaged quaternion is simply: