Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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, 1, 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);