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


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Wed Sep 16 2015 04:36:15