tfvisualizer.cpp
Go to the documentation of this file.
1 
19 #ifndef Q_MOC_RUN
20 #include <boost/foreach.hpp>
21 #include <cv_bridge/cv_bridge.h>
22 #endif
23 
24 TF_Visualizer::TF_Visualizer(const std::vector<std::string>& frame_ids, std::string camera_input_topic, std::string camera_output_topic)
25 : it_(nh_), frame_ids_(frame_ids), camera_input_topic_(camera_input_topic), camera_output_topic_(camera_output_topic)
26 {
27 }
28 
29 void TF_Visualizer::imageCb(const sensor_msgs::ImageConstPtr& image_msg, const sensor_msgs::CameraInfoConstPtr& info_msg)
30 {
31  cv::Mat image;
32  cv_bridge::CvImagePtr input_bridge;
33  try {
34  input_bridge = cv_bridge::toCvCopy(image_msg, sensor_msgs::image_encodings::BGR8);
35  image = input_bridge->image;
36  }
37  catch (cv_bridge::Exception& ex){
38  ROS_ERROR("[draw_frames] Failed to convert image");
39  return;
40  }
41 
42  cam_model_.fromCameraInfo(info_msg);
43 
44  BOOST_FOREACH(const std::string& frame_id, frame_ids_) {
45  tf::StampedTransform transform;
46  try {
47  ros::Time acquisition_time = info_msg->header.stamp;
48  ros::Duration timeout(1.0 / 30);
49  tf_listener_.waitForTransform(cam_model_.tfFrame(), frame_id,
50  acquisition_time, timeout);
51  tf_listener_.lookupTransform(cam_model_.tfFrame(), frame_id,
52  acquisition_time, transform);
53  }
54  catch (tf::TransformException& ex) {
55  ROS_WARN("[draw_frames] TF exception:\n%s", ex.what());
56  return;
57  }
58 
59  tf::Point pt = transform.getOrigin();
60  cv::Point3d pt_cv(pt.x(), pt.y(), pt.z());
61  cv::Point2d uv;
62  uv = cam_model_.project3dToPixel(pt_cv);
63 
64  static const int RADIUS = 3;
65  cv::circle(image, uv, RADIUS, CV_RGB(255,0,0), -1);
66  CvSize text_size;
67  int baseline;
68  cvGetTextSize(frame_id.c_str(), &font_, &text_size, &baseline);
69  CvPoint origin = cvPoint(uv.x - text_size.width / 2,
70  uv.y - RADIUS - baseline - 3);
71  cv::putText(image, frame_id.c_str(), origin, cv::FONT_HERSHEY_SIMPLEX, 12, CV_RGB(255,0,0));
72  }
73 
74  pub_.publish(input_bridge->toImageMsg());
75 }
76 
78 {
79  std::string image_topic = nh_.resolveName(camera_input_topic_);
80  sub_ = it_.subscribeCamera(image_topic, 1, &TF_Visualizer::imageCb, this);
81  pub_ = it_.advertise(camera_output_topic_, 1);
82  cvInitFont(&font_, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5);
83 }
84 
86 {
87  //sub_ = null;
88  //pub_ = null;
89 }
image_transport::Publisher pub_
Definition: tfvisualizer.h:50
std::string resolveName(const std::string &name, bool remap=true) const
std::vector< std::string > frame_ids_
Definition: tfvisualizer.h:53
image_geometry::PinholeCameraModel cam_model_
Definition: tfvisualizer.h:52
void imageCb(const sensor_msgs::ImageConstPtr &image_msg, const sensor_msgs::CameraInfoConstPtr &info_msg)
#define ROS_WARN(...)
std::string camera_input_topic_
Definition: tfvisualizer.h:54
image_transport::ImageTransport it_
Definition: tfvisualizer.h:48
tf::TransformListener tf_listener_
Definition: tfvisualizer.h:51
std::string camera_output_topic_
Definition: tfvisualizer.h:55
ros::NodeHandle nh_
Definition: tfvisualizer.h:47
image_transport::CameraSubscriber sub_
Definition: tfvisualizer.h:49
#define ROS_ERROR(...)
TF_Visualizer(const std::vector< std::string > &frame_ids, std::string camera_input_topic, std::string camera_output_topic)


asr_mild_calibration_tool
Author(s): Aumann Florian, Heller Florian, Meißner Pascal
autogenerated on Mon Dec 2 2019 03:11:43