extract_image_positions.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <fstream>
00003 
00004 #include <ros/ros.h>
00005 #include <cv_bridge/cv_bridge.h>
00006 #include <tf/transform_listener.h>
00007 #include <message_filters/subscriber.h>
00008 #include <message_filters/time_synchronizer.h>
00009 #include <message_filters/sync_policies/approximate_time.h>
00010 #include <image_transport/subscriber_filter.h>
00011 #include <sensor_msgs/image_encodings.h>
00012 #include <auv_msgs/NavSts.h>
00013 #include <image_transport/image_transport.h>
00014 #include <opencv2/opencv.hpp>
00015 
00016  // Topic sync
00017   typedef message_filters::sync_policies::ApproximateTime<auv_msgs::NavSts,
00018                                                           sensor_msgs::Image,
00019                                                           sensor_msgs::CameraInfo> SyncPolicy;
00020   typedef message_filters::Synchronizer<SyncPolicy> Sync;
00021 
00022 class ImagePositions {
00023   ros::NodeHandle nh_;
00024   image_transport::ImageTransport it_;
00025   image_transport::CameraSubscriber cam_sub_;
00026   image_transport::CameraSubscriber sub_;
00027   tf::TransformListener tf_listener_;
00028   std::string parent_frame_id_;
00029   std::string child_frame_id_;
00030   std::string output_file_;
00031   std::string output_folder_;
00032   std::ofstream file_;
00033   int count_;
00034 
00035 public:
00036   ImagePositions(const std::vector<std::string>& frame_ids)
00037     : it_(nh_), count_(0){
00038     nh_.getParam("parent_frame_id", parent_frame_id_);
00039     nh_.getParam("child_frame_id", child_frame_id_);
00040     nh_.getParam("output_file", output_file_);
00041     nh_.getParam("output_folder", output_folder_);
00042     std::string fn(output_folder_ + "/" + output_file_);
00043     file_.open(fn.c_str());
00044     file_ << "name, lat, lon, depth, altitude, roll, pitch, yaw\n";
00045     std::string camera_topic = nh_.resolveName("camera");
00046     std::string nav_sts_topic = nh_.resolveName("nav_sts");
00047 
00048     image_transport::SubscriberFilter image_sub;
00049     message_filters::Subscriber<sensor_msgs::CameraInfo> info_sub;
00050     message_filters::Subscriber<auv_msgs::NavSts> nav_sts_sub;
00051 
00052     // Message sync
00053     boost::shared_ptr<Sync> sync;
00054     nav_sts_sub.subscribe(nh_, nav_sts_topic, 20);
00055     image_sub  .subscribe(it_, camera_topic + "/image_rect_color", 5);
00056     info_sub   .subscribe(nh_, camera_topic + "/camera_info", 5);
00057     sync.reset(new Sync(SyncPolicy(10), nav_sts_sub, image_sub, info_sub) );
00058     sync->registerCallback(boost::bind(&ImagePositions::msgsCallback, this, _1, _2, _3));
00059   }
00060 
00061   ~ImagePositions() {
00062     file_.close();
00063   }
00064 
00065   void msgsCallback(const auv_msgs::NavStsConstPtr& nav_sts_msg,
00066                     const sensor_msgs::ImageConstPtr& image_msg,
00067                     const sensor_msgs::CameraInfoConstPtr& info_msg) {
00068     cv::Mat image;
00069     cv_bridge::CvImagePtr input_bridge;
00070     try {
00071       input_bridge = cv_bridge::toCvCopy(image_msg, sensor_msgs::image_encodings::BGR8);
00072       image = input_bridge->image;
00073     }
00074     catch (cv_bridge::Exception& ex){
00075       ROS_ERROR("[ImagePositions] Failed to convert image");
00076       return;
00077     }
00078 
00079     double lat      = nav_sts_msg->position.north;
00080     double lon      = nav_sts_msg->position.east;
00081     double depth    = nav_sts_msg->position.depth;
00082     double altitude = nav_sts_msg->altitude;
00083     double roll     = nav_sts_msg->orientation.roll;
00084     double pitch    = nav_sts_msg->orientation.pitch;
00085     double yaw      = nav_sts_msg->orientation.yaw;
00086 
00087     std::stringstream ss;
00088     ss << std::setw(6) << std::setfill('0') << count_;
00089     std::string name = ss.str() + ".png";
00090 
00091     ROS_INFO_STREAM("[ImagePositions]: Saving image " << name);
00092     cv::imwrite(output_folder_ + "/" + name, image);
00093 
00094     file_ << name      << ", "
00095           << lat       << ", "
00096           << lon       << ", "
00097           << depth     << ", "
00098           << altitude  << ", "
00099           << roll      << ", "
00100           << pitch     << ", "
00101           << yaw       << "\n";
00102   }
00103 };
00104 
00105 int main(int argc, char** argv) {
00106   ros::init(argc, argv, "image_positions");
00107   ImagePositions ip();
00108   ros::spin();
00109 }


bag_tools
Author(s): Stephan Wirth , Miquel Massot
autogenerated on Sat Jun 8 2019 20:10:13