|
| 1 | +/// Copyright (c) 2012, |
| 2 | +/// Systems, Robotics and Vision Group |
| 3 | +/// University of the Balearican Islands |
| 4 | +/// All rights reserved. |
| 5 | +/// |
| 6 | +/// Redistribution and use in source and binary forms, with or without |
| 7 | +/// modification, are permitted provided that the following conditions are met: |
| 8 | +/// * Redistributions of source code must retain the above copyright |
| 9 | +/// notice, this list of conditions and the following disclaimer. |
| 10 | +/// * Redistributions in binary form must reproduce the above copyright |
| 11 | +/// notice, this list of conditions and the following disclaimer in the |
| 12 | +/// documentation and/or other materials provided with the distribution. |
| 13 | +/// * Neither the name of Systems, Robotics and Vision Group, University of |
| 14 | +/// the Balearican Islands nor the names of its contributors may be used |
| 15 | +/// to endorse or promote products derived from this software without |
| 16 | +/// specific prior written permission. |
| 17 | +/// |
| 18 | +/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | +/// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | +/// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | +/// ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY |
| 22 | +/// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | +/// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | +/// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | +/// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | +/// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | +/// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + |
| 29 | +#include <ros/ros.h> |
| 30 | + |
| 31 | +#include <boost/format.hpp> |
| 32 | + |
| 33 | +#include <sensor_msgs/Image.h> |
| 34 | +#include <sensor_msgs/CompressedImage.h> |
| 35 | +#include <sensor_msgs/CameraInfo.h> |
| 36 | + |
| 37 | +#include <image_proc/processor.h> |
| 38 | +#include <camera_calibration_parsers/parse.h> |
| 39 | + |
| 40 | +#include <opencv2/highgui/highgui.hpp> // for cv::imwrite |
| 41 | + |
| 42 | +#include <cv_bridge/cv_bridge.h> |
| 43 | + |
| 44 | +#include "bag_tools/image_bag_processor.h" |
| 45 | +#include <iostream> |
| 46 | +/** |
| 47 | + * Saves color images from raw input |
| 48 | + */ |
| 49 | +class ImageSaver |
| 50 | +{ |
| 51 | +public: |
| 52 | + ImageSaver(const std::string& save_dir, const std::string& filetype, |
| 53 | + const std::string& prefix) : |
| 54 | + save_dir_(save_dir), filetype_(filetype), prefix_(prefix), num_saved_(0) |
| 55 | + {} |
| 56 | + |
| 57 | + void save(const sensor_msgs::ImageConstPtr &img) |
| 58 | + { |
| 59 | + |
| 60 | + cv_bridge::CvImagePtr cv_ptr; |
| 61 | + |
| 62 | + if (img->encoding == "16UC1") |
| 63 | + { |
| 64 | + try |
| 65 | + { |
| 66 | + cv_ptr = cv_bridge::toCvCopy(img, sensor_msgs::image_encodings::TYPE_16UC1); |
| 67 | + } |
| 68 | + catch (cv_bridge::Exception& e) |
| 69 | + { |
| 70 | + ROS_ERROR("cv_bridge exception: %s", e.what()); |
| 71 | + return; |
| 72 | + } |
| 73 | + cv_ptr->image.convertTo(cv_ptr->image, CV_8U, 255.0 / 4096.0); |
| 74 | + save_image(img->header.stamp.toNSec(), cv_ptr->image); |
| 75 | + } |
| 76 | + |
| 77 | + else if (img->encoding == "rgb8") |
| 78 | + { |
| 79 | + try |
| 80 | + { |
| 81 | + cv_ptr = cv_bridge::toCvCopy(img, sensor_msgs::image_encodings::TYPE_8UC3); |
| 82 | + } |
| 83 | + catch (cv_bridge::Exception& e) |
| 84 | + { |
| 85 | + ROS_ERROR("cv_bridge exception: %s", e.what()); |
| 86 | + return; |
| 87 | + } |
| 88 | + cv::cvtColor( cv_ptr->image, cv_ptr->image, cv::COLOR_BGR2RGB ); |
| 89 | + save_image(img->header.stamp.toNSec(), cv_ptr->image); |
| 90 | + } |
| 91 | + |
| 92 | + else |
| 93 | + { |
| 94 | + image_proc::ImageSet output; |
| 95 | + image_geometry::PinholeCameraModel camera_model; // empty, we don't need it here |
| 96 | + if (!processor_.process(img, camera_model, output, image_proc::Processor::COLOR)) |
| 97 | + { |
| 98 | + std::cerr << "ERROR Processing image" << std::endl; |
| 99 | + return; |
| 100 | + } |
| 101 | + save_image(img->header.stamp.toNSec(), output.color); |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | + void save_compressed(const sensor_msgs::CompressedImageConstPtr& _compressed) |
| 107 | + { |
| 108 | + cv::Mat image_uncomrpessed = cv::imdecode(cv::Mat(_compressed->data),1); |
| 109 | + save_image(_compressed->header.stamp.toNSec(), image_uncomrpessed); |
| 110 | + } |
| 111 | + |
| 112 | +private: |
| 113 | + void save_image(uint64_t _time_stamp, const cv::Mat& _image) |
| 114 | + { |
| 115 | + std::string filename = |
| 116 | + boost::str(boost::format("%s/%lu.%s") |
| 117 | + % save_dir_ |
| 118 | + // % prefix_ |
| 119 | + // % num_saved_ |
| 120 | + % _time_stamp |
| 121 | + % filetype_); |
| 122 | + if (!cv::imwrite(filename, _image)) |
| 123 | + { |
| 124 | + ROS_ERROR_STREAM("ERROR Saving " << filename); |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + ROS_DEBUG_STREAM("Saved " << filename); |
| 129 | + num_saved_++; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + image_proc::Processor processor_; |
| 134 | + std::string save_dir_; |
| 135 | + std::string filetype_; |
| 136 | + std::string prefix_; |
| 137 | + int num_saved_; |
| 138 | +}; |
| 139 | + |
| 140 | +int main(int argc, char** argv) |
| 141 | +{ |
| 142 | + if (argc < 4) |
| 143 | + { |
| 144 | + std::cout << "Usage: " << argv[0] << " OUT_DIR FILETYPE IMAGE_TOPIC BAGFILE [BAGFILE...]" << std::endl; |
| 145 | + std::cout << " Example: " << argv[0] << " /tmp jpg /stereo_down/left/image_raw bag1.bag bag2.bag" << std::endl; |
| 146 | + return 0; |
| 147 | + } |
| 148 | + |
| 149 | + std::string out_dir(argv[1]); |
| 150 | + std::string filetype(argv[2]); |
| 151 | + std::string image_topic(argv[3]); |
| 152 | + |
| 153 | + ros::Time::init(); |
| 154 | + |
| 155 | + std::string prefix("image"); |
| 156 | + |
| 157 | + ImageSaver saver(out_dir, filetype, prefix); |
| 158 | + bag_tools::ImageBagProcessor processor(image_topic); |
| 159 | + processor.registerCallback(boost::bind(&ImageSaver::save, saver, _1)); |
| 160 | + processor.registerCompressedCallback(boost::bind(&ImageSaver::save_compressed, saver, _1)); |
| 161 | + |
| 162 | + for (int i = 4; i < argc; ++i) |
| 163 | + processor.processBag(argv[i]); |
| 164 | + |
| 165 | + return 0; |
| 166 | +} |
| 167 | + |
0 commit comments