background_substraction_nodelet.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2014, 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>
39 
40 namespace jsk_perception
41 {
43  {
44  DiagnosticNodelet::onInit();
45 
46  srv_ = boost::make_shared <dynamic_reconfigure::Server<Config> > (*pnh_);
47  dynamic_reconfigure::Server<Config>::CallbackType f =
48  boost::bind (
50  srv_->setCallback (f);
51 
52  image_pub_ = advertise<sensor_msgs::Image>(*pnh_, "output", 1);
53 
55  }
56 
57  void BackgroundSubstraction::configCallback(Config& config, uint32_t level)
58  {
59  boost::mutex::scoped_lock lock(mutex_);
60 #if CV_MAJOR_VERSION >= 3
61  bg_ = cv::createBackgroundSubtractorMOG2();
62 #else
63  bg_ = cv::BackgroundSubtractorMOG2();
64 #endif
65  nmixtures_ = config.nmixtures;
66  detect_shadows_ = config.detect_shadows;
67 #if CV_MAJOR_VERSION >= 3
68  bg_->setNMixtures(nmixtures_);
69 #else
70  bg_.set("nmixtures", nmixtures_);
71 #endif
72  if (detect_shadows_) {
73 #if CV_MAJOR_VERSION >= 3
74  bg_->setDetectShadows(1);
75 #else
76  bg_.set("detectShadows", 1);
77 #endif
78  }
79  else {
80 #if CV_MAJOR_VERSION >= 3
81  bg_->setDetectShadows(1);
82 #else
83  bg_.set("detectShadows", 0);
84 #endif
85  }
86  }
87 
89  {
91  sub_ = it_->subscribe("image", 1, &BackgroundSubstraction::substract, this);
92  ros::V_string names = boost::assign::list_of("image");
94  }
95 
97  {
98  sub_.shutdown();
99  }
100 
102  const sensor_msgs::Image::ConstPtr& image_msg)
103  {
104  vital_checker_->poke();
105  boost::mutex::scoped_lock lock(mutex_);
106  cv_bridge::CvImagePtr cv_ptr
108  cv::Mat image = cv_ptr->image;
109  cv::Mat fg;
110  std::vector <std::vector<cv::Point > > contours;
111 #if CV_MAJOR_VERSION >= 3
112  bg_->apply(image, fg);
113 #else
114  bg_(image, fg);
115 #endif
116  sensor_msgs::Image::Ptr diff_image
117  = cv_bridge::CvImage(image_msg->header,
119  fg).toImageMsg();
120  image_pub_.publish(diff_image);
121  }
122 }
123 
virtual void configCallback(Config &config, uint32_t level)
f
jsk_perception::BackgroundSubstractionConfig Config
void publish(const boost::shared_ptr< M > &message) const
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
std::vector< std::string > V_string
virtual void substract(const sensor_msgs::Image::ConstPtr &image_msg)
boost::shared_ptr< ros::NodeHandle > pnh_
bool warnNoRemap(const std::vector< std::string > names)
CvImagePtr toCvCopy(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
boost::shared_ptr< image_transport::ImageTransport > it_
jsk_topic_tools::VitalChecker::Ptr vital_checker_
mutex_t * lock
PLUGINLIB_EXPORT_CLASS(jsk_perception::BackgroundSubstraction, nodelet::Nodelet)
sensor_msgs::ImagePtr toImageMsg() const


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