overlay_image_color_on_mono.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2016, JSK Lab
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/o2r other materials provided
17  * with the distribution.
18  * * Neither the name of the JSK Lab nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
37 #include <boost/assign.hpp>
38 #include <nodelet/nodelet.h>
39 #include <opencv2/opencv.hpp>
40 #include <cv_bridge/cv_bridge.h>
44 
45 namespace jsk_perception
46 {
48  {
49  DiagnosticNodelet::onInit();
50  pnh_->param("approximate_sync", approximate_sync_, false);
51  pnh_->param("queue_size", queue_size_, 100);
52  srv_ = boost::make_shared <dynamic_reconfigure::Server<Config> > (*pnh_);
53  dynamic_reconfigure::Server<Config>::CallbackType f =
54  boost::bind (
56  srv_->setCallback (f);
57  pub_ = advertise<sensor_msgs::Image>(*pnh_, "output", 1);
59  }
60 
61  void OverlayImageColorOnMono::configCallback(Config &config, uint32_t level)
62  {
63  boost::mutex::scoped_lock lock(mutex_);
64  color_alpha_ = config.color_alpha;
65  }
66 
68  {
69  sub_color_.subscribe(*pnh_, "input/color", 1);
70  sub_mono_.subscribe(*pnh_, "input/mono", 1);
71  if (approximate_sync_) {
72  async_ = boost::make_shared<message_filters::Synchronizer<ApproximateSyncPolicy> >(queue_size_);
73  async_->connectInput(sub_color_, sub_mono_);
74  async_->registerCallback(boost::bind(&OverlayImageColorOnMono::overlay, this, _1, _2));
75  } else {
76  sync_ = boost::make_shared<message_filters::Synchronizer<SyncPolicy> >(queue_size_);
77  sync_->connectInput(sub_color_, sub_mono_);
78  sync_->registerCallback(boost::bind(&OverlayImageColorOnMono::overlay, this, _1, _2));
79  }
80  ros::V_string names = boost::assign::list_of("~input/color")("~input/mono");
82  }
83 
85  {
88  }
89 
90  void OverlayImageColorOnMono::overlay(const sensor_msgs::Image::ConstPtr& color_imgmsg,
91  const sensor_msgs::Image::ConstPtr& mono_imgmsg)
92  {
93  // validate image channels
94  if (sensor_msgs::image_encodings::numChannels(color_imgmsg->encoding) != 3) {
95  NODELET_ERROR_THROTTLE(10, "Input ~image/color message must be 3 channels color image. (RGB/BGR).");
96  return;
97  }
98  // validate image size
99  if (! ((color_imgmsg->height == mono_imgmsg->height) && (color_imgmsg->width == mono_imgmsg->width))) {
101  10, "The size of input color and mono image is different: (color: h=%d w=%d), (mono: h=%d w=%d)",
102  color_imgmsg->height, color_imgmsg->width, mono_imgmsg->height, mono_imgmsg->width);
103  return;
104  }
105 
106  boost::mutex::scoped_lock lock(mutex_);
107 
108  cv::Mat color_image = cv_bridge::toCvShare(color_imgmsg, color_imgmsg->encoding)->image;
109  cv::Mat mono_image = cv_bridge::toCvShare(mono_imgmsg, sensor_msgs::image_encodings::MONO8)->image;
110 
111  cv::Mat overlayed_image = cv::Mat::zeros(color_image.rows, color_image.cols, CV_8UC3);
112  for (size_t j = 0; j < overlayed_image.rows; j++) {
113  for (size_t i = 0; i < overlayed_image.cols; i++) {
114  cv::Vec3b color = color_image.at<cv::Vec3b>(j, i);
115  uchar mono = mono_image.at<uchar>(j, i);
116  overlayed_image.at<cv::Vec3b>(j, i) = cv::Vec3b(color[0] * color_alpha_ + mono * (1 - color_alpha_),
117  color[1] * color_alpha_ + mono * (1 - color_alpha_),
118  color[2] * color_alpha_ + mono * (1 - color_alpha_));
119  }
120  }
121  pub_.publish(cv_bridge::CvImage(color_imgmsg->header,
122  color_imgmsg->encoding,
123  overlayed_image).toImageMsg());
124  }
125 
126 } // namespace jsk_perception
127 
CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
f
PLUGINLIB_EXPORT_CLASS(jsk_perception::OverlayImageColorOnMono, nodelet::Nodelet)
void publish(const boost::shared_ptr< M > &message) const
#define NODELET_ERROR_THROTTLE(rate,...)
boost::shared_ptr< message_filters::Synchronizer< ApproximateSyncPolicy > > async_
virtual void overlay(const sensor_msgs::Image::ConstPtr &image_msg, const sensor_msgs::Image::ConstPtr &mask_msg)
message_filters::Subscriber< sensor_msgs::Image > sub_mono_
std::vector< std::string > V_string
boost::shared_ptr< ros::NodeHandle > pnh_
bool warnNoRemap(const std::vector< std::string > names)
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
mutex_t * lock
static int numChannels(const std::string &encoding)
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)
boost::shared_ptr< message_filters::Synchronizer< SyncPolicy > > sync_
sensor_msgs::ImagePtr toImageMsg() const
virtual void configCallback(Config &config, uint32_t level)
message_filters::Subscriber< sensor_msgs::Image > sub_color_


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