aruco_ros_utils.cpp
Go to the documentation of this file.
00001 #include <aruco_ros/aruco_ros_utils.h>
00002 #include <ros/console.h>
00003 #include <ros/assert.h>
00004 #include <iostream>
00005 #include <tf/transform_datatypes.h>
00006 
00007 aruco::CameraParameters aruco_ros::rosCameraInfo2ArucoCamParams(const sensor_msgs::CameraInfo& cam_info,
00008                                                                 bool useRectifiedParameters)
00009 {
00010     cv::Mat cameraMatrix(3, 3, CV_32FC1);
00011     cv::Mat distorsionCoeff(4, 1, CV_32FC1);
00012     cv::Size size(cam_info.height, cam_info.width);
00013 
00014     if ( useRectifiedParameters )
00015     {
00016       cameraMatrix.setTo(0);
00017       cameraMatrix.at<float>(0,0) = cam_info.P[0];   cameraMatrix.at<float>(0,1) = cam_info.P[1];   cameraMatrix.at<float>(0,2) = cam_info.P[2];
00018       cameraMatrix.at<float>(1,0) = cam_info.P[4];   cameraMatrix.at<float>(1,1) = cam_info.P[5];   cameraMatrix.at<float>(1,2) = cam_info.P[6];
00019       cameraMatrix.at<float>(2,0) = cam_info.P[8];   cameraMatrix.at<float>(2,1) = cam_info.P[9];   cameraMatrix.at<float>(2,2) = cam_info.P[10];
00020 
00021       for(int i=0; i<4; ++i)
00022         distorsionCoeff.at<float>(i, 0) = 0;
00023     }
00024     else
00025     {
00026       for(int i=0; i<9; ++i)
00027         cameraMatrix.at<float>(i%3, i-(i%3)*3) = cam_info.K[i];
00028 
00029       if(cam_info.D.size() == 4)
00030       {
00031         for(int i=0; i<4; ++i)
00032           distorsionCoeff.at<float>(i, 0) = cam_info.D[i];
00033       }
00034       else
00035       {
00036         ROS_WARN("length of camera_info D vector is not 4, assuming zero distortion...");
00037         for(int i=0; i<4; ++i)
00038           distorsionCoeff.at<float>(i, 0) = 0;
00039       }
00040     }
00041 
00042     return aruco::CameraParameters(cameraMatrix, distorsionCoeff, size);
00043 }
00044 
00045 tf::Transform aruco_ros::arucoMarker2Tf(const aruco::Marker &marker)
00046 {
00047     cv::Mat rot(3, 3, CV_32FC1);
00048     cv::Rodrigues(marker.Rvec, rot);
00049     cv::Mat tran = marker.Tvec;
00050 
00051     cv::Mat rotate_to_ros(3, 3, CV_32FC1);
00052     // -1 0 0
00053     // 0 0 1
00054     // 0 1 0
00055     rotate_to_ros.at<float>(0,0) = -1.0;
00056     rotate_to_ros.at<float>(0,1) = 0.0;
00057     rotate_to_ros.at<float>(0,2) = 0.0;
00058     rotate_to_ros.at<float>(1,0) = 0.0;
00059     rotate_to_ros.at<float>(1,1) = 0.0;
00060     rotate_to_ros.at<float>(1,2) = 1.0;
00061     rotate_to_ros.at<float>(2,0) = 0.0;
00062     rotate_to_ros.at<float>(2,1) = 1.0;
00063     rotate_to_ros.at<float>(2,2) = 0.0;
00064     rot = rot*rotate_to_ros.t();
00065 
00066     tf::Matrix3x3 tf_rot(rot.at<float>(0,0), rot.at<float>(0,1), rot.at<float>(0,2),
00067                        rot.at<float>(1,0), rot.at<float>(1,1), rot.at<float>(1,2),
00068                        rot.at<float>(2,0), rot.at<float>(2,1), rot.at<float>(2,2));
00069 
00070     tf::Vector3 tf_orig(tran.at<float>(0,0), tran.at<float>(1,0), tran.at<float>(2,0));
00071 
00072     return tf::Transform(tf_rot, tf_orig);
00073 }


aruco_ros
Author(s): Rafael Muñoz Salinas , Bence Magyar
autogenerated on Thu Aug 27 2015 12:26:22