polygon_array_color_histogram.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2015, 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 #define BOOST_PARAMETER_MAX_ARITY 7
37 #include <cv_bridge/cv_bridge.h>
39 #include <jsk_recognition_msgs/HistogramWithRangeArray.h>
46 
47 namespace jsk_perception
48 {
50  {
51  DiagnosticNodelet::onInit();
53  pnh_->param("max_queue_size", max_queue_size_, 10);
54  pnh_->param("synchronizer_queue_size", sync_queue_size_, 100);
55  srv_ = boost::make_shared <dynamic_reconfigure::Server<Config> > (*pnh_);
56  dynamic_reconfigure::Server<Config>::CallbackType f =
57  boost::bind (
59  srv_->setCallback (f);
60  pub_ = advertise<jsk_recognition_msgs::HistogramWithRangeArray>(*pnh_, "output", 1);
61  pub_debug_polygon_ = advertise<sensor_msgs::Image>(*pnh_, "debug/polygon_image", 1);
63  }
64 
66  {
67  sub_info_ = pnh_->subscribe("input/info", 1, &PolygonArrayColorHistogram::infoCallback, this);
69  sub_image_.subscribe(*pnh_, "input/image", max_queue_size_);
70  async_ = boost::make_shared<message_filters::Synchronizer<ApproximateSyncPolicy> >(sync_queue_size_);
71  async_->connectInput(sub_image_, sub_polygon_);
72  async_->registerCallback(
73  boost::bind(&PolygonArrayColorHistogram::compute, this, _1, _2));
74  }
75 
77  {
81  }
82 
83  void PolygonArrayColorHistogram::infoCallback(const sensor_msgs::CameraInfo::ConstPtr& msg)
84  {
85  boost::mutex::scoped_lock lock(mutex_);
86  info_ = msg;
87  }
88 
91  cv::Mat& image,
93  size_t pi) const
94  {
95  polygon->drawLineToImage(model, image,
98  if (polygon->centroid()[2] > 0) {
99  std::stringstream ss;
100  ss << pi;
101  cv::putText(image, ss.str(),
103  cv::FONT_HERSHEY_SIMPLEX,
104  0.5,
106  }
107  }
108 
109 
111  const sensor_msgs::Image::ConstPtr& image_msg,
112  const jsk_recognition_msgs::PolygonArray::ConstPtr& polygon_msg)
113  {
114  boost::mutex::scoped_lock lock(mutex_);
115  if (!info_) {
116  NODELET_WARN("No camera_info is available");
117  return;
118  }
119  // check frame_id
120  if (!jsk_recognition_utils::isSameFrameId(image_msg->header.frame_id,
121  polygon_msg->header.frame_id)) {
122  NODELET_ERROR("frame_id does not match. image: %s, polygon: %s",
123  image_msg->header.frame_id.c_str(),
124  polygon_msg->header.frame_id.c_str());
125  return;
126  }
127  try
128  {
131  tf_listener_, polygon_msg->header.frame_id, image_msg->header.frame_id,
132  image_msg->header.stamp, ros::Duration(1.0));
133  Eigen::Affine3f polygon_transform;
134  tf::transformTFToEigen(transform, polygon_transform);
135  cv_bridge::CvImagePtr input_cv_image
137  cv::Mat input_image = input_cv_image->image;
139  model.setCameraInfo(*info_);
140  cv::Mat debug_polygon_image = model.image(CV_8UC3);
141  std::vector<jsk_recognition_utils::Polygon::Ptr> polygons
142  = jsk_recognition_utils::Polygon::fromROSMsg(*polygon_msg, polygon_transform);
143  cv::Mat image = model.image(CV_8UC1);
144  jsk_recognition_msgs::HistogramWithRangeArray histogram_array;
145  histogram_array.header = polygon_msg->header;
146  for (size_t pi = 0; pi < polygons.size(); pi++) {
147  jsk_recognition_utils::Polygon::Ptr polygon = polygons[pi];
148  debugPolygonImage(model, debug_polygon_image, polygon, pi);
149  cv::Mat mask_image;
150  polygon->maskImage(model, mask_image);
151  cv::MatND hist = jsk_recognition_utils::computeHistogram(input_image, bin_size_,
153  mask_image);
154  jsk_recognition_msgs::HistogramWithRange histogram;
155  histogram.header = polygon_msg->header;
156  histogram.bins
159  histogram_array.histograms.push_back(histogram);
160  }
163  debug_polygon_image).toImageMsg());
164  pub_.publish(histogram_array);
165  }
166  catch (...)
167  {
168  NODELET_ERROR("Failed to resolve tf");
169  }
170 
171  }
172 
174  {
175  boost::mutex::scoped_lock lock(mutex_);
176  bin_size_ = config.bin_size;
177  pixel_min_value_ = config.pixel_min_value;
178  pixel_max_value_ = config.pixel_max_value;
179  debug_line_width_ = config.debug_line_width;
180  }
181 
182 }
183 
f
virtual void configCallback(Config &config, uint32_t level)
#define NODELET_ERROR(...)
#define NODELET_WARN(...)
void publish(const boost::shared_ptr< M > &message) const
void transformTFToEigen(const tf::Transform &t, Eigen::Affine3d &e)
tf::StampedTransform lookupTransformWithDuration(tf::TransformListener *listener, const std::string &to_frame, const std::string &from_frame, const ros::Time &stamp, ros::Duration duration)
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
cv::MatND computeHistogram(const cv::Mat &input_image, int bin_size, float min_value, float max_value, const cv::Mat &mask_image)
virtual cv::Mat image(const int type) const
static Polygon fromROSMsg(const geometry_msgs::Polygon &polygon)
virtual image_geometry::PinholeCameraModel getPinholeCameraModel() const
cv::Point project3DPointToPixel(const image_geometry::PinholeCameraModel &model, const Eigen::Vector3f &p)
hist
std::vector< jsk_recognition_msgs::HistogramWithRangeBin > cvMatNDToHistogramWithRangeBinArray(const cv::MatND &cv_hist, float min_value, float max_value)
jsk_perception::PolygonArrayColorHistogramConfig Config
virtual void infoCallback(const sensor_msgs::CameraInfo::ConstPtr &msg)
boost::shared_ptr< ros::NodeHandle > pnh_
CvImagePtr toCvCopy(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
cv::Scalar colorROSToCVRGB(const std_msgs::ColorRGBA &ros_color)
mutex_t * lock
message_filters::Subscriber< jsk_recognition_msgs::PolygonArray > sub_polygon_
std_msgs::ColorRGBA colorCategory20(int i)
boost::shared_ptr< message_filters::Synchronizer< ApproximateSyncPolicy > > async_
virtual void debugPolygonImage(const jsk_recognition_utils::CameraDepthSensor &model, cv::Mat &image, jsk_recognition_utils::Polygon::Ptr polygon, size_t pi) const
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 compute(const sensor_msgs::Image::ConstPtr &image_msg, const jsk_recognition_msgs::PolygonArray::ConstPtr &polygon_msg)
bool isSameFrameId(const std::string &a, const std::string &b)
static tf::TransformListener * getInstance()
message_filters::Subscriber< sensor_msgs::Image > sub_image_
sensor_msgs::ImagePtr toImageMsg() const
virtual void setCameraInfo(const sensor_msgs::CameraInfo &info)
PLUGINLIB_EXPORT_CLASS(jsk_perception::PolygonArrayColorHistogram, nodelet::Nodelet)


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Mon May 3 2021 03:03:27