grabcut_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 
36 #include "jsk_perception/grabcut.h"
37 #include <cv_bridge/cv_bridge.h>
39 #include <opencv2/opencv.hpp>
40 
41 namespace jsk_perception
42 {
44  {
45  DiagnosticNodelet::onInit();
46  srv_ = boost::make_shared <dynamic_reconfigure::Server<Config> > (*pnh_);
47  dynamic_reconfigure::Server<Config>::CallbackType f =
48  boost::bind (
49  &GrabCut::configCallback, this, _1, _2);
50  srv_->setCallback (f);
52  = advertise<sensor_msgs::Image>(*pnh_, "output/foreground", 1);
54  = advertise<sensor_msgs::Image>(*pnh_, "output/background", 1);
56  = advertise<sensor_msgs::Image>(*pnh_, "output/foreground_mask", 1);
58  = advertise<sensor_msgs::Image>(*pnh_, "output/background_mask", 1);
60  }
61 
63  {
64  sub_image_.subscribe(*pnh_, "input", 1);
65  sub_foreground_.subscribe(*pnh_, "input/foreground", 1);
66  sub_background_.subscribe(*pnh_, "input/background", 1);
67  sync_ = boost::make_shared<message_filters::Synchronizer<SyncPolicy> >(100);
69  sync_->registerCallback(boost::bind(&GrabCut::segment,
70  this, _1, _2, _3));
71  }
72 
74  {
78  }
79 
82  {
83  // foreground and background is not continuous,
84  // so just say OK
85  stat.summary(diagnostic_msgs::DiagnosticStatus::OK,
86  "GrabCut running");
87  }
88 
90  const sensor_msgs::Image::ConstPtr& image_msg,
91  const sensor_msgs::Image::ConstPtr& foreground_msg,
92  const sensor_msgs::Image::ConstPtr& background_msg)
93  {
94  boost::mutex::scoped_lock lock(mutex_);
95  cv::Mat input;
96  cv::Mat in_image = cv_bridge::toCvShare(image_msg,
97  image_msg->encoding)->image;
98  if (in_image.channels() == 3) {
99  input = in_image;
100  }
101  else if (in_image.channels() == 1) {
102  input = cv::Mat::zeros(in_image.rows, in_image.cols, CV_8UC3);
103  for (size_t j = 0; j < in_image.rows; ++j) {
104  for (size_t i = 0; i < in_image.cols; ++i) {
105  input.at<cv::Vec3b>(j, i) = cv::Vec3b(in_image.at<uchar>(j, i),
106  in_image.at<uchar>(j, i),
107  in_image.at<uchar>(j, i));
108  }
109  }
110  }
111  cv::Mat foreground = cv_bridge::toCvCopy(
112  foreground_msg, sensor_msgs::image_encodings::MONO8)->image;
113  cv::Mat background = cv_bridge::toCvCopy(
114  background_msg, sensor_msgs::image_encodings::MONO8)->image;
115  if (!(input.cols == foreground.cols &&
116  input.rows == foreground.rows &&
117  background.cols == foreground.cols &&
118  background.rows == foreground.rows)) {
119  NODELET_WARN("size of image is not corretct");
120  return;
121  }
122  cv::Mat mask = cv::Mat::zeros(input.size(), CV_8UC1);
123  mask.setTo(cv::Scalar::all(cv::GC_PR_BGD));
124  for (size_t j = 0; j < input.rows; j++) {
125  for (size_t i = 0; i < input.cols; i++) {
126  if (foreground.at<uchar>(j, i) == 255) {
128  mask.at<uchar>(j, i) = cv::GC_PR_FGD;
129  }
130  else {
131  mask.at<uchar>(j, i) = cv::GC_FGD;
132  }
133  }
134  if (background.at<uchar>(j, i) == 255) {
136  mask.at<uchar>(j, i) = cv::GC_PR_BGD;
137  }
138  else {
139  mask.at<uchar>(j, i) = cv::GC_BGD;
140  }
141  }
142  }
143  }
144  cv::Rect roi;
145  cv::Mat bgd_model;
146  cv::Mat fgd_model;
147 
148  cv::grabCut(input, mask, roi, bgd_model, fgd_model, 5, cv::GC_INIT_WITH_MASK);
149  cv::Mat bgd, fgd, bgd_mask, fgd_mask;
150  // model -> mask
151  //cv::compare(mask, cv::GC_BGD, bgd_mask, cv::CMP_EQ);
152  bgd_mask = (mask == cv::GC_BGD) | (mask == cv::GC_PR_BGD);
153  //cv::compare(mask, cv::GC_FGD, fgd_mask, cv::CMP_EQ);
154  fgd_mask = (mask == cv::GC_FGD) | (mask == cv::GC_PR_FGD);
155  // mask -> image
156  input.copyTo(bgd, bgd_mask);
157  input.copyTo(fgd, fgd_mask);
158  cv_bridge::CvImage fg_bridge(
159  image_msg->header, sensor_msgs::image_encodings::BGR8, fgd);
160  cv_bridge::CvImage bg_bridge(
161  image_msg->header, sensor_msgs::image_encodings::BGR8, bgd);
162  cv_bridge::CvImage fg_mask_bridge(
163  image_msg->header, sensor_msgs::image_encodings::MONO8, fgd_mask);
164  cv_bridge::CvImage bg_mask_bridge(
165  image_msg->header, sensor_msgs::image_encodings::MONO8, bgd_mask);
166  pub_foreground_.publish(fg_bridge.toImageMsg());
167  pub_background_.publish(bg_bridge.toImageMsg());
168  pub_foreground_mask_.publish(fg_mask_bridge.toImageMsg());
169  pub_background_mask_.publish(bg_mask_bridge.toImageMsg());
170  }
171 
172  void GrabCut::configCallback(Config &config, uint32_t level)
173  {
174  boost::mutex::scoped_lock lock(mutex_);
175  use_probable_pixel_seed_ = (config.seed_pixel_policy == 1);
176  }
177 
178 }
179 
message_filters::Subscriber< sensor_msgs::Image > sub_foreground_
Definition: grabcut.h:87
CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
f
#define NODELET_WARN(...)
void publish(const boost::shared_ptr< M > &message) const
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
Definition: grabcut.h:89
void summary(unsigned char lvl, const std::string s)
GrabCutConfig Config
Definition: grabcut.h:55
virtual void updateDiagnostic(diagnostic_updater::DiagnosticStatusWrapper &stat)
bool use_probable_pixel_seed_
Definition: grabcut.h:95
message_filters::Subscriber< sensor_msgs::Image > sub_background_
Definition: grabcut.h:88
input
virtual void configCallback(Config &config, uint32_t level)
boost::shared_ptr< message_filters::Synchronizer< SyncPolicy > > sync_
Definition: grabcut.h:85
ros::Publisher pub_background_
Definition: grabcut.h:82
boost::shared_ptr< ros::NodeHandle > pnh_
CvImagePtr toCvCopy(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
virtual void segment(const sensor_msgs::Image::ConstPtr &image_msg, const sensor_msgs::Image::ConstPtr &foreground_msg, const sensor_msgs::Image::ConstPtr &background_msg)
ros::Publisher pub_foreground_
Definition: grabcut.h:81
mutex_t * lock
PLUGINLIB_EXPORT_CLASS(jsk_perception::GrabCut, nodelet::Nodelet)
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)
message_filters::Subscriber< sensor_msgs::Image > sub_image_
Definition: grabcut.h:86
ros::Publisher pub_background_mask_
Definition: grabcut.h:84
ros::Publisher pub_foreground_mask_
Definition: grabcut.h:83
boost::mutex mutex_
Definition: grabcut.h:90
sensor_msgs::ImagePtr toImageMsg() const


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