grid_label.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 
37 #include <boost/assign.hpp>
39 #include <opencv2/opencv.hpp>
40 #include <cv_bridge/cv_bridge.h>
42 #if ( CV_MAJOR_VERSION >= 4)
43 #include <opencv2/imgproc/imgproc_c.h>
44 #endif
45 
46 namespace jsk_perception
47 {
49  {
50  DiagnosticNodelet::onInit();
51 
52  srv_ = boost::make_shared <dynamic_reconfigure::Server<Config> > (*pnh_);
53  dynamic_reconfigure::Server<Config>::CallbackType f =
54  boost::bind (&GridLabel::configCallback, this, _1, _2);
55  srv_->setCallback (f);
56 
57  pnh_->param("use_camera_info", use_camera_info_, false);
58  pub_ = advertise<sensor_msgs::Image>(*pnh_, "output", 1);
60  }
61 
63  {
64  if (use_camera_info_) {
65  sub_ = pnh_->subscribe(
66  "input", 1, &GridLabel::infoCallback, this);
67  }
68  else {
69  sub_ = pnh_->subscribe(
70  "input", 1, &GridLabel::imageCallback, this);
71  }
72  ros::V_string names = boost::assign::list_of("~input");
74  }
75 
77  {
78  sub_.shutdown();
79  }
80 
81  void GridLabel::configCallback(Config &config, uint32_t level)
82  {
83  boost::mutex::scoped_lock lock(mutex_);
84  label_size_ = config.label_size;
85  }
86 
88  const sensor_msgs::CameraInfo::ConstPtr& info_msg)
89  {
90  boost::mutex::scoped_lock lock(mutex_);
91  cv::Mat label = cv::Mat::zeros(info_msg->height,
92  info_msg->width,
93  CV_32SC1); // int
94  makeLabel(label, info_msg->header);
95  }
96 
98  const sensor_msgs::Image::ConstPtr& image_msg)
99  {
100  boost::mutex::scoped_lock lock(mutex_);
101  cv::Mat label = cv::Mat::zeros(image_msg->height,
102  image_msg->width,
103  CV_32SC1); // int
104  makeLabel(label, image_msg->header);
105  }
106 
107  void GridLabel::makeLabel(cv::Mat& label, const std_msgs::Header& header) {
108  int num_u = ceil(label.cols / (float)label_size_);
109  int num_v = ceil(label.rows / (float)label_size_);
110  int counter = 1;
111  for (int v = 0; v < num_v; v++) {
112  for (int u = 0; u < num_u; u++) {
113  cv::Rect region(u * label_size_, v * label_size_,
114  label_size_, label_size_);
115  cv::rectangle(label, region, cv::Scalar(counter), CV_FILLED);
116  ++counter;
117  }
118  }
121  label).toImageMsg());
122  }
123 
124 }
125 
virtual void unsubscribe()
Definition: grid_label.cpp:76
virtual void makeLabel(cv::Mat &label, const std_msgs::Header &header)
Definition: grid_label.cpp:107
f
virtual void subscribe()
Definition: grid_label.cpp:62
void publish(const boost::shared_ptr< M > &message) const
int counter
virtual void infoCallback(const sensor_msgs::CameraInfo::ConstPtr &info_msg)
Definition: grid_label.cpp:87
std::vector< std::string > V_string
PLUGINLIB_EXPORT_CLASS(jsk_perception::GridLabel, nodelet::Nodelet)
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
Definition: grid_label.h:65
boost::shared_ptr< ros::NodeHandle > pnh_
bool warnNoRemap(const std::vector< std::string > names)
virtual void imageCallback(const sensor_msgs::Image::ConstPtr &image_msg)
Definition: grid_label.cpp:97
GridLabelConfig Config
Definition: grid_label.h:52
ros::Subscriber sub_
Definition: grid_label.h:69
ros::Publisher pub_
Definition: grid_label.h:70
mutex_t * lock
label
GLfloat v[8][3]
const std::string TYPE_32SC1
virtual void configCallback(Config &config, uint32_t level)
Definition: grid_label.cpp:81


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