color_histogram_filter_nodelet.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2017, JSK Lab
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/o2r other materials provided
17  * with the distribution.
18  * * Neither the name of the JSK Lab nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 /*
36  * color_histogram_filter_nodelet.cpp
37  * Author: Yuki Furuta <furushchev@jsk.imi.i.u-tokyo.ac.jp>
38  */
39 
40 #include <limits>
42 
43 namespace jsk_pcl_ros
44 {
46  {
47  DiagnosticNodelet::onInit();
48 
49  pnh_->param("reference_histogram", reference_histogram_, std::vector<float>());
50  if (reference_histogram_.empty()) {
51  NODELET_WARN_STREAM("reference histogram is not yet set. waiting ~input/reference topic");
52  }
53 
54  srv_ = boost::make_shared<dynamic_reconfigure::Server<Config> >(*pnh_);
55  dynamic_reconfigure::Server<Config>::CallbackType f =
56  boost::bind(&ColorHistogramFilter::configCallback, this, _1, _2);
57  srv_->setCallback(f);
58  pub_histogram_ = advertise<jsk_recognition_msgs::ColorHistogramArray>(*pnh_, "output", 1);
59  pub_indices_ = advertise<jsk_recognition_msgs::ClusterPointIndices>(*pnh_, "output/indices", 1);
61  }
62 
63  void ColorHistogramFilter::configCallback(Config &config, uint32_t level)
64  {
65  boost::mutex::scoped_lock lock(mutex_);
67  distance_threshold_ = config.distance_threshold;
68  flip_threshold_ = config.flip_threshold;
69  if (queue_size_ != config.queue_size) {
70  queue_size_ = config.queue_size;
71  if (isSubscribed()) {
72  unsubscribe();
73  subscribe();
74  }
75  }
76  }
77 
79  {
80  sub_reference_ = pnh_->subscribe("input/reference", 1,
82  sub_histogram_.subscribe(*pnh_, "input", 1);
83  sub_indices_.subscribe(*pnh_, "input/indices", 1);
84  sync_ = boost::make_shared<message_filters::Synchronizer<SyncPolicy> >(queue_size_);
85  sync_->connectInput(sub_histogram_, sub_indices_);
86  sync_->registerCallback(boost::bind(&ColorHistogramFilter::feature, this, _1, _2));
87  }
88 
90  {
94  }
95 
96  void ColorHistogramFilter::feature(const jsk_recognition_msgs::ColorHistogramArray::ConstPtr &input_histogram,
97  const jsk_recognition_msgs::ClusterPointIndices::ConstPtr &input_indices)
98  {
99  boost::mutex::scoped_lock lock(mutex_);
100  if (reference_histogram_.empty()) {
101  NODELET_WARN_THROTTLE(1.0, "reference histogram is empty");
102  return;
103  }
104 
105  // check histogram size
106  size_t size = reference_histogram_.size();
107  for (size_t i = 0; i < input_histogram->histograms.size(); ++i) {
108  if (input_histogram->histograms[i].histogram.size() != size) {
109  NODELET_ERROR_STREAM("size of histogram " << i << " is different from reference");
110  return;
111  }
112  }
113 
114  jsk_recognition_msgs::ColorHistogramArray out_hist;
115  jsk_recognition_msgs::ClusterPointIndices out_indices;
116 
117  out_hist.header = input_histogram->header;
118  out_indices.header = input_indices->header;
119 
120  NODELET_DEBUG("distance_threshold: %f", distance_threshold_);
121  for (size_t i = 0; i < input_histogram->histograms.size(); ++i) {
122  double distance = 0.0;
124  input_histogram->histograms[i].histogram, reference_histogram_,
125  compare_policy_, distance);
126  if (flip_threshold_) ok &= distance_threshold_ < distance;
127  else ok &= distance_threshold_ > distance;
128  NODELET_DEBUG("#%lu: %f (%s)", i, distance, ok ? "OK" : "NG");
129  if (ok) {
130  out_hist.histograms.push_back(input_histogram->histograms[i]);
131  out_indices.cluster_indices.push_back(input_indices->cluster_indices[i]);
132  }
133  }
134 
135  pub_histogram_.publish(out_hist);
136  pub_indices_.publish(out_indices);
137  }
138 
139  void ColorHistogramFilter::reference(const jsk_recognition_msgs::ColorHistogram::ConstPtr &input)
140  {
141  boost::mutex::scoped_lock lock(mutex_);
142  reference_histogram_ = input->histogram;
143  }
144 }
145 
message_filters::Subscriber< jsk_recognition_msgs::ClusterPointIndices > sub_indices_
void publish(const boost::shared_ptr< M > &message) const
bool compareHistogram(const std::vector< float > &input, const std::vector< float > &reference, const ComparePolicy policy, double &distance)
virtual void reference(const jsk_recognition_msgs::ColorHistogram::ConstPtr &input)
message_filters::Subscriber< jsk_recognition_msgs::ColorHistogramArray > sub_histogram_
#define NODELET_WARN_STREAM(...)
size
#define NODELET_ERROR_STREAM(...)
PLUGINLIB_EXPORT_CLASS(jsk_pcl_ros::ColorHistogramFilter, nodelet::Nodelet)
#define NODELET_WARN_THROTTLE(rate,...)
boost::shared_ptr< ros::NodeHandle > pnh_
jsk_recognition_utils::ComparePolicy compare_policy_
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
void subscribe(ros::NodeHandle &nh, const std::string &topic, uint32_t queue_size, const ros::TransportHints &transport_hints=ros::TransportHints(), ros::CallbackQueueInterface *callback_queue=0)
virtual void feature(const jsk_recognition_msgs::ColorHistogramArray::ConstPtr &input_histogram, const jsk_recognition_msgs::ClusterPointIndices::ConstPtr &input_indices)
virtual void configCallback(Config &config, uint32_t level)
boost::shared_ptr< message_filters::Synchronizer< SyncPolicy > > sync_
#define NODELET_DEBUG(...)
double distance(const urdf::Pose &transform)


jsk_pcl_ros
Author(s): Yohei Kakiuchi
autogenerated on Mon May 3 2021 03:03:46