mask_image_to_rect.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>
41 #include <cv_bridge/cv_bridge.h>
43 
44 namespace jsk_perception
45 {
47  {
48  DiagnosticNodelet::onInit();
49  srv_ = boost::make_shared<dynamic_reconfigure::Server<Config> >(*pnh_);
50  typename dynamic_reconfigure::Server<Config>::CallbackType f =
51  boost::bind(&MaskImageToRect::configCallback, this, _1, _2);
52  srv_->setCallback(f);
53 
54  pub_ = advertise<jsk_recognition_msgs::RectArray>(*pnh_, "output", 1);
55  onInitPostProcess();
56  }
57 
59  {
60  sub_mask_ = pnh_->subscribe("input", 1, &MaskImageToRect::convert, this);
61  ros::V_string names = boost::assign::list_of("~input");
62  jsk_topic_tools::warnNoRemap(names);
63  }
64 
66  {
68  }
69 
70  void MaskImageToRect::configCallback(Config& config, uint32_t level)
71  {
72  rect_type_ = config.rect_type;
73  }
74 
76  const sensor_msgs::Image::ConstPtr& mask_msg)
77  {
78  vital_checker_->poke();
79  std::vector<cv::Point> indices;
82  cv::Mat mask = cv_ptr->image;
83  jsk_recognition_msgs::RectArray rects;
84  rects.header = mask_msg->header;
85 
86  switch (rect_type_) {
87  case jsk_perception::MaskImageToRect_Whole: {
88  for (size_t j = 0; j < mask.rows; j++) {
89  for (size_t i = 0; i < mask.cols; i++) {
90  if (mask.at<uchar>(j, i) == 255) {
91  indices.push_back(cv::Point(i, j));
92  }
93  }
94  }
95 
96  if (indices.size() > 0){
97  cv::Rect mask_rect = cv::boundingRect(indices);
98  jsk_recognition_msgs::Rect rect;
99  rect.x = mask_rect.x;
100  rect.y = mask_rect.y;
101  rect.width = mask_rect.width;
102  rect.height = mask_rect.height;
103  rects.rects.push_back(rect);
104  }
105  break;
106  }
107  case jsk_perception::MaskImageToRect_External: {
108  std::vector< std::vector<cv::Point> > contours;
109  std::vector<cv::Vec4i> hierarchy;
110  cv::findContours(mask, contours, hierarchy,
111  CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
112  for (size_t i = 0; i < contours.size(); ++i) {
113  cv::Rect cv_rect = jsk_recognition_utils::boundingRectFromContours(contours.at(i));
114  jsk_recognition_msgs::Rect rect;
115  rect.x = cv_rect.x;
116  rect.y = cv_rect.y;
117  rect.width = cv_rect.width;
118  rect.height = cv_rect.height;
119  rects.rects.push_back(rect);
120  }
121  break;
122  }
123  }
124  pub_.publish(rects);
125  }
126 }
127 
jsk_perception::MaskImageToRect
Definition: mask_image_to_rect.h:81
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(jsk_perception::MaskImageToRect, nodelet::Nodelet)
image_encodings.h
boost::shared_ptr< CvImage >
i
int i
jsk_perception::MaskImageToRect::srv_
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
Definition: mask_image_to_rect.h:128
jsk_perception::MaskImageToRect::onInit
virtual void onInit()
Definition: mask_image_to_rect.cpp:78
jsk_perception::MaskImageToRect::sub_mask_
ros::Subscriber sub_mask_
Definition: mask_image_to_rect.h:126
ros::Subscriber::shutdown
void shutdown()
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
class_list_macros.h
jsk_perception::MaskImageToRect::pub_
ros::Publisher pub_
Definition: mask_image_to_rect.h:127
jsk_perception
Definition: add_mask_image.h:48
jsk_perception::MaskImageToRect::subscribe
virtual void subscribe()
Definition: mask_image_to_rect.cpp:90
cv_bridge::toCvCopy
CvImagePtr toCvCopy(const sensor_msgs::CompressedImage &source, const std::string &encoding=std::string())
jsk_perception::MaskImageToRect::convert
virtual void convert(const sensor_msgs::Image::ConstPtr &mask_msg)
Definition: mask_image_to_rect.cpp:107
mask_image_to_rect.h
f
f
nodelet::Nodelet
sensor_msgs::image_encodings::MONO8
const std::string MONO8
jsk_perception::MaskImageToRect::configCallback
virtual void configCallback(Config &config, uint32_t level)
Definition: mask_image_to_rect.cpp:102
cv_bridge.h
cv_utils.h
ros::V_string
std::vector< std::string > V_string
jsk_perception::MaskImageToRect::rect_type_
int rect_type_
Definition: mask_image_to_rect.h:125
jsk_perception::MaskImageToRect::unsubscribe
virtual void unsubscribe()
Definition: mask_image_to_rect.cpp:97
jsk_recognition_utils::boundingRectFromContours
cv::Rect boundingRectFromContours(const std::vector< cv::Point > &contours)
config
config


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