image_bag_processor.h
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 #include <ros/ros.h>
00030 #include <rosbag/bag.h>
00031 #include <rosbag/view.h>
00032 
00033 #include <boost/foreach.hpp>
00034 #include <boost/progress.hpp>
00035 
00036 #include <sensor_msgs/Image.h>
00037 #include <sensor_msgs/CompressedImage.h>
00038 
00039 namespace bag_tools
00040 {
00041 
00042 class ImageBagProcessor
00043 {
00044 
00045 public:
00046 
00047   typedef boost::function<void (const sensor_msgs::ImageConstPtr&)> CallbackType;
00048   typedef boost::function<void (const sensor_msgs::CompressedImageConstPtr&)> CallbackCompressedType;
00049 
00050   ImageBagProcessor(const std::string& image_topic) :
00051     image_topic_(image_topic)
00052   {
00053     ros::Time::init();
00054   }
00055 
00056   void registerCallback(const CallbackType& callback)
00057   {
00058     callback_ = callback;
00059   }
00060 
00061   void registerCompressedCallback(const CallbackCompressedType& callback)
00062   {
00063     callback_compressed_ = callback;
00064   }
00065 
00070   void processBag(const std::string &filename)
00071   {
00072     // Image topics to load
00073     std::vector<std::string> topics;
00074     topics.push_back(image_topic_);
00075 
00076     std::cout << "Starting processing " << filename << ", ";
00077     rosbag::Bag bag;
00078     bag.open(filename, rosbag::bagmode::Read);
00079     rosbag::View view(bag, rosbag::TopicQuery(topics));
00080 
00081     int num_messages = view.size();
00082     std::cout << num_messages << " messages to process." << std::endl;
00083 
00084     // Load all messages
00085     boost::progress_display show_progress(num_messages);
00086     BOOST_FOREACH(rosbag::MessageInstance const m, view)
00087     {
00088       if (m.getTopic() == image_topic_)
00089       {
00090         sensor_msgs::Image::ConstPtr img_msg = m.instantiate<sensor_msgs::Image>();
00091         if (img_msg != NULL && callback_.empty() == false)
00092         {
00093           callback_(img_msg);
00094         }else
00095         {
00096           sensor_msgs::CompressedImageConstPtr compressed = m.instantiate<sensor_msgs::CompressedImage>();
00097           if(compressed != NULL && callback_compressed_.empty() == false)
00098           {
00099               callback_compressed_(compressed);
00100           }
00101         }
00102       }
00103       ++show_progress;
00104     }
00105     bag.close();
00106     std::cout << "Finished processing " << filename << std::endl;
00107   }
00108 
00109 private:
00110 
00111   std::string image_topic_;
00112   CallbackType callback_;
00113   CallbackCompressedType callback_compressed_;
00114 
00115 };
00116 
00117 } // end of namespace
00118 


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