cv_bridge.h
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2011, 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 
00035 #ifndef CV_BRIDGE_CV_BRIDGE_H
00036 #define CV_BRIDGE_CV_BRIDGE_H
00037 
00038 #include <sensor_msgs/Image.h>
00039 #include <ros/static_assert.h>
00040 #include <opencv2/core/core.hpp>
00041 #include <stdexcept>
00042 
00043 namespace cv_bridge {
00044 
00045 class Exception : public std::runtime_error
00046 {
00047 public:
00048   Exception(const std::string& description) : std::runtime_error(description) {}
00049 };
00050 
00051 class CvImage;
00052 
00053 typedef boost::shared_ptr<CvImage> CvImagePtr;
00054 typedef boost::shared_ptr<CvImage const> CvImageConstPtr;
00055 
00060 class CvImage
00061 {
00062 public:
00063   std_msgs::Header header; 
00064   std::string encoding;    
00065   cv::Mat image;           
00066 
00070   CvImage() {}
00071 
00075   CvImage(const std_msgs::Header& header, const std::string& encoding,
00076           const cv::Mat& image = cv::Mat())
00077     : header(header), encoding(encoding), image(image)
00078   {
00079   }
00080   
00086   sensor_msgs::ImagePtr toImageMsg() const;
00087 
00094   void toImageMsg(sensor_msgs::Image& ros_image) const;
00095 
00096   typedef boost::shared_ptr<CvImage> Ptr;
00097   typedef boost::shared_ptr<CvImage const> ConstPtr;
00098 
00099 protected:
00100   boost::shared_ptr<void const> tracked_object_; // for sharing ownership
00101 
00103   friend
00104   CvImageConstPtr toCvShare(const sensor_msgs::Image& source,
00105                             const boost::shared_ptr<void const>& tracked_object,
00106                             const std::string& encoding);
00108 };
00109 
00126 CvImagePtr toCvCopy(const sensor_msgs::ImageConstPtr& source,
00127                     const std::string& encoding = std::string());
00128 
00145 CvImagePtr toCvCopy(const sensor_msgs::Image& source,
00146                     const std::string& encoding = std::string());
00147 
00168 CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr& source,
00169                           const std::string& encoding = std::string());
00170 
00195 CvImageConstPtr toCvShare(const sensor_msgs::Image& source,
00196                           const boost::shared_ptr<void const>& tracked_object,
00197                           const std::string& encoding = std::string());
00198 
00203 CvImagePtr cvtColor(const CvImageConstPtr& source,
00204                     const std::string& encoding);
00205 
00211 int getCvType(const std::string& encoding);
00212 
00213 } // namespace cv_bridge
00214 
00215 
00216 
00217 // CvImage as a first class message type
00218 
00219 // The rest of this file hooks into the roscpp serialization API to make CvImage
00220 // a first-class message type you can publish and subscribe to directly.
00221 // Unfortunately this doesn't yet work with image_transport, so don't rewrite all
00222 // your callbacks to use CvImage! It might be useful for specific tasks, like
00223 // processing bag files.
00224 
00226 namespace ros {
00227 
00228 namespace message_traits {
00229 
00230 template<> struct MD5Sum<cv_bridge::CvImage>
00231 {
00232   static const char* value() { return MD5Sum<sensor_msgs::Image>::value(); }
00233   static const char* value(const cv_bridge::CvImage&) { return value(); }
00234 
00235   static const uint64_t static_value1 = MD5Sum<sensor_msgs::Image>::static_value1;
00236   static const uint64_t static_value2 = MD5Sum<sensor_msgs::Image>::static_value2;
00237   
00238   // If the definition of sensor_msgs/Image changes, we'll get a compile error here.
00239   ROS_STATIC_ASSERT(MD5Sum<sensor_msgs::Image>::static_value1 == 0x060021388200f6f0ULL);
00240   ROS_STATIC_ASSERT(MD5Sum<sensor_msgs::Image>::static_value2 == 0xf447d0fcd9c64743ULL);
00241 };
00242 
00243 template<> struct DataType<cv_bridge::CvImage>
00244 {
00245   static const char* value() { return DataType<sensor_msgs::Image>::value(); }
00246   static const char* value(const cv_bridge::CvImage&) { return value(); }
00247 };
00248 
00249 template<> struct Definition<cv_bridge::CvImage>
00250 {
00251   static const char* value() { return Definition<sensor_msgs::Image>::value(); }
00252   static const char* value(const cv_bridge::CvImage&) { return value(); }
00253 };
00254 
00255 template<> struct HasHeader<cv_bridge::CvImage> : TrueType {};
00256 
00257 } // namespace ros::message_traits
00258 
00259 namespace serialization {
00260 
00261 template<> struct Serializer<cv_bridge::CvImage>
00262 {
00264   
00265   template<typename Stream>
00266   inline static void write(Stream& stream, const cv_bridge::CvImage& m)
00267   {
00268     stream.next(m.header);
00269     stream.next((uint32_t)m.image.rows); // height
00270     stream.next((uint32_t)m.image.cols); // width
00271     stream.next(m.encoding);
00272     uint8_t is_bigendian = 0;
00273     stream.next(is_bigendian);
00274     stream.next((uint32_t)m.image.step);
00275     size_t data_size = m.image.step*m.image.rows;
00276     stream.next((uint32_t)data_size);
00277     if (data_size > 0)
00278       memcpy(stream.advance(data_size), m.image.data, data_size);
00279   }
00280 
00281   template<typename Stream>
00282   inline static void read(Stream& stream, cv_bridge::CvImage& m)
00283   {
00284     stream.next(m.header);
00285     uint32_t height, width;
00286     stream.next(height);
00287     stream.next(width);
00288     stream.next(m.encoding);
00289     uint8_t is_bigendian;
00290     stream.next(is_bigendian);
00291     uint32_t step, data_size;
00292     stream.next(step);
00293     stream.next(data_size);
00294     int type = cv_bridge::getCvType(m.encoding);
00295     // Construct matrix pointing to the stream data, then copy it to m.image
00296     cv::Mat tmp((int)height, (int)width, type, stream.advance(data_size), (size_t)step);
00297     tmp.copyTo(m.image);
00298   }
00299 
00300   inline static uint32_t serializedLength(const cv_bridge::CvImage& m)
00301   {
00302     size_t data_size = m.image.step*m.image.rows;
00303     return serializationLength(m.header) + serializationLength(m.encoding) + 17 + data_size;
00304   }
00305 };
00306 
00307 } // namespace ros::serialization
00308 
00309 namespace message_operations {
00310 
00311 template<> struct Printer<cv_bridge::CvImage>
00312 {
00313   template<typename Stream>
00314   static void stream(Stream& s, const std::string& indent, const cv_bridge::CvImage& m)
00315   {
00317   }
00318 };
00319 
00320 } // namespace ros::message_operations
00321 
00322 } // namespace ros
00323 
00324 namespace cv_bridge {
00325 
00326 inline std::ostream& operator<<(std::ostream& s, const CvImage& m)
00327 {
00328   ros::message_operations::Printer<CvImage>::stream(s, "", m);
00329   return s;
00330 }
00331 
00332 } // namespace cv_bridge
00333 
00335 
00336 #endif


cv_bridge
Author(s): Patrick Mihelich, James Bowman
autogenerated on Sat Dec 28 2013 17:53:16