label_to_cluster_point_indices_nodelet.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2015, 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  *********************************************************************/
37 #include <jsk_recognition_msgs/ClusterPointIndices.h>
38 #include <cv_bridge/cv_bridge.h>
40 #include <boost/assign.hpp>
41 #include <map>
42 
43 namespace jsk_pcl_ros_utils
44 {
45 
47  {
48  DiagnosticNodelet::onInit();
49  pnh_->param("bg_label", bg_label_, 0);
50  pnh_->param("ignore_labels", ignore_labels_, std::vector<int>());
51  pub_ = advertise<jsk_recognition_msgs::ClusterPointIndices>(*pnh_, "output", 1);
52  pub_bg_ = advertise<pcl_msgs::PointIndices>(*pnh_, "output/bg_indices", 1);
54  }
55 
57  {
58  sub_ = pnh_->subscribe("input", 1,
60  this);
61  ros::V_string names = boost::assign::list_of("~input");
63  }
64 
66  {
67  sub_.shutdown();
68  }
69 
71  const sensor_msgs::Image::ConstPtr& label_msg)
72  {
73  vital_checker_->poke();
76  // collect indices for each labels
77  std::map<int, pcl_msgs::PointIndices> label_to_indices;
78  int max_label = 0;
79  for (size_t j = 0; j < label_img_ptr->image.rows; j++)
80  {
81  for (size_t i = 0; i < label_img_ptr->image.cols; i++)
82  {
83  int label = label_img_ptr->image.at<int>(j, i);
84  if (label > max_label) {
85  max_label = label;
86  }
87  label_to_indices[label].header = label_msg->header;
88  label_to_indices[label].indices.push_back(j * label_img_ptr->image.cols + i);
89  }
90  }
91  // convert 'map for label to indices' to 'cluster point indices'
92  jsk_recognition_msgs::ClusterPointIndices cluster_indices_msg;
93  pcl_msgs::PointIndices bg_indices_msg;
94  cluster_indices_msg.header = bg_indices_msg.header = label_msg->header;
95  for (size_t i=0; i <= max_label; i++)
96  {
97  pcl_msgs::PointIndices indices_msg;
98  if (i == bg_label_) {
99  if (label_to_indices.count(i) == 0) {
100  indices_msg.header = label_msg->header;
101  bg_indices_msg = indices_msg;
102  }
103  else {
104  bg_indices_msg = label_to_indices[i];
105  }
106  }
107  else if (label_to_indices.count(i) == 0 ||
108  std::find(ignore_labels_.begin(), ignore_labels_.end(), i) != ignore_labels_.end()) {
109  indices_msg.header = label_msg->header;
110  cluster_indices_msg.cluster_indices.push_back(indices_msg);
111  }
112  else {
113  cluster_indices_msg.cluster_indices.push_back(label_to_indices[i]);
114  }
115  }
116  pub_bg_.publish(bg_indices_msg);
117  pub_.publish(cluster_indices_msg);
118  }
119 
120 } // namespace jsk_pcl_ros_utils
121 
label
void publish(const boost::shared_ptr< M > &message) const
PLUGINLIB_EXPORT_CLASS(jsk_pcl_ros_utils::LabelToClusterPointIndices, nodelet::Nodelet)
std::vector< std::string > V_string
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())
jsk_topic_tools::VitalChecker::Ptr vital_checker_
virtual void convert(const sensor_msgs::Image::ConstPtr &label_msg)
const std::string TYPE_32SC1


jsk_pcl_ros_utils
Author(s): Yohei Kakiuchi
autogenerated on Mon May 3 2021 03:03:15