Converting cv::Mat rotation matrix to quaternion
Is there a way to convert a cv::Mat rotation matrix to a quaternion?
ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
Is there a way to convert a cv::Mat rotation matrix to a quaternion?
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);
Asked: 2020-07-28 11:39:15 -0500
Seen: 2,823 times
Last updated: Jul 29 '20
How to publish messages on a topic of type rosgraph_msgs/Clock?
how can i publish a std::map<std::string, std::string> to a ros topic using roscpp?
Are there macros which define the ROS version?
Unable to install keys for ROS Kinetic using Ubuntu 16.04
How to open a serial port using a ROS node?
How do I add the robot footprint area to the map?
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 anEigen::Affine3d
vs atf2::Matrix3x3
vs something else.If you do have a
tf2::Matrix3x3
you could use thegetRotation
methodIn fact, I have a cv::Mat object and I want to convert it to a quaternion to populate a geometry_msgs/Pose message.
I'll try! Thanks.