image_saver.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 * 
00004 *  Copyright (c) 2008, Willow Garage, Inc.
00005 *  All rights reserved.
00006 * 
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 * 
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 * 
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 #include <opencv2/highgui/highgui.hpp>
00035 
00036 #include <ros/ros.h>
00037 #include <cv_bridge/cv_bridge.h>
00038 #include <image_transport/image_transport.h>
00039 #include <camera_calibration_parsers/parse.h>
00040 #include <boost/format.hpp>
00041 
00042 #include <std_srvs/Empty.h>
00043 
00044 int g_count = 0;
00045 boost::format g_format;
00046 bool save_all_image, save_image_service;
00047 std::string encoding;
00048 
00049 bool service(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res) {
00050   save_image_service = true;
00051   return true;
00052 }
00053 
00054 void callback(const sensor_msgs::ImageConstPtr& image_msg, const sensor_msgs::CameraInfoConstPtr& info)
00055 {
00056   cv::Mat image;
00057   try
00058   {
00059     image = cv_bridge::toCvShare(image_msg, encoding)->image;
00060   } catch(cv_bridge::Exception)
00061   {
00062     ROS_ERROR("Unable to convert %s image to bgr8", image_msg->encoding.c_str());
00063     return;
00064   }
00065 
00066     if (!image.empty()) {
00067       std::string filename;
00068       try {
00069         filename = (g_format).str();
00070       } catch (...) { g_format.clear(); }
00071       try {
00072         filename = (g_format % g_count).str();
00073       } catch (...) { g_format.clear(); }
00074       try { 
00075         filename = (g_format % g_count % "jpg").str();
00076       } catch (...) { g_format.clear(); }
00077 
00078       if ( save_all_image || save_image_service ) {
00079         cv::imwrite(filename, image);
00080         ROS_INFO("Saved image %s", filename.c_str());
00081         if (info) {
00082           filename = filename.replace(filename.rfind("."), filename.length(),".ini");
00083           camera_calibration_parsers::writeCalibration(filename, "camera", *info);
00084         }
00085 
00086         g_count++;
00087         save_image_service = false;
00088       }
00089     } else {
00090       ROS_WARN("Couldn't save image, no data!");
00091     }
00092 }
00093 
00094 int main(int argc, char** argv)
00095 {
00096   ros::init(argc, argv, "image_saver", ros::init_options::AnonymousName);
00097   ros::NodeHandle nh;
00098   image_transport::ImageTransport it(nh);
00099   std::string topic = nh.resolveName("image");
00100   image_transport::CameraSubscriber sub_camera = it.subscribeCamera(topic, 1, &callback);
00101   image_transport::Subscriber sub_image = it.subscribe(topic, 1,
00102                            boost::bind(callback, _1, sensor_msgs::CameraInfoConstPtr()));
00103 
00104   ros::NodeHandle local_nh("~");
00105   std::string format_string;
00106   local_nh.param("filename_format", format_string, std::string("left%04i.%s"));
00107   local_nh.param("encoding", encoding, std::string("bgr8"));
00108   local_nh.param("save_all_image", save_all_image, true);
00109   g_format.parse(format_string);
00110   ros::ServiceServer save = local_nh.advertiseService ("save", service);
00111 
00112   ros::spin();
00113 }


image_view
Author(s): Patrick Mihelich
autogenerated on Mon Oct 6 2014 00:45:31