sparse_image_encoder.cpp
Go to the documentation of this file.
1 #include <stdint.h>
2 #include <boost/shared_ptr.hpp>
3 #include <boost/assign.hpp>
4 #include <vector>
5 #include <iostream>
6 
7 #include <nodelet/nodelet.h>
11 #include <jsk_recognition_msgs/SparseImage.h>
12 
13 
15 
16 namespace jsk_perception {
18 {
21 
22  jsk_recognition_msgs::SparseImagePtr _spr_img_ptr;
23 
28  double _rate;
30 
31  void imageCallback(const sensor_msgs::ImageConstPtr& msg){
32  do_work(msg, msg->header.frame_id);
33  }
34 
35  void do_work(const sensor_msgs::ImageConstPtr& msg, const std::string input_frame_from_msg){
36  try {
37  int channel = enc::numChannels(msg->encoding);
38 
39  // check if uint16 array needed
40  bool useData32 = false;
41  if (std::max(msg->width, msg->height) > 256){
42  useData32 = true;
43  NODELET_DEBUG("use data32 array");
44  }
45 
46  _spr_img_ptr->header.stamp = msg->header.stamp;
47  _spr_img_ptr->header.frame_id = input_frame_from_msg;
48  _spr_img_ptr->width = msg->width;
49  _spr_img_ptr->height = msg->height;
50  if (!_spr_img_ptr->data16.empty()) _spr_img_ptr->data16.clear();
51  if (!_spr_img_ptr->data32.empty()) _spr_img_ptr->data32.clear();
52 
53  // make sparse image
54  for (uint32_t y = 0; y < msg->height; ++y){
55  for (uint32_t x = 0; x < msg->width; ++x){
56  if(msg->data[x*channel+y*msg->step] > 125){
57  if (useData32) _spr_img_ptr->data32.push_back( (x << 16) | y );
58  else _spr_img_ptr->data16.push_back( (x << 8) | y );
59  }
60  }
61  }
62 
63  // print number of point if enabled
64  if (_print_point_num) {
65  int size = 0;
66  if (useData32) size = _spr_img_ptr->data32.size();
67  else size = _spr_img_ptr->data16.size();
68  NODELET_INFO("%d point encoded. encoded area per is %lf%%.", size, (100 * ((double) size)/(msg->height* msg->width)));
69  }
70 
71  // publish sparse image message
72  _spr_img_pub.publish(*_spr_img_ptr);
73  } // end of try
74  catch (...) {
75  NODELET_ERROR("making sparse image error");
76  }
77 
78  ros::Rate pubRate(_rate); // hz
79  pubRate.sleep();
80  } // end of do_work function
81 
82  void subscribe() {
83  NODELET_DEBUG("Subscribing to image topic.");
84  _img_sub = _it->subscribe("image", 3, &SparseImageEncoder::imageCallback, this);
85  ros::V_string names = boost::assign::list_of("image");
87  }
88 
89  void unsubscribe() {
90  NODELET_DEBUG("Unsubscribing from image topic.");
91  _img_sub.shutdown();
92  }
93 
95  if (_subscriber_count++ == 0) {
96  subscribe();
97  }
98  }
99 
101  _subscriber_count--;
102  if (_subscriber_count == 0) {
103  unsubscribe();
104  }
105  }
106 
107 public:
108  void onInit() {
109  _nh = getNodeHandle();
110  _ln = ros::NodeHandle("~");
111  _it.reset(new image_transport::ImageTransport(_nh));
112  _subscriber_count = 0;
113  ros::SubscriberStatusCallback connect_cb = boost::bind(&SparseImageEncoder::connectCb, this, _1);
114  ros::SubscriberStatusCallback disconnect_cb = boost::bind(&SparseImageEncoder::disconnectCb, this, _1);
115  _spr_img_pub = _nh.advertise<jsk_recognition_msgs::SparseImage>("sparse_image", 10, connect_cb, disconnect_cb);
116  _spr_img_ptr = boost::make_shared<jsk_recognition_msgs::SparseImage>();
117  _ln.param("rate", _rate, 3.0);
118  _ln.param("print_point_num", _print_point_num, false);
119  } // end of onInit function
120 }; // end of SparseImageEncoder class definition
121 } // end of jsk_perception namespace
122 
void do_work(const sensor_msgs::ImageConstPtr &msg, const std::string input_frame_from_msg)
boost::function< void(const SingleSubscriberPublisher &)> SubscriberStatusCallback
#define NODELET_ERROR(...)
boost::shared_ptr< image_transport::ImageTransport > _it
void publish(const boost::shared_ptr< M > &message) const
void imageCallback(const sensor_msgs::ImageConstPtr &msg)
jsk_recognition_msgs::SparseImagePtr _spr_img_ptr
void connectCb(const ros::SingleSubscriberPublisher &ssp)
std::vector< std::string > V_string
image_transport::Subscriber _img_sub
bool param(const std::string &param_name, T &param_val, const T &default_val) const
bool warnNoRemap(const std::vector< std::string > names)
void disconnectCb(const ros::SingleSubscriberPublisher &)
PLUGINLIB_EXPORT_CLASS(jsk_perception::SparseImageEncoder, nodelet::Nodelet)
x
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::NodeHandle & getNodeHandle() const
y
bool sleep()
#define NODELET_INFO(...)
#define NODELET_DEBUG(...)


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Mon May 3 2021 03:03:27