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 <opencv2/core/core.hpp>
00040 #include <stdexcept>
00041
00042 namespace cv_bridge {
00043
00044 class Exception : public std::runtime_error
00045 {
00046 public:
00047 Exception(const std::string& description) : std::runtime_error(description) {}
00048 };
00049
00050 class CvImage;
00051
00052 typedef boost::shared_ptr<CvImage> CvImagePtr;
00053 typedef boost::shared_ptr<CvImage const> CvImageConstPtr;
00054
00059 class CvImage
00060 {
00061 public:
00062 std_msgs::Header header;
00063 std::string encoding;
00064 cv::Mat image;
00065
00071 sensor_msgs::ImagePtr toImageMsg() const;
00072
00079 void toImageMsg(sensor_msgs::Image& ros_image) const;
00080
00081 typedef boost::shared_ptr<CvImage> Ptr;
00082 typedef boost::shared_ptr<CvImage const> ConstPtr;
00083
00084 protected:
00085 boost::shared_ptr<void const> tracked_object_;
00086
00088 friend
00089 CvImageConstPtr toCvShare(const sensor_msgs::Image& source,
00090 const boost::shared_ptr<void const>& tracked_object,
00091 const std::string& encoding);
00093 };
00094
00111 CvImagePtr toCvCopy(const sensor_msgs::ImageConstPtr& source,
00112 const std::string& encoding = std::string());
00113
00130 CvImagePtr toCvCopy(const sensor_msgs::Image& source,
00131 const std::string& encoding = std::string());
00132
00153 CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr& source,
00154 const std::string& encoding = std::string());
00155
00180 CvImageConstPtr toCvShare(const sensor_msgs::Image& source,
00181 const boost::shared_ptr<void const>& tracked_object,
00182 const std::string& encoding = std::string());
00183
00187 CvImagePtr cvtColor(const CvImageConstPtr& source,
00188 const std::string& encoding);
00189
00195 int getCvType(const std::string& encoding);
00196
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00210 namespace ros {
00211
00212 namespace message_traits {
00213
00214 template<> struct MD5Sum<cv_bridge::CvImage>
00215 {
00216 static const char* value() { return MD5Sum<sensor_msgs::Image>::value(); }
00217 static const char* value(const cv_bridge::CvImage&) { return value(); }
00218
00219 static const uint64_t static_value1 = MD5Sum<sensor_msgs::Image>::static_value1;
00220 static const uint64_t static_value2 = MD5Sum<sensor_msgs::Image>::static_value2;
00221
00222
00223 ROS_STATIC_ASSERT(MD5Sum<sensor_msgs::Image>::static_value1 == 0x060021388200f6f0ULL);
00224 ROS_STATIC_ASSERT(MD5Sum<sensor_msgs::Image>::static_value2 == 0xf447d0fcd9c64743ULL);
00225 };
00226
00227 template<> struct DataType<cv_bridge::CvImage>
00228 {
00229 static const char* value() { return DataType<sensor_msgs::Image>::value(); }
00230 static const char* value(const cv_bridge::CvImage&) { return value(); }
00231 };
00232
00233 template<> struct Definition<cv_bridge::CvImage>
00234 {
00235 static const char* value() { return Definition<sensor_msgs::Image>::value(); }
00236 static const char* value(const cv_bridge::CvImage&) { return value(); }
00237 };
00238
00239 template<> struct HasHeader<cv_bridge::CvImage> : TrueType {};
00240
00241 }
00242
00243 namespace serialization {
00244
00245 template<> struct Serializer<cv_bridge::CvImage>
00246 {
00248
00249 template<typename Stream>
00250 inline static void write(Stream& stream, const cv_bridge::CvImage& m)
00251 {
00252 stream.next(m.header);
00253 stream.next((uint32_t)m.image.rows);
00254 stream.next((uint32_t)m.image.cols);
00255 stream.next(m.encoding);
00256 uint8_t is_bigendian = 0;
00257 stream.next(is_bigendian);
00258 stream.next((uint32_t)m.image.step);
00259 size_t data_size = m.image.step*m.image.rows;
00260 stream.next((uint32_t)data_size);
00261 if (data_size > 0)
00262 memcpy(stream.advance(data_size), m.image.data, data_size);
00263 }
00264
00265 template<typename Stream>
00266 inline static void read(Stream& stream, cv_bridge::CvImage& m)
00267 {
00268 stream.next(m.header);
00269 uint32_t height, width;
00270 stream.next(height);
00271 stream.next(width);
00272 stream.next(m.encoding);
00273 uint8_t is_bigendian;
00274 stream.next(is_bigendian);
00275 uint32_t step, data_size;
00276 stream.next(step);
00277 stream.next(data_size);
00278 int type = cv_bridge::getCvType(m.encoding);
00279
00280 cv::Mat tmp((int)height, (int)width, type, stream.advance(data_size), (size_t)step);
00281 tmp.copyTo(m.image);
00282 }
00283
00284 inline static uint32_t serializedLength(const cv_bridge::CvImage& m)
00285 {
00286 size_t data_size = m.image.step*m.image.rows;
00287 return serializationLength(m.header) + serializationLength(m.encoding) + 17 + data_size;
00288 }
00289 };
00290
00291 }
00292
00293 namespace message_operations {
00294
00295 template<> struct Printer<cv_bridge::CvImage>
00296 {
00297 template<typename Stream>
00298 static void stream(Stream& s, const std::string& indent, const cv_bridge::CvImage& m)
00299 {
00301 }
00302 };
00303
00304 }
00305
00306 }
00307
00308 namespace cv_bridge {
00309
00310 inline std::ostream& operator<<(std::ostream& s, const CvImage& m)
00311 {
00312 ros::message_operations::Printer<CvImage>::stream(s, "", m);
00313 return s;
00314 }
00315
00316 }
00317
00319
00320 #endif