resize.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2017, Kentaro Wada.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Kentaro Wada nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 #include <mutex>
35 #include <cv_bridge/cv_bridge.h>
36 #include <dynamic_reconfigure/server.h>
37 #include <nodelet/nodelet.h>
38 #include <ros/ros.h>
39 #include <sensor_msgs/CameraInfo.h>
40 #include <sensor_msgs/Image.h>
42 
43 #include "image_proc/ResizeConfig.h"
44 
45 namespace image_proc
46 {
48 {
49 protected:
50  // ROS communication
51  std::shared_ptr<ros::NodeHandle> nh_;
52  std::shared_ptr<ros::NodeHandle> pnh_;
57 
58  std::shared_ptr<image_transport::ImageTransport> it_, private_it_;
59  std::mutex connect_mutex_;
60 
61  // Dynamic reconfigure
62  std::mutex config_mutex_;
63  typedef image_proc::ResizeConfig Config;
64  typedef dynamic_reconfigure::Server<Config> ReconfigureServer;
65  std::shared_ptr<ReconfigureServer> reconfigure_server_;
66  Config config_;
68 
69  virtual void onInit();
70  void connectCb();
71 
72  void imageCb(const sensor_msgs::ImageConstPtr& image_msg);
73  void infoCb(const sensor_msgs::CameraInfoConstPtr& info_msg);
74 
75  void configCb(Config& config, uint32_t level);
76 };
77 
79 {
80  nh_.reset(new ros::NodeHandle(getNodeHandle()));
84 
85  // Set up dynamic reconfigure
87  ReconfigureServer::CallbackType f =
88  std::bind(&ResizeNodelet::configCb, this, std::placeholders::_1, std::placeholders::_2);
89  reconfigure_server_->setCallback(f);
90 
91  // Monitor whether anyone is subscribed to the output
92  auto connect_cb = std::bind(&ResizeNodelet::connectCb, this);
93  // Make sure we don't enter connectCb() between advertising and assigning to
94  // pub_XXX
95  std::lock_guard<std::mutex> lock(connect_mutex_);
96  pub_image_ = private_it_->advertise("image", 1, connect_cb, connect_cb);
97  pub_info_ = pnh_->advertise<sensor_msgs::CameraInfo>("camera_info", 1, connect_cb, connect_cb);
98 }
99 
100 // Handles (un)subscribing when clients (un)subscribe
102 {
103  std::lock_guard<std::mutex> lock(connect_mutex_);
104  if (pub_image_.getNumSubscribers() == 0)
105  {
107  }
108  else if (!sub_image_)
109  {
110  sub_image_ = it_->subscribe("image", 1, &ResizeNodelet::imageCb, this);
111  }
112  if (pub_info_.getNumSubscribers() == 0)
113  {
115  }
116  else if (!sub_info_)
117  {
118  sub_info_ = nh_->subscribe<sensor_msgs::CameraInfo>("camera_info", 1, &ResizeNodelet::infoCb, this);
119  }
120 }
121 
122 void ResizeNodelet::configCb(Config& config, uint32_t level)
123 {
124  std::lock_guard<std::mutex> lock(config_mutex_);
125  config_ = config;
126 }
127 
128 void ResizeNodelet::infoCb(const sensor_msgs::CameraInfoConstPtr& info_msg)
129 {
130  Config config;
131  {
132  std::lock_guard<std::mutex> lock(config_mutex_);
133  config = config_;
134  }
135 
136  sensor_msgs::CameraInfoPtr dst_info_msg(new sensor_msgs::CameraInfo(*info_msg));
137 
138  double scale_y;
139  double scale_x;
140  if (config.use_scale)
141  {
142  scale_y = config.scale_height;
143  scale_x = config.scale_width;
144  dst_info_msg->height = static_cast<int>(info_msg->height * config.scale_height);
145  dst_info_msg->width = static_cast<int>(info_msg->width * config.scale_width);
146  }
147  else
148  {
149  scale_y = static_cast<double>(config.height) / info_msg->height;
150  scale_x = static_cast<double>(config.width) / info_msg->width;
151  dst_info_msg->height = config.height;
152  dst_info_msg->width = config.width;
153  }
154 
155  dst_info_msg->K[0] = dst_info_msg->K[0] * scale_x; // fx
156  dst_info_msg->K[2] = dst_info_msg->K[2] * scale_x; // cx
157  dst_info_msg->K[4] = dst_info_msg->K[4] * scale_y; // fy
158  dst_info_msg->K[5] = dst_info_msg->K[5] * scale_y; // cy
159 
160  dst_info_msg->P[0] = dst_info_msg->P[0] * scale_x; // fx
161  dst_info_msg->P[2] = dst_info_msg->P[2] * scale_x; // cx
162  dst_info_msg->P[3] = dst_info_msg->P[3] * scale_x; // T
163  dst_info_msg->P[5] = dst_info_msg->P[5] * scale_y; // fy
164  dst_info_msg->P[6] = dst_info_msg->P[6] * scale_y; // cy
165 
166  pub_info_.publish(dst_info_msg);
167 }
168 
169 void ResizeNodelet::imageCb(const sensor_msgs::ImageConstPtr& image_msg)
170 {
171  Config config;
172  {
173  std::lock_guard<std::mutex> lock(config_mutex_);
174  config = config_;
175  }
176 
178  try
179  {
180  cv_ptr = cv_bridge::toCvShare(image_msg);
181  }
182  catch (cv_bridge::Exception& e)
183  {
184  ROS_ERROR("cv_bridge exception: %s", e.what());
185  return;
186  }
187 
188  if (config.use_scale)
189  {
190  cv::resize(cv_ptr->image, scaled_cv_.image, cv::Size(0, 0), config.scale_width, config.scale_height,
191  config.interpolation);
192  }
193  else
194  {
195  int height = config.height == -1 ? image_msg->height : config.height;
196  int width = config.width == -1 ? image_msg->width : config.width;
197  cv::resize(cv_ptr->image, scaled_cv_.image, cv::Size(width, height), 0, 0, config.interpolation);
198  }
199 
200  scaled_cv_.header = image_msg->header;
201  scaled_cv_.encoding = image_msg->encoding;
203 }
204 
205 } // namespace image_proc
206 
CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
std::mutex config_mutex_
Definition: resize.cpp:62
cv_bridge::CvImage scaled_cv_
Definition: resize.cpp:67
std::mutex connect_mutex_
Definition: resize.cpp:59
void imageCb(const sensor_msgs::ImageConstPtr &image_msg)
Definition: resize.cpp:169
f
uint32_t getNumSubscribers() const
ros::NodeHandle & getNodeHandle() const
std::string encoding
std::shared_ptr< image_transport::ImageTransport > private_it_
Definition: resize.cpp:58
ros::Publisher pub_info_
Definition: resize.cpp:54
image_transport::Subscriber sub_image_
Definition: resize.cpp:55
void infoCb(const sensor_msgs::CameraInfoConstPtr &info_msg)
Definition: resize.cpp:128
ros::NodeHandle & getPrivateNodeHandle() const
std::shared_ptr< ReconfigureServer > reconfigure_server_
Definition: resize.cpp:65
virtual void onInit()
Definition: resize.cpp:78
std::shared_ptr< image_transport::ImageTransport > it_
Definition: resize.cpp:58
void configCb(Config &config, uint32_t level)
Definition: resize.cpp:122
dynamic_reconfigure::Server< Config > ReconfigureServer
Definition: resize.cpp:64
sensor_msgs::ImagePtr toImageMsg() const
void publish(const boost::shared_ptr< M > &message) const
PLUGINLIB_EXPORT_CLASS(image_proc::CropNonZeroNodelet, nodelet::Nodelet)
ros::Subscriber sub_info_
Definition: resize.cpp:56
std::shared_ptr< ros::NodeHandle > nh_
Definition: resize.cpp:51
image_proc::ResizeConfig Config
Definition: resize.cpp:63
void publish(const sensor_msgs::Image &message) const
image_transport::Publisher pub_image_
Definition: resize.cpp:53
uint32_t getNumSubscribers() const
#define ROS_ERROR(...)
std::shared_ptr< ros::NodeHandle > pnh_
Definition: resize.cpp:52
std_msgs::Header header


image_proc
Author(s): Patrick Mihelich, Kurt Konolige, Jeremy Leibs
autogenerated on Wed Dec 7 2022 03:25:23