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/or 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>
38 #include <jsk_topic_tools/log_utils.h>
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 {
48  void GridLabel::onInit()
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);
59  onInitPostProcess();
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");
73  jsk_topic_tools::warnNoRemap(names);
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_,
115  cv::rectangle(label, region, cv::Scalar(counter), CV_FILLED);
116  ++counter;
117  }
118  }
121  label).toImageMsg());
122  }
123 
124 }
125 
image_encodings.h
grid_label.h
jsk_perception::GridLabel::mutex_
boost::mutex mutex_
Definition: grid_label.h:130
counter
int counter
label
label
jsk_perception::GridLabel::Config
GridLabelConfig Config
Definition: grid_label.h:116
ros::Subscriber::shutdown
void shutdown()
jsk_perception::GridLabel::srv_
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
Definition: grid_label.h:129
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
jsk_perception::GridLabel::onInit
virtual void onInit()
Definition: grid_label.cpp:80
class_list_macros.h
jsk_perception
Definition: add_mask_image.h:48
jsk_perception::GridLabel::infoCallback
virtual void infoCallback(const sensor_msgs::CameraInfo::ConstPtr &info_msg)
Definition: grid_label.cpp:119
lock
mutex_t * lock
jsk_perception::GridLabel::label_size_
int label_size_
Definition: grid_label.h:132
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(jsk_perception::GridLabel, nodelet::Nodelet)
jsk_perception::GridLabel::imageCallback
virtual void imageCallback(const sensor_msgs::Image::ConstPtr &image_msg)
Definition: grid_label.cpp:129
f
f
nodelet::Nodelet
sensor_msgs::image_encodings::TYPE_32SC1
const std::string TYPE_32SC1
jsk_perception::GridLabel::use_camera_info_
bool use_camera_info_
Definition: grid_label.h:131
jsk_perception::GridLabel
Definition: grid_label.h:81
jsk_perception::GridLabel::configCallback
virtual void configCallback(Config &config, uint32_t level)
Definition: grid_label.cpp:113
cv_bridge.h
jsk_perception::GridLabel::unsubscribe
virtual void unsubscribe()
Definition: grid_label.cpp:108
cv_bridge::CvImage
jsk_perception::GridLabel::makeLabel
virtual void makeLabel(cv::Mat &label, const std_msgs::Header &header)
Definition: grid_label.cpp:139
jsk_perception::GridLabel::sub_
ros::Subscriber sub_
Definition: grid_label.h:133
ros::V_string
std::vector< std::string > V_string
jsk_perception::GridLabel::pub_
ros::Publisher pub_
Definition: grid_label.h:134
jsk_perception::GridLabel::subscribe
virtual void subscribe()
Definition: grid_label.cpp:94
config
config
v
GLfloat v[8][3]
info_msg
info_msg


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Fri May 16 2025 03:11:17