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

Apply transformation matrix to existing frame

asked 2018-04-19 13:35:31 -0500

Georgee gravatar image

updated 2018-04-19 13:55:03 -0500

As the title says, I have a frame, which comes by default in the Kinect, and then I calculated a 4x4 transformation matrix. How can I apply it to the existing frame?

Thanks in advance.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-04-19 20:32:17 -0500

Geoff gravatar image

updated 2018-04-22 18:09:55 -0500

The easiest method is to create a new frame that is the transformed Kinect frame. You can then use that frame throughout your system.

You can create a new frame from your transformation matrix and broadcast it using tf2. See this tutorial for how to broadcast a transform. If the transform will never change, you can also follow this tutorial to broadcast a static frame. (There's also a tutorial specifically about adding a new frame, but it's much the same as the broadcast frame tutorial.)

To create the frame from your transformation matrix, you need to convert it to tf2's Matrix3x3 type for the rotation part and a Vector3 for the translation part. From that, you can construct a tf2::Transform object, passing the matrix into the constructor. That can then be converted to the message data type for broadcasting using the tf2::fromMsg() function

tf2::Matrix3x3 tm(xx, xy, xz, yx, yy, yz, zx, zy, zz);
tf2::Vector3 translation(x, y, z);
tf2::Transform trans(tm, translation);
geometry_msgs::TransformStamped new_frame;
new_frame.header.stamp = ros::Time::now();
new_frame.header.frame_id = "kinect_frame_name";
new_frame.child_frame_id = "transformed_kinect";
new_frame.transform = tf2::toMsg(trans);

If you know the Euler rotation angles originally used to create the transformation matrix, then another option is to convert them to a quaternion and put that into the message's transform member.

On the other hand, if you want to use the transformed frame locally only, you can get the pose of the Kinect frame, convert it to a tf2::Transform object using the tf2::fromMsg() function. This will convert a geometry_msgs::Pose to a tf2::Transform. You can then convert your 4x4 transformation matrix to another tf2::Transform object, multiply them together to get the resulting transformation from the origin of the frame you original got the Kinect frame relative to, and that's the pose of your transformed Kinect frame.

edit flag offensive delete link more


Thank you very much for your detailed response. I'm trying to apply everything you told me but I can't help but notice that the last part is ideal for me. Isn't there a way to subscribe to that tf, save it somewhere and multiply it with the transformation matrix and then publish the new one?

Georgee gravatar image Georgee  ( 2018-04-20 03:20:54 -0500 )edit

That's what the first part does.

Geoff gravatar image Geoff  ( 2018-04-20 08:35:12 -0500 )edit

Thanks, it worked perfectly ^^

Georgee gravatar image Georgee  ( 2018-04-20 10:11:44 -0500 )edit

Question Tools



Asked: 2018-04-19 13:35:31 -0500

Seen: 4,732 times

Last updated: Apr 22 '18