NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
MatPublisher.hpp
1 //MatPublisher.hpp
2 // Created on: Apr 21, 2015
3 // Author: Hafez Farazi <farazi@ais.uni-bonn.de>
4 #pragma once
5 #include <ros/ros.h>
6 #include <std_msgs/String.h>
7 #include <ros/console.h>
8 #include <opencv2/opencv.hpp>
9 #include <sstream>
10 #include <boost/timer/timer.hpp>
11 #include <inttypes.h>
12 #include <time.h>
13 #include <std_msgs/Int64.h>
14 #include <string.h>
15 #include <image_transport/image_transport.h>
16 #include <image_geometry/pinhole_camera_model.h>
17 #include <tf/transform_listener.h>
18 #include <sensor_msgs/image_encodings.h>
19 #include <sensor_msgs/distortion_models.h>
20 #include <sys/ioctl.h>
21 #include <linux/videodev2.h>
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <vision_module/Tools/LineSegment.hpp>
26 #include <vision_module/Tools/General.hpp>
27 #include <cv_bridge/cv_bridge.h>
28 #include <sensor_msgs/image_encodings.h>
29 #include <config_server/parameter.h>
30 #include <std_srvs/Empty.h>
31 #include <boost/format.hpp>
32 
33 using namespace cv;
34 using namespace std;
41 {
42 private:
43  ros::NodeHandle nodeHandle;
44  cv_bridge::CvImage msg;
45  image_transport::ImageTransport it;
46  image_transport::Publisher img_pub;
47  static int number;
48  const int currentNumber;
49  bool enable;
50  bool hasListener;
51 public:
52  enum imgColorType
53  {
54  gray, hsv, bgr
55  };
56  inline MatPublisher(string name = "", bool _enable = true) :
57  it(nodeHandle), currentNumber(number++), enable(_enable),hasListener(false)
58  {
59  if (!enable)
60  return;
61  char cName[255];
62  if (name.length() < 1)
63  {
64  sprintf(cName, "/vision/debug/img%d", currentNumber);
65  }
66  else
67  {
68  sprintf(cName, "%s", name.c_str());
69  }
70  img_pub = it.advertise(cName, 1);
71  msg.header.frame_id = "/picture";
72 
73  }
74  inline void publish(Mat img, imgColorType t, ros::Time now =
75  ros::Time::now())
76  {
77  hasListener=(img_pub.getNumSubscribers() > 0);
78  if (!enable || !hasListener)
79  return;
80  Mat res;
81  switch (t)
82  {
83  case gray:
84  msg.encoding = sensor_msgs::image_encodings::MONO8;
85  res = img;
86  break;
87  case hsv:
88  msg.encoding = sensor_msgs::image_encodings::BGR8;
89  cvtColor(img, res, CV_HSV2BGR);
90  break;
91  case bgr:
92  default:
93  msg.encoding = sensor_msgs::image_encodings::BGR8;
94  res = img;
95  break;
96  }
97  msg.header.stamp = now;
98  msg.image = res;
99  img_pub.publish(msg.toImageMsg());
100  }
101 
102  inline bool thereAreListeners(bool loadNow=true) //This will be zero if we close rqt
103  {
104  if(loadNow)
105  {
106  hasListener=(img_pub.getNumSubscribers() > 0);
107  }
108  return hasListener;
109  }
110 };
111 
112 
113 
A class for publish mat objects.
Definition: MatPublisher.hpp:40