color_histogram_filter_nodelet.cpp
Go to the documentation of this file.
00001 // -*- mode: c++ -*-
00002 /*********************************************************************
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2017, 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  * color_histogram_filter_nodelet.cpp
00037  * Author: Yuki Furuta <furushchev@jsk.imi.i.u-tokyo.ac.jp>
00038  */
00039 
00040 #include <limits>
00041 #include <jsk_pcl_ros/color_histogram_filter.h>
00042 
00043 namespace jsk_pcl_ros
00044 {
00045   void ColorHistogramFilter::onInit()
00046   {
00047     DiagnosticNodelet::onInit();
00048 
00049     pnh_->param("reference_histogram", reference_histogram_, std::vector<float>());
00050     if (reference_histogram_.empty()) {
00051       NODELET_WARN_STREAM("reference histogram is not yet set. waiting ~input/reference topic");
00052     }
00053 
00054     srv_ = boost::make_shared<dynamic_reconfigure::Server<Config> >(*pnh_);
00055     dynamic_reconfigure::Server<Config>::CallbackType f =
00056       boost::bind(&ColorHistogramFilter::configCallback, this, _1, _2);
00057     srv_->setCallback(f);
00058     pub_histogram_ = advertise<jsk_recognition_msgs::ColorHistogramArray>(*pnh_, "output", 1);
00059     pub_indices_ = advertise<jsk_recognition_msgs::ClusterPointIndices>(*pnh_, "output/indices", 1);
00060     onInitPostProcess();
00061   }
00062 
00063   void ColorHistogramFilter::configCallback(Config &config, uint32_t level)
00064   {
00065     boost::mutex::scoped_lock lock(mutex_);
00066     compare_policy_ = jsk_recognition_utils::ComparePolicy(config.compare_policy);
00067     distance_threshold_ = config.distance_threshold;
00068     flip_threshold_ = config.flip_threshold;
00069     if (queue_size_ != config.queue_size) {
00070       queue_size_ = config.queue_size;
00071       if (isSubscribed()) {
00072         unsubscribe();
00073         subscribe();
00074       }
00075     }
00076   }
00077 
00078   void ColorHistogramFilter::subscribe()
00079   {
00080     sub_reference_ = pnh_->subscribe("input/reference", 1,
00081                                      &ColorHistogramFilter::reference, this);
00082     sub_histogram_.subscribe(*pnh_, "input", 1);
00083     sub_indices_.subscribe(*pnh_, "input/indices", 1);
00084     sync_ = boost::make_shared<message_filters::Synchronizer<SyncPolicy> >(queue_size_);
00085     sync_->connectInput(sub_histogram_, sub_indices_);
00086     sync_->registerCallback(boost::bind(&ColorHistogramFilter::feature, this, _1, _2));
00087   }
00088 
00089   void ColorHistogramFilter::unsubscribe()
00090   {
00091     sub_reference_.shutdown();
00092     sub_histogram_.unsubscribe();
00093     sub_indices_.unsubscribe();
00094   }
00095 
00096   void ColorHistogramFilter::feature(const jsk_recognition_msgs::ColorHistogramArray::ConstPtr &input_histogram,
00097                                      const jsk_recognition_msgs::ClusterPointIndices::ConstPtr &input_indices)
00098   {
00099     boost::mutex::scoped_lock lock(mutex_);
00100     if (reference_histogram_.empty()) {
00101       NODELET_WARN_THROTTLE(1.0, "reference histogram is empty");
00102       return;
00103     }
00104 
00105     // check histogram size
00106     size_t size = reference_histogram_.size();
00107     for (size_t i = 0; i < input_histogram->histograms.size(); ++i) {
00108       if (input_histogram->histograms[i].histogram.size() != size) {
00109         NODELET_ERROR_STREAM("size of histogram " << i << " is different from reference");
00110         return;
00111       }
00112     }
00113 
00114     jsk_recognition_msgs::ColorHistogramArray out_hist;
00115     jsk_recognition_msgs::ClusterPointIndices out_indices;
00116 
00117     out_hist.header = input_histogram->header;
00118     out_indices.header = input_indices->header;
00119 
00120     NODELET_DEBUG("distance_threshold: %f", distance_threshold_);
00121     for (size_t i = 0; i < input_histogram->histograms.size(); ++i) {
00122       double distance = 0.0;
00123       bool ok = jsk_recognition_utils::compareHistogram(
00124         input_histogram->histograms[i].histogram, reference_histogram_,
00125         compare_policy_, distance);
00126       if (flip_threshold_) ok &= distance_threshold_ < distance;
00127       else                 ok &= distance_threshold_ > distance;
00128       NODELET_DEBUG("#%lu: %f (%s)", i, distance, ok ? "OK" : "NG");
00129       if (ok) {
00130         out_hist.histograms.push_back(input_histogram->histograms[i]);
00131         out_indices.cluster_indices.push_back(input_indices->cluster_indices[i]);
00132       }
00133     }
00134 
00135     pub_histogram_.publish(out_hist);
00136     pub_indices_.publish(out_indices);
00137   }
00138 
00139   void ColorHistogramFilter::reference(const jsk_recognition_msgs::ColorHistogram::ConstPtr &input)
00140   {
00141     boost::mutex::scoped_lock lock(mutex_);
00142     reference_histogram_ = input->histogram;
00143   }
00144 }
00145 
00146 #include <pluginlib/class_list_macros.h>
00147 PLUGINLIB_EXPORT_CLASS(jsk_pcl_ros::ColorHistogramFilter, nodelet::Nodelet);


jsk_pcl_ros
Author(s): Yohei Kakiuchi
autogenerated on Tue Jul 2 2019 19:41:42