blend_images_nodelet.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2017, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 
30 #include <opencv2/core/core.hpp>
31 
32 #include <ros/ros.h>
33 #include <cv_bridge/cv_bridge.h>
34 #include <nodelet/nodelet.h>
39 #include <sensor_msgs/Image.h>
40 #include <swri_opencv_util/blend.h>
41 
42 namespace swri_image_util
43 {
44  const double DEFAULT_ALPHA_LEVEL = 0.5;
45  const cv::Scalar NO_MASK = cv::Scalar(-1, -1, -1);
46 
47  // ROS nodelet for blending the images together
49  {
50  public:
53  private:
54  // Use the message_filters to listen for the base and top image
55  // using an approximate time synchronization policy. These typedefs
56  // make creating the filter cleaner looking
58  sensor_msgs::Image, sensor_msgs::Image> ApproximateTimePolicy;
61  // Called when the nodelet starts up. This does all of the initialization
62  virtual void onInit();
63  // Callback for the two images we are blending together
64  void imageCallback(
65  const sensor_msgs::ImageConstPtr &base_image,
66  const sensor_msgs::ImageConstPtr &top_image);
67  // Alpha blending level of the two images
68  double alpha_;
69  // Color to mask, if necessary
70  cv::Scalar mask_color_;
71  // Publishes the blended image
73  // The subscribers for the base and top image
76  // Synchronization object for the two images we need for the blending
77  // process
79  };
80 
82  alpha_(DEFAULT_ALPHA_LEVEL),
83  mask_color_(NO_MASK)
84  {
85  }
86 
88  {
89  }
90 
92  {
93  // Node handles for interacting with the wider ROS system
96 
97  // User setting for the alpha value. The constructor should have
98  // already set this to the default value
99  priv.param("alpha", alpha_, alpha_);
100 
101  // Values should be in the range [0,255]
102  double mask_r;
103  double mask_g;
104  double mask_b;
105  priv.param("mask_r", mask_r, -1.0);
106  priv.param("mask_g", mask_g, -1.0);
107  priv.param("mask_b", mask_b, -1.0);
108 
109  // Only create the mask if all components are valid
110  if ((mask_r >= 0) && (mask_g >= 0) && (mask_b >= 0) &&
111  (mask_r <= 255) && (mask_g <= 255) && (mask_b <= 255))
112  {
113  mask_color_ = cv::Scalar(mask_r, mask_g, mask_b);
114  }
115  else
116  {
117  ROS_ERROR("Mask color components must be in range [0,255]");
118  ROS_ERROR(" Components were (%f, %f, %f)", mask_r, mask_g, mask_b);
119  }
120 
121  // Set up our publisher of the blended data and listen to the two input
122  // images
124  image_pub_ = it.advertise("blended_image", 1);
125  base_image_sub_.subscribe(node, "base_image", 1);
126  top_image_sub_.subscribe(node, "top_image", 1);
130  top_image_sub_));
131  // Start listening for the images
132  image_sync_->registerCallback(boost::bind(
134  this,
135  _1,
136  _2));
137  }
138 
140  const sensor_msgs::ImageConstPtr& base_image,
141  const sensor_msgs::ImageConstPtr& top_image)
142  {
143  // Convert the ROS image types to OpenCV types. Use toCvShare() here because
144  // the base and top image will not be modified, and so we do not need our
145  // own copy of the image
146  cv_bridge::CvImageConstPtr cv_base_image = cv_bridge::toCvShare(base_image);
147  // Use the base image encoding during the conversion so different types of
148  // images can be blended together
150  top_image,
151  base_image->encoding);
152 
153  // Initialize the output to the same size and type as the base image
154  cv::Mat blended = cv::Mat::zeros(
155  cv_base_image->image.rows,
156  cv_base_image->image.cols,
157  cv_base_image->image.type());
158 
159  // Blend the images together
160  if (mask_color_ != NO_MASK)
161  {
162  cv::Mat mask;
163  cv::inRange(cv_top_image->image, mask_color_, mask_color_, mask);
164 
166  cv_base_image->image,
167  mask,
168  mask_color_,
169  alpha_);
170  }
171  else
172  {
173  blended = swri_opencv_util::blend(
174  cv_top_image->image,
175  cv_base_image->image,
176  alpha_);
177  }
178 
179  // Convert the blended image to a ROS type and publish the result
180  cv_bridge::CvImagePtr cv_blended = boost::make_shared<cv_bridge::CvImage>();
181  cv_blended->image = blended;
182  cv_blended->encoding = cv_base_image->encoding;
183  cv_blended->header = cv_base_image->header;
184 
185  image_pub_.publish(cv_blended->toImageMsg());
186  }
187 }
188 
189 // Register nodelet plugin
CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
const double DEFAULT_ALPHA_LEVEL
message_filters::sync_policies::ApproximateTime< sensor_msgs::Image, sensor_msgs::Image > ApproximateTimePolicy
Publisher advertise(const std::string &base_topic, uint32_t queue_size, bool latch=false)
message_filters::Subscriber< sensor_msgs::Image > base_image_sub_
cv::Mat blend(const cv::Mat &src1, const cv::Mat &alpha1, const cv::Mat &src2, const cv::Mat &alpha2)
boost::shared_ptr< ApproximateTimeSync > image_sync_
void imageCallback(const sensor_msgs::ImageConstPtr &base_image, const sensor_msgs::ImageConstPtr &top_image)
cv::Mat overlayColor(const cv::Mat &src, const cv::Mat &mask, const cv::Scalar &color, double alpha)
const cv::Scalar NO_MASK
ros::NodeHandle & getPrivateNodeHandle() const
void publish(const sensor_msgs::Image &message) const
SWRI_NODELET_EXPORT_CLASS(swri_image_util, DrawPolygonNodelet)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
message_filters::Subscriber< sensor_msgs::Image > top_image_sub_
ros::NodeHandle & getNodeHandle() const
void subscribe(ros::NodeHandle &nh, const std::string &topic, uint32_t queue_size, const ros::TransportHints &transport_hints=ros::TransportHints(), ros::CallbackQueueInterface *callback_queue=0)
message_filters::Synchronizer< ApproximateTimePolicy > ApproximateTimeSync
#define ROS_ERROR(...)


swri_image_util
Author(s): Kris Kozak
autogenerated on Tue Apr 6 2021 02:50:39