39 #include <boost/assign.hpp> 43 #include <opencv2/opencv.hpp> 49 DiagnosticNodelet::onInit();
50 srv_ = boost::make_shared <dynamic_reconfigure::Server<Config> > (*pnh_);
51 dynamic_reconfigure::Server<Config>::CallbackType
f =
53 srv_->setCallback (f);
55 pub_ = advertise<sensor_msgs::Image>(*
pnh_,
"output", 1);
72 Config &config, uint32_t level)
79 const sensor_msgs::Image::ConstPtr& image_msg)
81 if ((image_msg->width == 0) && (image_msg->height == 0)) {
86 image_msg, image_msg->encoding);
87 cv::Mat image = cv_ptr->image;
89 cv::Mat reshaped_img = image.reshape(1, image.cols * image.rows);
90 cv::Mat reshaped_img32f;
91 reshaped_img.convertTo(reshaped_img32f, CV_32FC1, 1.0 / 255.0);
93 cv::TermCriteria criteria = cv::TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0);
95 cv::kmeans(reshaped_img32f,
n_clusters_, labels, criteria, 1, cv::KMEANS_PP_CENTERS, centers);
97 cv::Mat rgb_image(image.rows, image.cols, CV_8UC3);
98 cv::MatIterator_<cv::Vec3b> rgb_first = rgb_image.begin<cv::Vec3b>();
99 cv::MatIterator_<cv::Vec3b> rgb_last = rgb_image.end<cv::Vec3b>();
100 cv::MatConstIterator_<int> label_first = labels.begin<
int>();
103 centers.convertTo(centers_u8, CV_8UC1, 255.0);
104 cv::Mat centers_u8c3 = centers_u8.reshape(3);
106 while ( rgb_first != rgb_last ) {
107 const cv::Vec3b& rgb = centers_u8c3.ptr<cv::Vec3b>(*label_first)[0];
virtual void apply(const sensor_msgs::Image::ConstPtr &image_msg)
void publish(const boost::shared_ptr< M > &message) const
virtual void configCallback(Config &config, uint32_t level)
PLUGINLIB_EXPORT_CLASS(jsk_perception::KMeans, nodelet::Nodelet)
std::vector< std::string > V_string
virtual void unsubscribe()
CvImagePtr toCvCopy(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
jsk_perception::KMeansConfig Config
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
sensor_msgs::ImagePtr toImageMsg() const