From e8214234fbca461f5d30945cf975d1360f07caa4 Mon Sep 17 00:00:00 2001 From: Ted Steiner Date: Tue, 29 Oct 2024 14:02:19 -0400 Subject: [PATCH] Use TF2 package for quaternion conversion (#1031) The OpenCV quaternion class was not added until OpenCV 4.5.1, so it's less widely available than the TF2 conversion. This change allows a source build of the ROS 2 "perception" variant on Ubuntu 20.04 without a custom source build of OpenCV. Addresses issue https://github.com/ros-perception/image_pipeline/issues/1030 (cherry picked from commit 4993e05e86fb742e29e6b94d8ad66f31e4aa4f1c) --- image_proc/package.xml | 2 ++ image_proc/src/track_marker.cpp | 12 ++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/image_proc/package.xml b/image_proc/package.xml index 869a10212..235dc4f51 100644 --- a/image_proc/package.xml +++ b/image_proc/package.xml @@ -30,6 +30,8 @@ rclcpp_components rcutils sensor_msgs + tf2 + tf2_geometry_msgs tracetools_image_pipeline ament_lint_auto diff --git a/image_proc/src/track_marker.cpp b/image_proc/src/track_marker.cpp index e103de067..861774388 100644 --- a/image_proc/src/track_marker.cpp +++ b/image_proc/src/track_marker.cpp @@ -30,6 +30,8 @@ // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. +#include + #include #include #include @@ -40,9 +42,9 @@ #include #include #include -#include #include #include +#include namespace image_proc { @@ -135,11 +137,9 @@ void TrackMarkerNode::imageCb( pose.pose.position.y = tvecs[0][1]; pose.pose.position.z = tvecs[0][2]; // Convert angle-axis to quaternion - cv::Quatd q = cv::Quatd::createFromRvec(rvecs[0]); - pose.pose.orientation.x = q.x; - pose.pose.orientation.y = q.y; - pose.pose.orientation.z = q.z; - pose.pose.orientation.w = q.w; + const tf2::Vector3 rvec(rvecs[0][0], rvecs[0][1], rvecs[0][2]); + const tf2::Quaternion q(rvec.normalized(), rvec.length()); + tf2::convert(q, pose.pose.orientation); pub_->publish(pose); } }