image_resizer_nodelet.cpp
Go to the documentation of this file.
2 
4 {
6  raw_width_ = 0;
7  raw_height_ = 0;
8  DiagnosticNodelet::onInit();
9  initParams();
13  }
14 
17  sub_ = pnh_->subscribe("input/mask", 1, &ImageResizer::mask_region_callback, this);
18  }
19 
22  sub_.shutdown();
23  }
24 
26  reconfigure_server_ = boost::make_shared <dynamic_reconfigure::Server<ImageResizerConfig> > (*pnh_);
27  ReconfigureServer::CallbackType f
28  = boost::bind(&ImageResizer::config_cb, this, _1, _2);
29  reconfigure_server_->setCallback(f);
30  }
31 
34  period_ = ros::Duration(1.0);
35  std::string interpolation_method;
36  pnh_->param<std::string>("interpolation", interpolation_method, "LINEAR");
37  if(interpolation_method == "NEAREST"){
38  interpolation_ = cv::INTER_NEAREST;
39  }else if(interpolation_method == "LINEAR") {
40  interpolation_ = cv::INTER_LINEAR;
41  }else if(interpolation_method == "AREA") {
42  interpolation_ = cv::INTER_AREA;
43  }else if(interpolation_method == "CUBIC") {
44  interpolation_ = cv::INTER_CUBIC;
45  }else if(interpolation_method == "LANCZOS4") {
46  interpolation_ = cv::INTER_LANCZOS4;
47  }else{
48  ROS_ERROR("unknown interpolation method");
49  }
50  }
51 
52  void ImageResizer::mask_region_callback(const sensor_msgs::Image::ConstPtr& msg) {
53  boost::mutex::scoped_lock lock(mutex_);
56  cv::Mat mask = cv_ptr->image;
57  int step_x, step_y, ox, oy;
58  int pixel_x = 0;
59  int pixel_y = 0;
60  int maskwidth = mask.cols;
61  int maskheight = mask.rows;
62  int cnt = 0;
63  for (size_t j = 0; j < maskheight; j++) {
64  for (size_t i = 0; i < maskwidth; i++) {
65  if (mask.at<uchar>(j, i) != 0) {
66  cnt++;
67  }
68  }
69  }
70  int surface_per = ((double) cnt) / (maskwidth * maskheight) * 100;
71  // step_x = surface_per /10;
72  step_x = sqrt (surface_per);
73  if (step_x < 1) {
74  step_x = 1;
75  }
76  step_y = step_x;
77 
78  //raw image wo step de bunkatu pixel dasu
79  ox = step_x / 2;
80  oy = step_y / 2;
81  for (int i = ox; i < raw_width_; i += step_x) {
82  pixel_x++;
83  }
84  for (int i = oy; i < raw_height_; i += step_y) {
85  pixel_y++;
86  }
87  resize_x_ = ((double) pixel_x) / raw_width_;
88  resize_y_ = ((double) pixel_y) / raw_height_;
89  }
90 
91  void ImageResizer::config_cb ( ImageResizerConfig &config, uint32_t level) {
92  NODELET_INFO("config_cb");
93  resize_x_ = config.resize_scale_x;
94  resize_y_ = config.resize_scale_y;
95  period_ = ros::Duration(1.0/config.msg_par_second);
96  verbose_ = config.verbose;
97  NODELET_DEBUG("resize_scale_x : %f", resize_x_);
98  NODELET_DEBUG("resize_scale_y : %f", resize_y_);
99  NODELET_DEBUG("message period : %f", period_.toSec());
100  }
101 
102 
103  void ImageResizer::process(const sensor_msgs::ImageConstPtr &src_img, const sensor_msgs::CameraInfoConstPtr &src_info,
104  sensor_msgs::ImagePtr &dst_img, sensor_msgs::CameraInfo &dst_info){
105  int image_width, image_height;
106  if(use_camera_info_) {
107  image_width = src_info->width;
108  image_height = src_info->height;
109  }
110  else {
111  image_width = src_img->width;
112  image_height = src_img->height;
113  }
114 
115  int width = dst_width_ ? dst_width_ : (resize_x_ * image_width);
116  int height = dst_height_ ? dst_height_ : (resize_y_ * image_height);
117 
118  double scale_x = dst_width_ ? ((double)dst_width_)/image_width : resize_x_;
119  double scale_y = dst_height_ ? ((double)dst_height_)/image_height : resize_y_;
120 
121  cv_bridge::CvImagePtr cv_img = cv_bridge::toCvCopy(src_img);
122 
123  cv::Mat tmpmat(height, width, cv_img->image.type());
124  if (raw_width_ == 0) {
125  raw_width_ = tmpmat.cols;
126  raw_height_ = tmpmat.rows;
127  }
128  cv::resize(cv_img->image, tmpmat, cv::Size(width, height), 0, 0, interpolation_);
129  NODELET_DEBUG("mat rows:%d cols:%d", tmpmat.rows, tmpmat.cols);
130  cv_img->image = tmpmat;
131 
132  dst_img = cv_img->toImageMsg();
133  if (use_camera_info_) {
134  dst_info = *src_info;
135  dst_info.height = height;
136  dst_info.width = width;
137  dst_info.K[0] = dst_info.K[0] * scale_x; // fx
138  dst_info.K[2] = dst_info.K[2] * scale_x; // cx
139  dst_info.K[4] = dst_info.K[4] * scale_y; // fy
140  dst_info.K[5] = dst_info.K[5] * scale_y; // cy
141 
142  dst_info.P[0] = dst_info.P[0] * scale_x; // fx
143  dst_info.P[2] = dst_info.P[2] * scale_x; // cx
144  dst_info.P[3] = dst_info.P[3] * scale_x; // T
145  dst_info.P[5] = dst_info.P[5] * scale_y; // fy
146  dst_info.P[6] = dst_info.P[6] * scale_y; // cy
147  }
148  }
149 }
150 
void config_cb(ImageResizerConfig &config, uint32_t level)
f
void process(const sensor_msgs::ImageConstPtr &src_img, const sensor_msgs::CameraInfoConstPtr &src_info, sensor_msgs::ImagePtr &dst_img, sensor_msgs::CameraInfo &dst_info)
boost::shared_ptr< ReconfigureServer > reconfigure_server_
boost::shared_ptr< ros::NodeHandle > pnh_
void mask_region_callback(const sensor_msgs::Image::ConstPtr &msg)
CvImagePtr toCvCopy(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
PLUGINLIB_EXPORT_CLASS(ImageResizer, nodelet::Nodelet)
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
resized_image_transport::ImageResizer ImageResizer
#define NODELET_INFO(...)
#define ROS_ERROR(...)
#define NODELET_DEBUG(...)


resized_image_transport
Author(s): Yohei Kakiuchi
autogenerated on Mon May 3 2021 03:03:39