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/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>
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 
54  onInitPostProcess();
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  {
90  it_.reset(new image_transport::ImageTransport(*pnh_));
91  sub_ = it_->subscribe("image", 1, &BackgroundSubstraction::substract, this);
92  ros::V_string names = boost::assign::list_of("image");
93  jsk_topic_tools::warnNoRemap(names);
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 
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(jsk_perception::BackgroundSubstraction, nodelet::Nodelet)
image_transport::ImageTransport
boost::shared_ptr< CvImage >
cv_bridge::CvImage::toImageMsg
sensor_msgs::ImagePtr toImageMsg() const
background_substraction.h
jsk_perception::BackgroundSubstraction::image_pub_
ros::Publisher image_pub_
Definition: background_substraction.h:139
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
jsk_perception::BackgroundSubstraction::sub_
image_transport::Subscriber sub_
Definition: background_substraction.h:140
jsk_perception::BackgroundSubstraction::srv_
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
Definition: background_substraction.h:142
class_list_macros.h
jsk_perception
Definition: add_mask_image.h:48
jsk_perception::BackgroundSubstraction::it_
boost::shared_ptr< image_transport::ImageTransport > it_
Definition: background_substraction.h:141
jsk_perception::BackgroundSubstraction
Definition: background_substraction.h:89
cv_bridge::toCvCopy
CvImagePtr toCvCopy(const sensor_msgs::CompressedImage &source, const std::string &encoding=std::string())
lock
mutex_t * lock
jsk_perception::BackgroundSubstraction::configCallback
virtual void configCallback(Config &config, uint32_t level)
Definition: background_substraction_nodelet.cpp:89
jsk_perception::BackgroundSubstraction::unsubscribe
virtual void unsubscribe()
Definition: background_substraction_nodelet.cpp:128
f
f
jsk_perception::BackgroundSubstraction::substract
virtual void substract(const sensor_msgs::Image::ConstPtr &image_msg)
Definition: background_substraction_nodelet.cpp:133
nodelet::Nodelet
sensor_msgs::image_encodings::MONO8
const std::string MONO8
cv_bridge::CvImage
jsk_perception::BackgroundSubstraction::nmixtures_
int nmixtures_
Definition: background_substraction.h:155
jsk_perception::BackgroundSubstraction::mutex_
boost::mutex mutex_
Definition: background_substraction.h:143
jsk_perception::BackgroundSubstraction::onInit
virtual void onInit()
Definition: background_substraction_nodelet.cpp:74
jsk_perception::BackgroundSubstraction::detect_shadows_
bool detect_shadows_
Definition: background_substraction.h:154
jsk_perception::BackgroundSubstraction::subscribe
virtual void subscribe()
Definition: background_substraction_nodelet.cpp:120
sensor_msgs::image_encodings::BGR8
const std::string BGR8
ros::V_string
std::vector< std::string > V_string
config
config
jsk_perception::BackgroundSubstraction::bg_
cv::BackgroundSubtractorMOG2 bg_
Definition: background_substraction.h:152
image_transport::Subscriber::shutdown
void shutdown()


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