label_to_cluster_point_indices_nodelet.cpp
Go to the documentation of this file.
00001 // -*- mode: c++ -*-
00002 /*********************************************************************
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2015, JSK Lab
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/o2r other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the JSK Lab nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *********************************************************************/
00035 #include "jsk_pcl_ros_utils/label_to_cluster_point_indices.h"
00036 #include "jsk_topic_tools/log_utils.h"
00037 #include <jsk_recognition_msgs/ClusterPointIndices.h>
00038 #include <cv_bridge/cv_bridge.h>
00039 #include <sensor_msgs/image_encodings.h>
00040 #include <boost/assign.hpp>
00041 #include <map>
00042 
00043 namespace jsk_pcl_ros_utils
00044 {
00045 
00046   void LabelToClusterPointIndices::onInit()
00047   {
00048     DiagnosticNodelet::onInit();
00049     pnh_->param("bg_label", bg_label_, 0);
00050     pnh_->param("ignore_labels", ignore_labels_, std::vector<int>());
00051     pub_ = advertise<jsk_recognition_msgs::ClusterPointIndices>(*pnh_, "output", 1);
00052     pub_bg_ = advertise<pcl_msgs::PointIndices>(*pnh_, "output/bg_indices", 1);
00053     onInitPostProcess();
00054   }
00055 
00056   void LabelToClusterPointIndices::subscribe()
00057   {
00058     sub_ = pnh_->subscribe("input", 1,
00059                            &LabelToClusterPointIndices::convert,
00060                            this);
00061     ros::V_string names = boost::assign::list_of("~input");
00062     jsk_topic_tools::warnNoRemap(names);
00063   }
00064 
00065   void LabelToClusterPointIndices::unsubscribe()
00066   {
00067     sub_.shutdown();
00068   }
00069 
00070   void LabelToClusterPointIndices::convert(
00071     const sensor_msgs::Image::ConstPtr& label_msg)
00072   {
00073     vital_checker_->poke();
00074     cv_bridge::CvImagePtr label_img_ptr = cv_bridge::toCvCopy(
00075       label_msg, sensor_msgs::image_encodings::TYPE_32SC1);
00076     // collect indices for each labels
00077     std::map<int, pcl_msgs::PointIndices> label_to_indices;
00078     int max_label = 0;
00079     for (size_t j = 0; j < label_img_ptr->image.rows; j++)
00080     {
00081       for (size_t i = 0; i < label_img_ptr->image.cols; i++)
00082       {
00083         int label = label_img_ptr->image.at<int>(j, i);
00084         if (label > max_label) {
00085            max_label = label;
00086         }
00087         label_to_indices[label].header = label_msg->header;
00088         label_to_indices[label].indices.push_back(j * label_img_ptr->image.cols + i);
00089       }
00090     }
00091     // convert 'map for label to indices' to 'cluster point indices'
00092     jsk_recognition_msgs::ClusterPointIndices cluster_indices_msg;
00093     pcl_msgs::PointIndices bg_indices_msg;
00094     cluster_indices_msg.header = bg_indices_msg.header = label_msg->header;
00095     for (size_t i=0; i <= max_label; i++)
00096     {
00097       pcl_msgs::PointIndices indices_msg;
00098       if (i == bg_label_) {
00099         if (label_to_indices.count(i) == 0) {
00100           indices_msg.header = label_msg->header;
00101           bg_indices_msg = indices_msg;
00102         }
00103         else {
00104           bg_indices_msg = label_to_indices[i];
00105         }
00106       }
00107       else if (label_to_indices.count(i) == 0 ||
00108                std::find(ignore_labels_.begin(), ignore_labels_.end(), i) != ignore_labels_.end()) {
00109         indices_msg.header = label_msg->header;
00110         cluster_indices_msg.cluster_indices.push_back(indices_msg);
00111       }
00112       else {
00113         cluster_indices_msg.cluster_indices.push_back(label_to_indices[i]);
00114       }
00115     }
00116     pub_bg_.publish(bg_indices_msg);
00117     pub_.publish(cluster_indices_msg);
00118   }
00119 
00120 }  // namespace jsk_pcl_ros_utils
00121 
00122 #include <pluginlib/class_list_macros.h>
00123 PLUGINLIB_EXPORT_CLASS(jsk_pcl_ros_utils::LabelToClusterPointIndices, nodelet::Nodelet);


jsk_pcl_ros_utils
Author(s): Yohei Kakiuchi
autogenerated on Sun Oct 8 2017 02:43:04