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/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 "slic.h"
38 #include <boost/assign.hpp>
39 #include <jsk_topic_tools/log_utils.h>
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");
68  jsk_topic_tools::warnNoRemap(names);
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 
random_forest_client_sample.w
w
Definition: random_forest_client_sample.py:106
Slic::colour_with_cluster_means
void colour_with_cluster_means(cv::Mat &image)
Definition: slic.cpp:316
nodelet::Nodelet::getNodeHandle
ros::NodeHandle & getNodeHandle() const
image_encodings.h
Slic::create_connectivity
void create_connectivity(const cv::Mat &image)
Definition: slic.cpp:195
image_transport::ImageTransport
cv_bridge::CvImage::toImageMsg
sensor_msgs::ImagePtr toImageMsg() const
jsk_perception::SLICSuperPixels::configCallback
virtual void configCallback(Config &config, uint32_t level)
Definition: slic_superpixels.cpp:165
jsk_perception::SLICSuperPixels::imageCallback
virtual void imageCallback(const sensor_msgs::Image::ConstPtr &image)
Definition: slic_superpixels.cpp:103
jsk_perception::SLICSuperPixels::number_of_super_pixels_
int number_of_super_pixels_
Definition: slic_superpixels.h:150
step
unsigned int step
sensor_msgs::image_encodings::RGB8
const std::string RGB8
random_forest_client_sample.h
h
Definition: random_forest_client_sample.py:106
jsk_perception::SLICSuperPixels::pub_debug_mean_color_
ros::Publisher pub_debug_mean_color_
Definition: slic_superpixels.h:142
nodelet::Nodelet::getPrivateNodeHandle
ros::NodeHandle & getPrivateNodeHandle() const
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(jsk_perception::SLICSuperPixels, nodelet::Nodelet)
jsk_perception::SLICSuperPixels::pub_debug_center_grid_
ros::Publisher pub_debug_center_grid_
Definition: slic_superpixels.h:143
class_list_macros.h
jsk_perception
Definition: add_mask_image.h:48
jsk_perception::SLICSuperPixels::weight_
int weight_
Definition: slic_superpixels.h:151
Slic::generate_superpixels
void generate_superpixels(const cv::Mat &image, int step, int nc)
Definition: slic.cpp:123
jsk_perception::SLICSuperPixels::pub_debug_
ros::Publisher pub_debug_
Definition: slic_superpixels.h:141
jsk_perception::SLICSuperPixels::debug_image_
bool debug_image_
Definition: slic_superpixels.h:152
slic.h
jsk_perception::SLICSuperPixels::srv_
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
Definition: slic_superpixels.h:145
Slic::display_contours
void display_contours(cv::Mat &image, cv::Vec3b colour)
Definition: slic.cpp:270
ROS_WARN
#define ROS_WARN(...)
lock
mutex_t * lock
jsk_perception::SLICSuperPixels::onInit
virtual void onInit()
Definition: slic_superpixels.cpp:77
jsk_perception::SLICSuperPixels
Definition: slic_superpixels.h:89
jsk_perception::SLICSuperPixels::mutex_
boost::mutex mutex_
Definition: slic_superpixels.h:139
f
f
Slic::clusters
cv::Mat_< int > clusters
Definition: slic.h:69
nodelet::Nodelet
sqrt
double sqrt()
Slic
Definition: slic.h:38
sensor_msgs::image_encodings::TYPE_32SC1
const std::string TYPE_32SC1
cv_bridge::CvImage
jsk_perception::SLICSuperPixels::pnh_
ros::NodeHandle pnh_
Definition: slic_superpixels.h:137
jsk_perception::SLICSuperPixels::it_
boost::shared_ptr< image_transport::ImageTransport > it_
Definition: slic_superpixels.h:138
jsk_perception::SLICSuperPixels::nh_
ros::NodeHandle nh_
Definition: slic_superpixels.h:137
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
sensor_msgs::image_encodings::BGR8
const std::string BGR8
ros::V_string
std::vector< std::string > V_string
cv_bridge::toCvShare
CvImageConstPtr toCvShare(const sensor_msgs::Image &source, const boost::shared_ptr< void const > &tracked_object, const std::string &encoding=std::string())
slic_superpixels.h
Slic::display_center_grid
void display_center_grid(cv::Mat &image, cv::Scalar colour)
Definition: slic.cpp:258
config
config
jsk_perception::SLICSuperPixels::pub_
ros::Publisher pub_
Definition: slic_superpixels.h:144
ros::NodeHandle
jsk_perception::SLICSuperPixels::image_sub_
image_transport::Subscriber image_sub_
Definition: slic_superpixels.h:140


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