extract_images.cpp
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 
00031 #include <boost/format.hpp>
00032 
00033 #include <sensor_msgs/Image.h>
00034 #include <sensor_msgs/CompressedImage.h>
00035 #include <sensor_msgs/CameraInfo.h>
00036 
00037 #include <image_proc/processor.h>
00038 #include <camera_calibration_parsers/parse.h>
00039 
00040 #include <opencv2/highgui/highgui.hpp> // for cv::imwrite
00041 
00042 #include "bag_tools/image_bag_processor.h"
00043 #include <iostream>
00047 class ImageSaver
00048 {
00049 public:
00050   ImageSaver(const std::string& save_dir, const std::string& filetype,
00051       const std::string& prefix) :
00052     save_dir_(save_dir), filetype_(filetype), prefix_(prefix), num_saved_(0)
00053   {}
00054 
00055   void save(const sensor_msgs::ImageConstPtr &img)
00056   {
00057     image_proc::ImageSet output;
00058     image_geometry::PinholeCameraModel camera_model; // empty, we don't need it here
00059     if (!processor_.process(img, camera_model, output, image_proc::Processor::COLOR))
00060     {
00061       std::cerr << "ERROR Processing image" << std::endl;
00062       return;
00063     }
00064     save_image(img->header.stamp.toNSec(), output.color);
00065   }
00066   void save_compressed(const sensor_msgs::CompressedImageConstPtr& _compressed)
00067   {
00068     cv::Mat image_uncomrpessed = cv::imdecode(cv::Mat(_compressed->data),1);
00069     save_image(_compressed->header.stamp.toNSec(), image_uncomrpessed);
00070   }
00071 
00072 private:
00073   void save_image(uint64_t _time_stamp, const cv::Mat& _image)
00074   {
00075       std::string filename =
00076         boost::str(boost::format("%s/%s%lu.%s")
00077             % save_dir_
00078             % prefix_
00079             % _time_stamp
00080             % filetype_);
00081       if (!cv::imwrite(filename, _image))
00082       {
00083         ROS_ERROR_STREAM("ERROR Saving " << filename);
00084       }
00085       else
00086       {
00087         ROS_DEBUG_STREAM("Saved " << filename);
00088         num_saved_++;
00089       }
00090   }
00091 
00092   image_proc::Processor processor_;
00093   std::string save_dir_;
00094   std::string filetype_;
00095   std::string prefix_;
00096   int num_saved_;
00097 };
00098 
00099 int main(int argc, char** argv)
00100 {
00101   if (argc < 4)
00102   {
00103     std::cout << "Usage: " << argv[0] << " OUT_DIR FILETYPE IMAGE_TOPIC BAGFILE [BAGFILE...]" << std::endl;
00104     std::cout << "  Example: " << argv[0] << " /tmp jpg /stereo_down/left/image_raw bag1.bag bag2.bag" << std::endl;
00105     return 0;
00106   }
00107 
00108   std::string out_dir(argv[1]);
00109   std::string filetype(argv[2]);
00110   std::string image_topic(argv[3]);
00111 
00112   ros::Time::init();
00113 
00114   std::string prefix("image");
00115 
00116   ImageSaver saver(out_dir, filetype, prefix);
00117   bag_tools::ImageBagProcessor processor(image_topic);
00118   processor.registerCallback(boost::bind(&ImageSaver::save, saver, _1));
00119   processor.registerCompressedCallback(boost::bind(&ImageSaver::save_compressed, saver, _1));
00120 
00121   for (int i = 4; i < argc; ++i)
00122     processor.processBag(argv[i]);
00123 
00124   return 0;
00125 }
00126 


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