slic_superpixels.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 "slic.h"
38 #include <boost/assign.hpp>
41 
42 namespace jsk_perception
43 {
44 
46  {
47  ROS_WARN("Maybe this node does not work for large size images with segfault.");
48  nh_ = ros::NodeHandle(getNodeHandle(), "image");
50  srv_ = boost::make_shared <dynamic_reconfigure::Server<Config> > (pnh_);
51  dynamic_reconfigure::Server<Config>::CallbackType f =
52  boost::bind (
53  &SLICSuperPixels::configCallback, this, _1, _2);
54  srv_->setCallback (f);
55 
56  pnh_.param("publish_debug_images", debug_image_, false);
57 
59  pub_ = pnh_.advertise<sensor_msgs::Image>("output", 1);
60  if (debug_image_) {
61  pub_debug_ = pnh_.advertise<sensor_msgs::Image>("debug", 1);
62  pub_debug_mean_color_ = pnh_.advertise<sensor_msgs::Image>("debug/mean_color", 1);
63  pub_debug_center_grid_ = pnh_.advertise<sensor_msgs::Image>("debug/center_grid", 1);
64  }
65  image_sub_ = it_->subscribe("", 1, &SLICSuperPixels::imageCallback, this);
66 
67  ros::V_string names = boost::assign::list_of("image");
69  }
70 
71  void SLICSuperPixels::imageCallback(const sensor_msgs::Image::ConstPtr& image)
72  {
73  boost::mutex::scoped_lock lock(mutex_);
74  cv::Mat in_image = cv_bridge::toCvShare(image, image->encoding)->image;
75  cv::Mat bgr_image;
76  if (in_image.channels() == 1) {
77  // gray image
78  cv::cvtColor(in_image, bgr_image, CV_GRAY2BGR);
79  }
80  else if (image->encoding == sensor_msgs::image_encodings::RGB8) {
81  // convert to BGR8
82  cv::cvtColor(in_image, bgr_image, CV_RGB2BGR);
83  }
84  else {
85  bgr_image = in_image;
86  }
87  //cv::Mat bgr_image = cv_ptr->image;
88  cv::Mat lab_image, out_image, mean_color_image, center_grid_image;
89  // slic
90  if (debug_image_) {
91  bgr_image.copyTo(out_image);
92  bgr_image.copyTo(mean_color_image);
93  bgr_image.copyTo(center_grid_image);
94  }
95  cv::cvtColor(bgr_image, lab_image, CV_BGR2Lab);
96  int w = image->width, h = image->height;
97  double step = sqrt((w * h) / (double) number_of_super_pixels_);
98  Slic slic;
99  slic.generate_superpixels(lab_image, step, weight_);
100  slic.create_connectivity(lab_image);
101 
102  if (debug_image_) {
103  // creating debug image may occur seg fault.
104  // So, publish_debug_images was added, in order to create debug image explicitly
105  // See https://github.com/jsk-ros-pkg/jsk_recognition/pull/2181
106  slic.colour_with_cluster_means(mean_color_image);
107  slic.display_center_grid(center_grid_image, cv::Scalar(0, 0, 255));
108  slic.display_contours(out_image, cv::Vec3b(0,0,255));
109 
111  image->header,
113  out_image).toImageMsg());
115  image->header,
117  mean_color_image).toImageMsg());
119  image->header,
121  center_grid_image).toImageMsg());
122  }
123  // publish clusters
124  cv::Mat clusters;
125  cv::transpose(slic.clusters, clusters);
126  clusters = clusters + cv::Scalar(1);
128  image->header,
130  clusters).toImageMsg());
131  }
132 
133  void SLICSuperPixels::configCallback(Config &config, uint32_t level)
134  {
135  boost::mutex::scoped_lock lock(mutex_);
136  weight_ = config.weight;
137  number_of_super_pixels_ = config.number_of_super_pixels;
138  }
139 }
140 
CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
f
void create_connectivity(const cv::Mat &image)
Definition: slic.cpp:195
void publish(const boost::shared_ptr< M > &message) const
virtual void imageCallback(const sensor_msgs::Image::ConstPtr &image)
PLUGINLIB_EXPORT_CLASS(jsk_perception::SLICSuperPixels, nodelet::Nodelet)
boost::shared_ptr< image_transport::ImageTransport > it_
image_transport::Subscriber image_sub_
#define ROS_WARN(...)
ros::NodeHandle & getPrivateNodeHandle() const
std::vector< std::string > V_string
double sqrt()
bool param(const std::string &param_name, T &param_val, const T &default_val) const
bool warnNoRemap(const std::vector< std::string > names)
SLICSuperPixelsConfig Config
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::NodeHandle & getNodeHandle() const
unsigned int step
mutex_t * lock
void generate_superpixels(const cv::Mat &image, int step, int nc)
Definition: slic.cpp:123
virtual void configCallback(Config &config, uint32_t level)
cv::Mat_< int > clusters
Definition: slic.h:69
Definition: slic.h:38
void display_contours(cv::Mat &image, cv::Vec3b colour)
Definition: slic.cpp:270
void colour_with_cluster_means(cv::Mat &image)
Definition: slic.cpp:316
void display_center_grid(cv::Mat &image, cv::Scalar colour)
Definition: slic.cpp:258
const std::string TYPE_32SC1
sensor_msgs::ImagePtr toImageMsg() const
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_


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