unapply_mask_image.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2015, 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/or 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 <jsk_topic_tools/log_utils.h>
40 #include <opencv2/opencv.hpp>
41 #include <cv_bridge/cv_bridge.h>
43 
44 namespace jsk_perception
45 {
47  {
48  DiagnosticNodelet::onInit();
49  pnh_->param("approximate_sync", approximate_sync_, false);
50  pub_image_ = advertise<sensor_msgs::Image>(
51  *pnh_, "output", 1);
52  onInitPostProcess();
53  }
54 
56  // message_filters::Synchronizer needs to be called reset
57  // before message_filters::Subscriber is freed.
58  // Calling reset fixes the following error on shutdown of the nodelet:
59  // terminate called after throwing an instance of
60  // 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::lock_error> >'
61  // what(): boost: mutex lock failed in pthread_mutex_lock: Invalid argument
62  // Also see https://github.com/ros/ros_comm/issues/720 .
63  sync_.reset();
64  async_.reset();
65  }
66 
68  {
69  sub_image_.subscribe(*pnh_, "input", 1);
70  sub_mask_.subscribe(*pnh_, "input/mask", 1);
71  if (approximate_sync_) {
72  async_ = boost::make_shared<message_filters::Synchronizer<ApproximateSyncPolicy> >(100);
73  async_->connectInput(sub_image_, sub_mask_);
74  async_->registerCallback(boost::bind(&UnapplyMaskImage::apply, this, _1, _2));
75  }
76  else {
77  sync_ = boost::make_shared<message_filters::Synchronizer<SyncPolicy> >(100);
78  sync_->connectInput(sub_image_, sub_mask_);
79  sync_->registerCallback(boost::bind(&UnapplyMaskImage::apply, this, _1, _2));
80  }
81  ros::V_string names = boost::assign::list_of("~input")("~input/mask");
82  jsk_topic_tools::warnNoRemap(names);
83  }
84 
86  {
89  }
90 
92  const sensor_msgs::Image::ConstPtr& image_msg,
93  const sensor_msgs::Image::ConstPtr& mask_msg)
94  {
95  vital_checker_->poke();
96  cv::Mat image = cv_bridge::toCvShare(image_msg,
97  image_msg->encoding)->image;
98  cv::Mat mask = cv_bridge::toCvShare(mask_msg,
99  mask_msg->encoding)->image;
100  cv::Mat output;
101  bool single_channel = false;
102  if (image_msg->encoding == sensor_msgs::image_encodings::BGR8 ||
103  image_msg->encoding == sensor_msgs::image_encodings::RGB8) {
104  single_channel = false;
105  }
106  else {
107  single_channel = true;
108  }
109  if (single_channel) {
110  output = cv::Mat::zeros(mask.rows, mask.cols, CV_8UC1);
111  }
112  else {
113  output = cv::Mat::zeros(mask.rows, mask.cols, CV_8UC3);
114  }
115 
116  cv::Rect region = jsk_recognition_utils::boundingRectOfMaskImage(mask);
117  for (int j = 0; j < image.rows; j++) {
118  for (int i = 0; i < image.cols; i++) {
119  if (single_channel) {
120  output.at<uchar>(j + region.y, i + region.x)
121  = image.at<uchar>(j, i);
122  }
123  else {
124  output.at<cv::Vec3b>(j + region.y, i + region.x)
125  = image.at<cv::Vec3b>(j, i);
126  }
127  }
128  }
130  image_msg->header,
131  image_msg->encoding,
132  output).toImageMsg());
133  }
134 }
135 
jsk_perception::UnapplyMaskImage::~UnapplyMaskImage
virtual ~UnapplyMaskImage()
Definition: unapply_mask_image.cpp:87
jsk_perception::UnapplyMaskImage::approximate_sync_
bool approximate_sync_
Definition: unapply_mask_image.h:70
jsk_perception::UnapplyMaskImage::subscribe
virtual void subscribe()
Definition: unapply_mask_image.cpp:99
jsk_recognition_utils::boundingRectOfMaskImage
cv::Rect boundingRectOfMaskImage(const cv::Mat &image)
image_encodings.h
jsk_perception::UnapplyMaskImage::async_
boost::shared_ptr< message_filters::Synchronizer< ApproximateSyncPolicy > > async_
Definition: unapply_mask_image.h:72
i
int i
cv_bridge::CvImage::toImageMsg
sensor_msgs::ImagePtr toImageMsg() const
jsk_perception::UnapplyMaskImage::sub_mask_
message_filters::Subscriber< sensor_msgs::Image > sub_mask_
Definition: unapply_mask_image.h:74
sensor_msgs::image_encodings::RGB8
const std::string RGB8
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
message_filters::Subscriber::unsubscribe
void unsubscribe()
class_list_macros.h
jsk_perception
Definition: add_mask_image.h:48
jsk_perception::UnapplyMaskImage::sub_image_
message_filters::Subscriber< sensor_msgs::Image > sub_image_
Definition: unapply_mask_image.h:73
jsk_perception::UnapplyMaskImage::onInit
virtual void onInit()
Definition: unapply_mask_image.cpp:78
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(jsk_perception::UnapplyMaskImage, nodelet::Nodelet)
unapply_mask_image.h
jsk_perception::UnapplyMaskImage::unsubscribe
virtual void unsubscribe()
Definition: unapply_mask_image.cpp:117
jsk_perception::UnapplyMaskImage
Definition: unapply_mask_image.h:49
message_filters::Subscriber::subscribe
void subscribe()
nodelet::Nodelet
cv_bridge.h
cv_bridge::CvImage
jsk_perception::UnapplyMaskImage::pub_image_
ros::Publisher pub_image_
Definition: unapply_mask_image.h:75
cv_utils.h
sensor_msgs::image_encodings::BGR8
const std::string BGR8
node_scripts.craft.craft.output
output
Definition: craft.py:107
ros::V_string
std::vector< std::string > V_string
cv_bridge::toCvShare
CvImageConstPtr toCvShare(const sensor_msgs::Image &source, const boost::shared_ptr< void const > &tracked_object, const std::string &encoding=std::string())
jsk_perception::UnapplyMaskImage::apply
virtual void apply(const sensor_msgs::Image::ConstPtr &image_msg, const sensor_msgs::Image::ConstPtr &mask_msg)
Definition: unapply_mask_image.cpp:123
jsk_perception::UnapplyMaskImage::sync_
boost::shared_ptr< message_filters::Synchronizer< SyncPolicy > > sync_
Definition: unapply_mask_image.h:71


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Fri May 16 2025 03:11:17