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
00030
00031
00032
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 }