subtract_mask_image.cpp
Go to the documentation of this file.
1 // -*- mode: C++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2017, 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 /*
36  * subtract_mask_image.cpp
37  * Author: Yuki Furuta <furushchev@jsk.imi.i.u-tokyo.ac.jp>
38  */
39 
41 #include <boost/assign.hpp>
42 #include <opencv2/opencv.hpp>
43 #include <cv_bridge/cv_bridge.h>
45 
47 
48 namespace jsk_perception
49 {
51  {
52  DiagnosticNodelet::onInit();
53  pnh_->param("approximate_sync", approximate_sync_, false);
54  pnh_->param("queue_size", queue_size_, 100);
55  pub_ = advertise<sensor_msgs::Image>(
56  *pnh_, "output", 1);
57  onInitPostProcess();
58  }
59 
61  // message_filters::Synchronizer needs to be called reset
62  // before message_filters::Subscriber is freed.
63  // Calling reset fixes the following error on shutdown of the nodelet:
64  // terminate called after throwing an instance of
65  // 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::lock_error> >'
66  // what(): boost: mutex lock failed in pthread_mutex_lock: Invalid argument
67  // Also see https://github.com/ros/ros_comm/issues/720 .
68  sync_.reset();
69  async_.reset();
70  }
71 
73  {
74  sub_src1_.subscribe(*pnh_, "input/src1", 1);
75  sub_src2_.subscribe(*pnh_, "input/src2", 1);
76  if (approximate_sync_) {
77  async_ = boost::make_shared<message_filters::Synchronizer<ApproxSyncPolicy> >(queue_size_);
78  async_->connectInput(sub_src1_, sub_src2_);
79  async_->registerCallback(boost::bind(&SubtractMaskImage::subtract, this, _1, _2));
80  }
81  else {
82  sync_ = boost::make_shared<message_filters::Synchronizer<SyncPolicy> >(queue_size_);
83  sync_->connectInput(sub_src1_, sub_src2_);
84  sync_->registerCallback(boost::bind(&SubtractMaskImage::subtract, this, _1, _2));
85  }
86  }
87 
89  {
92  }
93 
95  const sensor_msgs::Image::ConstPtr& src1_msg,
96  const sensor_msgs::Image::ConstPtr& src2_msg)
97  {
98  vital_checker_->poke();
99 
100  if (src1_msg->width != src2_msg->width || src1_msg->height != src2_msg->height)
101  {
102  NODELET_ERROR("Size of masks are different!");
103  NODELET_ERROR("input/src1 = %dx%d", src1_msg->width, src1_msg->height);
104  NODELET_ERROR("input/src2 = %dx%d", src2_msg->width, src2_msg->height);
105  return;
106  }
107 
108  cv::Mat mask1 = cv_bridge::toCvShare(src1_msg, enc::MONO8)->image;
109  cv::Mat mask2 = cv_bridge::toCvShare(src2_msg, enc::MONO8)->image;
110 
111  cv::Mat mask2_not;
112  cv::bitwise_not(mask2, mask2_not);
113 
114  cv::Mat result = cv::Mat::zeros(mask1.size(), mask1.type());
115  mask1.copyTo(result, mask2_not);
116  pub_.publish(
117  cv_bridge::CvImage(src1_msg->header, enc::MONO8, result).toImageMsg());
118  }
119 }
120 
result
def result
sensor_msgs::image_encodings
NODELET_ERROR
#define NODELET_ERROR(...)
subtract_mask_image.h
image_encodings.h
cv_bridge::CvImage::toImageMsg
sensor_msgs::ImagePtr toImageMsg() const
jsk_perception::SubtractMaskImage::onInit
virtual void onInit()
Definition: subtract_mask_image.cpp:82
jsk_perception::SubtractMaskImage::unsubscribe
virtual void unsubscribe()
Definition: subtract_mask_image.cpp:120
jsk_perception::SubtractMaskImage::sync_
boost::shared_ptr< message_filters::Synchronizer< SyncPolicy > > sync_
Definition: subtract_mask_image.h:143
jsk_perception::SubtractMaskImage::subtract
virtual void subtract(const sensor_msgs::Image::ConstPtr &src1_msg, const sensor_msgs::Image::ConstPtr &src2_msg)
Definition: subtract_mask_image.cpp:126
jsk_perception::SubtractMaskImage
Definition: subtract_mask_image.h:86
jsk_perception::SubtractMaskImage::queue_size_
int queue_size_
Definition: subtract_mask_image.h:139
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
message_filters::Subscriber::unsubscribe
void unsubscribe()
jsk_perception::SubtractMaskImage::subscribe
virtual void subscribe()
Definition: subtract_mask_image.cpp:104
class_list_macros.h
jsk_perception
Definition: add_mask_image.h:48
jsk_perception::SubtractMaskImage::sub_src2_
message_filters::Subscriber< sensor_msgs::Image > sub_src2_
Definition: subtract_mask_image.h:142
jsk_perception::SubtractMaskImage::~SubtractMaskImage
virtual ~SubtractMaskImage()
Definition: subtract_mask_image.cpp:92
message_filters::Subscriber::subscribe
void subscribe()
jsk_perception::SubtractMaskImage::async_
boost::shared_ptr< message_filters::Synchronizer< ApproxSyncPolicy > > async_
Definition: subtract_mask_image.h:144
nodelet::Nodelet
cv_bridge.h
cv_bridge::CvImage
jsk_perception::SubtractMaskImage::pub_
ros::Publisher pub_
Definition: subtract_mask_image.h:140
jsk_perception::SubtractMaskImage::sub_src1_
message_filters::Subscriber< sensor_msgs::Image > sub_src1_
Definition: subtract_mask_image.h:141
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::SubtractMaskImage::approximate_sync_
bool approximate_sync_
Definition: subtract_mask_image.h:138
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(jsk_perception::SubtractMaskImage, nodelet::Nodelet)


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