ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Converting cv::Mat rotation matrix to quaternion

asked 2020-07-28 11:39:15 -0600

updated 2020-07-28 18:10:47 -0600

Is there a way to convert a cv::Mat rotation matrix to a quaternion?

edit retag flag offensive close merge delete

Comments

You tagged roscpp so I'd assume you are using CPP... is that right? It would likely help to say what datatype you have a rotation matrix as. For example, the answer you're looking for will depend on if you have an Eigen::Affine3d vs a tf2::Matrix3x3 vs something else.

jarvisschultz gravatar image jarvisschultz  ( 2020-07-28 12:25:50 -0600 )edit
1

If you do have a tf2::Matrix3x3 you could use the getRotation method

jarvisschultz gravatar image jarvisschultz  ( 2020-07-28 12:27:24 -0600 )edit

In fact, I have a cv::Mat object and I want to convert it to a quaternion to populate a geometry_msgs/Pose message.

EDU4RDO-SH gravatar image EDU4RDO-SH  ( 2020-07-28 12:34:49 -0600 )edit

I'll try! Thanks.

EDU4RDO-SH gravatar image EDU4RDO-SH  ( 2020-07-28 14:18:01 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
5

answered 2020-07-28 12:57:50 -0600

updated 2020-07-29 10:34:50 -0600

In that case, personally, I'd lean towards constructing a tf2::Matrix3x3 from the cv::Mat. Then you can construct a tf2::Transform from the Matrix3x3, and then use tf2::toMsg to convert to a geometry_msgs/Pose directly. You could also call getRotation as I mentioned earlier to get a Quaternion directly, and then fill out the components of the pose.orientation with the components of the quaternion.

A quick Google also found this which might be helpful: https://gist.github.com/shubh-agrawal...

EDIT

Since this became the accepted answer, I figured I'd post a little example of what I described. Something like the following should work:

#include <opencv2/calib3d.hpp>
#include <opencv2/core.hpp>
#include <tf2/LinearMath/Matrix3x3.h>
#include <tf2/LinearMath/Transform.h>
#include <tf2/LinearMath/Vector3.h>
#include <tf2_geometry_msgs/tf2_geometry_msgs.h>

// Build identity rotation matrix as a cv::Mat
cv::Mat rot(3, 3, CV_64FC1);
cv::Rodrigues(cv::Vec3d({0.0, 0.0, 0.0}), rot);
// Convert to a tf2::Matrix3x3
tf2::Matrix3x3 tf2_rot(rot.at<double>(0, 0), rot.at<double>(0, 1), rot.at<double>(0, 2),
                       rot.at<double>(1, 0), rot.at<double>(1, 1), rot.at<double>(1, 2),
                       rot.at<double>(2, 0), rot.at<double>(2, 1), rot.at<double>(2, 2));
// Create a transform and convert to a Pose
tf2::Transform tf2_transform(tf2_rot, tf2::Vector3());
geometry_msgs::Pose pose_msg;
tf2::toMsg(tf2_transform, pose_msg);
edit flag offensive delete link more

Comments

Is there a way to convert tf::Mtrix3x3 in cv::Mat?

Elektron97 gravatar image Elektron97  ( 2021-07-26 09:11:42 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-07-28 11:39:15 -0600

Seen: 3,056 times

Last updated: Jul 29 '20