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
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_;
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
00148 CvImagePtr toCvCopy(const sensor_msgs::Image& source,
00149 const std::string& encoding = std::string());
00150
00171 CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr& source,
00172 const std::string& encoding = std::string());
00173
00198 CvImageConstPtr toCvShare(const sensor_msgs::Image& source,
00199 const boost::shared_ptr<void const>& tracked_object,
00200 const std::string& encoding = std::string());
00201
00205 CvImagePtr cvtColor(const CvImageConstPtr& source,
00206 const std::string& encoding);
00207
00213 int getCvType(const std::string& encoding);
00214
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00228 namespace ros {
00229
00230 namespace message_traits {
00231
00232 template<> struct MD5Sum<cv_bridge::CvImage>
00233 {
00234 static const char* value() { return MD5Sum<sensor_msgs::Image>::value(); }
00235 static const char* value(const cv_bridge::CvImage&) { return value(); }
00236
00237 static const uint64_t static_value1 = MD5Sum<sensor_msgs::Image>::static_value1;
00238 static const uint64_t static_value2 = MD5Sum<sensor_msgs::Image>::static_value2;
00239
00240
00241 ROS_STATIC_ASSERT(MD5Sum<sensor_msgs::Image>::static_value1 == 0x060021388200f6f0ULL);
00242 ROS_STATIC_ASSERT(MD5Sum<sensor_msgs::Image>::static_value2 == 0xf447d0fcd9c64743ULL);
00243 };
00244
00245 template<> struct DataType<cv_bridge::CvImage>
00246 {
00247 static const char* value() { return DataType<sensor_msgs::Image>::value(); }
00248 static const char* value(const cv_bridge::CvImage&) { return value(); }
00249 };
00250
00251 template<> struct Definition<cv_bridge::CvImage>
00252 {
00253 static const char* value() { return Definition<sensor_msgs::Image>::value(); }
00254 static const char* value(const cv_bridge::CvImage&) { return value(); }
00255 };
00256
00257 template<> struct HasHeader<cv_bridge::CvImage> : TrueType {};
00258
00259 }
00260
00261 namespace serialization {
00262
00263 template<> struct Serializer<cv_bridge::CvImage>
00264 {
00266
00267 template<typename Stream>
00268 inline static void write(Stream& stream, const cv_bridge::CvImage& m)
00269 {
00270 stream.next(m.header);
00271 stream.next((uint32_t)m.image.rows);
00272 stream.next((uint32_t)m.image.cols);
00273 stream.next(m.encoding);
00274 uint8_t is_bigendian = 0;
00275 stream.next(is_bigendian);
00276 stream.next((uint32_t)m.image.step);
00277 size_t data_size = m.image.step*m.image.rows;
00278 stream.next((uint32_t)data_size);
00279 if (data_size > 0)
00280 memcpy(stream.advance(data_size), m.image.data, data_size);
00281 }
00282
00283 template<typename Stream>
00284 inline static void read(Stream& stream, cv_bridge::CvImage& m)
00285 {
00286 stream.next(m.header);
00287 uint32_t height, width;
00288 stream.next(height);
00289 stream.next(width);
00290 stream.next(m.encoding);
00291 uint8_t is_bigendian;
00292 stream.next(is_bigendian);
00293 uint32_t step, data_size;
00294 stream.next(step);
00295 stream.next(data_size);
00296 int type = cv_bridge::getCvType(m.encoding);
00297
00298 cv::Mat tmp((int)height, (int)width, type, stream.advance(data_size), (size_t)step);
00299 tmp.copyTo(m.image);
00300 }
00301
00302 inline static uint32_t serializedLength(const cv_bridge::CvImage& m)
00303 {
00304 size_t data_size = m.image.step*m.image.rows;
00305 return serializationLength(m.header) + serializationLength(m.encoding) + 17 + data_size;
00306 }
00307 };
00308
00309 }
00310
00311 namespace message_operations {
00312
00313 template<> struct Printer<cv_bridge::CvImage>
00314 {
00315 template<typename Stream>
00316 static void stream(Stream& s, const std::string& indent, const cv_bridge::CvImage& m)
00317 {
00319 }
00320 };
00321
00322 }
00323
00324 }
00325
00326 namespace cv_bridge {
00327
00328 inline std::ostream& operator<<(std::ostream& s, const CvImage& m)
00329 {
00330 ros::message_operations::Printer<CvImage>::stream(s, "", m);
00331 return s;
00332 }
00333
00334 }
00335
00337
00338 #endif