add_color_from_image_to_organized_nodelet.cpp
Go to the documentation of this file.
00001 // -*- mode: c++ -*-
00002 /*********************************************************************
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2017, 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 
00036 #define BOOST_PARAMETER_MAX_ARITY 7
00037 
00038 #include "jsk_pcl_ros/add_color_from_image_to_organized.h"
00039 #include <cv_bridge/cv_bridge.h>
00040 #include <sensor_msgs/image_encodings.h>
00041 #include <pcl_conversions/pcl_conversions.h>
00042 
00043 namespace jsk_pcl_ros
00044 {
00045   void AddColorFromImageToOrganized::onInit()
00046   {
00047     DiagnosticNodelet::onInit();
00048     pub_ = advertise<sensor_msgs::PointCloud2>(
00049       *pnh_, "output", 1);
00050     onInitPostProcess();
00051   }
00052 
00053   void AddColorFromImageToOrganized::subscribe()
00054   {
00055     sub_cloud_.subscribe(*pnh_, "input", 1);
00056     sub_image_.subscribe(*pnh_, "input/image", 1);
00057     sync_ = boost::make_shared<message_filters::Synchronizer<SyncPolicy> >(100);
00058     sync_->connectInput(sub_cloud_, sub_image_);
00059     sync_->registerCallback(boost::bind(&AddColorFromImageToOrganized::addColor, this, _1, _2));
00060   }
00061 
00062   void AddColorFromImageToOrganized::unsubscribe()
00063   {
00064     sub_cloud_.unsubscribe();
00065     sub_image_.unsubscribe();
00066   }
00067 
00068   void AddColorFromImageToOrganized::addColor(
00069     const sensor_msgs::PointCloud2::ConstPtr& cloud_msg,
00070     const sensor_msgs::Image::ConstPtr& image_msg)
00071   {
00072     vital_checker_->poke();
00073     if (cloud_msg->header.frame_id != image_msg->header.frame_id)
00074     {
00075       NODELET_FATAL("frame_id does not match: [%s, %s]",
00076                     cloud_msg->header.frame_id.c_str(),
00077                     image_msg->header.frame_id.c_str());
00078       return;
00079     }
00080     if (cloud_msg->height != image_msg->height || cloud_msg->width != image_msg->width)
00081     {
00082       NODELET_FATAL("Size of input cloud and image does not match.");
00083       return;
00084     }
00085 
00086     pcl::PointCloud<pcl::PointXYZ>::Ptr cloud
00087       (new pcl::PointCloud<pcl::PointXYZ>);
00088     pcl::fromROSMsg(*cloud_msg, *cloud);
00089 
00090     cv::Mat image = cv_bridge::toCvCopy(
00091       image_msg, sensor_msgs::image_encodings::BGR8)->image;
00092 
00093     pcl::PointCloud<pcl::PointXYZRGB>::Ptr rgb_cloud
00094       (new pcl::PointCloud<pcl::PointXYZRGB>);
00095     rgb_cloud->points.resize(cloud->points.size());
00096     rgb_cloud->is_dense = cloud->is_dense;
00097     rgb_cloud->width = cloud->width;
00098     rgb_cloud->height = cloud->height;
00099     for (size_t j=0; j<cloud->height; j++)
00100     {
00101       for (size_t i=0; i<cloud->width; i++)
00102       {
00103         pcl::PointXYZ p_xyz = cloud->points[i + j * cloud->width];
00104         pcl::PointXYZRGB p_color;
00105         p_color.x = p_xyz.x;
00106         p_color.y = p_xyz.y;
00107         p_color.z = p_xyz.z;
00108         cv::Vec3b bgr = image.at<cv::Vec3b>(j, i);
00109         p_color.b = bgr[0];
00110         p_color.g = bgr[1];
00111         p_color.r = bgr[2];
00112         rgb_cloud->points[i + j * cloud->width] = p_color;
00113       }
00114     }
00115     sensor_msgs::PointCloud2 ros_cloud;
00116     pcl::toROSMsg(*rgb_cloud, ros_cloud);
00117     ros_cloud.header = cloud_msg->header;
00118     pub_.publish(ros_cloud);
00119   }
00120 }
00121 
00122 #include <pluginlib/class_list_macros.h>
00123 PLUGINLIB_EXPORT_CLASS(jsk_pcl_ros::AddColorFromImageToOrganized, nodelet::Nodelet);


jsk_pcl_ros
Author(s): Yohei Kakiuchi
autogenerated on Sun Oct 8 2017 02:43:49