Go to the documentation of this file.00001
00018 #include "VisualisationUtils/tfvisualizer.h"
00019 #ifndef Q_MOC_RUN
00020 #include <boost/foreach.hpp>
00021 #include <cv_bridge/cv_bridge.h>
00022 #endif
00023
00024 TF_Visualizer::TF_Visualizer(const std::vector<std::string>& frame_ids, std::string camera_input_topic, std::string camera_output_topic)
00025 : it_(nh_), frame_ids_(frame_ids), camera_input_topic_(camera_input_topic), camera_output_topic_(camera_output_topic)
00026 {
00027 }
00028
00029 void TF_Visualizer::imageCb(const sensor_msgs::ImageConstPtr& image_msg, const sensor_msgs::CameraInfoConstPtr& info_msg)
00030 {
00031 cv::Mat image;
00032 cv_bridge::CvImagePtr input_bridge;
00033 try {
00034 input_bridge = cv_bridge::toCvCopy(image_msg, sensor_msgs::image_encodings::BGR8);
00035 image = input_bridge->image;
00036 }
00037 catch (cv_bridge::Exception& ex){
00038 ROS_ERROR("[draw_frames] Failed to convert image");
00039 return;
00040 }
00041
00042 cam_model_.fromCameraInfo(info_msg);
00043
00044 BOOST_FOREACH(const std::string& frame_id, frame_ids_) {
00045 tf::StampedTransform transform;
00046 try {
00047 ros::Time acquisition_time = info_msg->header.stamp;
00048 ros::Duration timeout(1.0 / 30);
00049 tf_listener_.waitForTransform(cam_model_.tfFrame(), frame_id,
00050 acquisition_time, timeout);
00051 tf_listener_.lookupTransform(cam_model_.tfFrame(), frame_id,
00052 acquisition_time, transform);
00053 }
00054 catch (tf::TransformException& ex) {
00055 ROS_WARN("[draw_frames] TF exception:\n%s", ex.what());
00056 return;
00057 }
00058
00059 tf::Point pt = transform.getOrigin();
00060 cv::Point3d pt_cv(pt.x(), pt.y(), pt.z());
00061 cv::Point2d uv;
00062 uv = cam_model_.project3dToPixel(pt_cv);
00063
00064 static const int RADIUS = 3;
00065 cv::circle(image, uv, RADIUS, CV_RGB(255,0,0), -1);
00066 CvSize text_size;
00067 int baseline;
00068 cvGetTextSize(frame_id.c_str(), &font_, &text_size, &baseline);
00069 CvPoint origin = cvPoint(uv.x - text_size.width / 2,
00070 uv.y - RADIUS - baseline - 3);
00071 cv::putText(image, frame_id.c_str(), origin, cv::FONT_HERSHEY_SIMPLEX, 12, CV_RGB(255,0,0));
00072 }
00073
00074 pub_.publish(input_bridge->toImageMsg());
00075 }
00076
00077 void TF_Visualizer::run()
00078 {
00079 std::string image_topic = nh_.resolveName(camera_input_topic_);
00080 sub_ = it_.subscribeCamera(image_topic, 1, &TF_Visualizer::imageCb, this);
00081 pub_ = it_.advertise(camera_output_topic_, 1);
00082 cvInitFont(&font_, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5);
00083 }
00084
00085 void TF_Visualizer::stop()
00086 {
00087
00088
00089 }