kmeans.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 
00036 
00037 #include "jsk_perception/kmeans.h"
00038 #include <jsk_recognition_utils/cv_utils.h>
00039 #include <boost/assign.hpp>
00040 #include <jsk_topic_tools/log_utils.h>
00041 #include <sensor_msgs/image_encodings.h>
00042 #include <cv_bridge/cv_bridge.h>
00043 #include <opencv2/opencv.hpp>
00044 
00045 namespace jsk_perception
00046 {
00047   void KMeans::onInit()
00048   {
00049     DiagnosticNodelet::onInit();
00050     srv_ = boost::make_shared <dynamic_reconfigure::Server<Config> > (*pnh_);
00051     dynamic_reconfigure::Server<Config>::CallbackType f =
00052       boost::bind (&KMeans::configCallback, this, _1, _2);
00053     srv_->setCallback (f);
00054 
00055     pub_ = advertise<sensor_msgs::Image>(*pnh_, "output", 1);
00056     onInitPostProcess();
00057   }
00058 
00059   void KMeans::subscribe()
00060   {
00061     sub_ = pnh_->subscribe("input", 1, &KMeans::apply, this);
00062     ros::V_string names = boost::assign::list_of("~input");
00063     jsk_topic_tools::warnNoRemap(names);
00064   }
00065 
00066   void KMeans::unsubscribe()
00067   {
00068     sub_.shutdown();
00069   }
00070 
00071   void KMeans::configCallback(
00072     Config &config, uint32_t level)
00073   {
00074     boost::mutex::scoped_lock lock(mutex_);
00075     n_clusters_ = config.n_clusters;
00076   }
00077 
00078   void KMeans::apply(
00079     const sensor_msgs::Image::ConstPtr& image_msg)
00080   {
00081     if ((image_msg->width == 0) && (image_msg->height == 0)) {
00082         ROS_WARN("invalid image input");
00083         return;
00084     }
00085     cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(
00086       image_msg, image_msg->encoding);
00087     cv::Mat image = cv_ptr->image;
00088 
00089     cv::Mat reshaped_img = image.reshape(1, image.cols * image.rows);
00090     cv::Mat reshaped_img32f;
00091     reshaped_img.convertTo(reshaped_img32f, CV_32FC1, 1.0 / 255.0);
00092     cv::Mat labels;
00093     cv::TermCriteria criteria = cv::TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0);
00094     cv::Mat centers;
00095     cv::kmeans(reshaped_img32f, n_clusters_, labels, criteria, /*attempts=*/1, /*flags=*/cv::KMEANS_PP_CENTERS, centers);
00096 
00097     cv::Mat rgb_image(image.rows, image.cols, CV_8UC3);
00098     cv::MatIterator_<cv::Vec3b> rgb_first = rgb_image.begin<cv::Vec3b>();
00099     cv::MatIterator_<cv::Vec3b> rgb_last = rgb_image.end<cv::Vec3b>();
00100     cv::MatConstIterator_<int> label_first = labels.begin<int>();
00101 
00102     cv::Mat centers_u8;
00103     centers.convertTo(centers_u8, CV_8UC1, 255.0);
00104     cv::Mat centers_u8c3 = centers_u8.reshape(3);
00105 
00106     while ( rgb_first != rgb_last ) {
00107       const cv::Vec3b& rgb = centers_u8c3.ptr<cv::Vec3b>(*label_first)[0];
00108       *rgb_first = rgb;
00109       ++rgb_first;
00110       ++label_first;
00111     }
00112 
00113     pub_.publish(cv_bridge::CvImage(
00114                   image_msg->header,
00115                   image_msg->encoding,
00116                   rgb_image).toImageMsg());
00117   }
00118 
00119 }
00120 
00121 #include <pluginlib/class_list_macros.h>
00122 PLUGINLIB_EXPORT_CLASS (jsk_perception::KMeans, nodelet::Nodelet);


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Sun Oct 8 2017 02:43:23