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


bag_tools
Author(s): Stephan Wirth , Miquel Massot
autogenerated on Fri Aug 28 2015 13:15:13