bing.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2016, 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 
36 #include "jsk_perception/bing.h"
37 #include <ros/ros.h>
38 #include <rospack/rospack.h>
39 #include <boost/assign.hpp>
40 #include <boost/filesystem.hpp>
42 #include <jsk_topic_tools/log_utils.h>
43 #include <jsk_recognition_msgs/RectArray.h>
44 #include <cv_bridge/cv_bridge.h>
45 #include <opencv2/opencv.hpp>
46 #include <opencv2/saliency.hpp>
47 #include <vector>
48 #include <algorithm>
49 
50 namespace jsk_perception
51 {
52  void Bing::onInit()
53  {
54  DiagnosticNodelet::onInit();
55  pub_rects_ = advertise<jsk_recognition_msgs::RectArray>(*pnh_, "output", 1);
56  pub_objectness_ = advertise<sensor_msgs::Image>(*pnh_, "output/objectness", 1);
57  pnh_->param("score_threshold", score_threshold_, 0.0);
58  pnh_->param("max_num", max_num_, 0);
59  // find trained data
60  std::string training_path;
61 #ifdef ROSPACK_EXPORT
63  rospack::Package *p = rp.get_pkg("jsk_perception");
64  if (p == NULL) {
65  ROS_ERROR("Package path of 'jsk_perception' does not found");
66  exit(1);
67  } else {
68  training_path = p->path + std::string("/trained_data/ObjectnessTrainedModel");
69  }
70 #else
72  ros::V_string search_path;
73  rp.getSearchPathFromEnv(search_path);
74  rp.crawl(search_path, 1);
75  std::string path;
76  if (rp.find("jsk_perception", path) == true) {
77  training_path = path + std::string("/trained_data/ObjectnessTrainedModel");
78  } else {
79  ROS_ERROR("Package path of 'jsk_perception' does not found");
80  exit(1);
81  }
82 #endif
83  if (!boost::filesystem::exists(training_path)) {
84  ROS_ERROR("Training data path '%s' does not exist", training_path.c_str());
85  exit(1);
86  }
87  // setup bing
88  binger_ = new cv::saliency::ObjectnessBING();
89  binger_->setTrainingPath(training_path);
90  onInitPostProcess();
91  }
92 
93  void Bing::subscribe()
94  {
95  sub_ = pnh_->subscribe("input", 1, &Bing::apply, this);
96  ros::V_string names = boost::assign::list_of("~input");
97  jsk_topic_tools::warnNoRemap(names);
98  }
99 
100  void Bing::unsubscribe()
101  {
102  sub_.shutdown();
103  }
104 
105  void Bing::apply(
106  const sensor_msgs::Image::ConstPtr& img_msg)
107  {
109  img_msg, img_msg->encoding);
110  cv::Mat img = cv_ptr->image;
111 
112  // Resize too large image for fast processing
113  double scale = 1.0;
114  if (img.rows * img.cols > 250000) {
115  scale = static_cast<double>(std::min(500. / img.rows, 500. / img.cols));
116  cv::resize(img, img, cv::Size(static_cast<int>(scale * img.cols),
117  static_cast<int>(scale * img.rows)));
118  }
119 
120  std::vector<cv::Vec4i> saliency_map;
121  binger_->computeSaliency(img, saliency_map);
122  std::vector<float> objectness_values = binger_->getobjectnessValues();
123  float max_objectness_values = *std::max_element(
124  objectness_values.begin(), objectness_values.end());
125  float threshold = score_threshold_ * max_objectness_values;
126 
127  jsk_recognition_msgs::RectArray rects_msg;
128  cv::Mat objectness_img = cv::Mat(img.rows, img.cols, CV_32FC1);
129  int count = 0;
130  for (size_t k=0; k < saliency_map.size(); k++) {
131  if (objectness_values[k] < threshold) {
132  continue;
133  }
134  int min_x = static_cast<int>(saliency_map[k][0] / scale);
135  int min_y = static_cast<int>(saliency_map[k][1] / scale);
136  int max_x = static_cast<int>(saliency_map[k][2] / scale);
137  int max_y = static_cast<int>(saliency_map[k][3] / scale);
138  // set a proposal
139  jsk_recognition_msgs::Rect rect;
140  rect.x = min_x;
141  rect.y = min_y;
142  rect.width = max_x - min_x;
143  rect.height = max_y - min_y;
144  rects_msg.rects.push_back(rect);
145  // set objectness
146  for (size_t j=std::max(0, min_y); j < std::min(max_y, img.rows); j++) {
147  for (size_t i=std::max(0, min_x); i < std::min(max_x, img.cols); i++) {
148  objectness_img.at<float>(j, i) += objectness_values[k];
149  }
150  }
151  count++;
152  if ((max_num_ > 0) && (count >= max_num_)) {
153  break;
154  }
155  }
156  // publish proposals
157  rects_msg.header = img_msg->header;
158  pub_rects_.publish(rects_msg);
159  // publish objectness
162  img_msg->header,
164  objectness_img).toImageMsg());
165  }
166 
167 } // namespace jsk_perception
168 
jsk_perception::Bing::pub_objectness_
ros::Publisher pub_objectness_
Definition: bing.h:124
image_encodings.h
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(jsk_perception::Bing, nodelet::Nodelet)
boost::shared_ptr< CvImage >
rospack::Rosstackage::getSearchPathFromEnv
bool getSearchPathFromEnv(std::vector< std::string > &sp)
i
int i
cv_bridge::CvImage::toImageMsg
sensor_msgs::ImagePtr toImageMsg() const
rospack::Rospack
ros.h
rospack::Rosstackage::find
bool find(const std::string &name, std::string &path)
ros::Subscriber::shutdown
void shutdown()
bing.h
jsk_perception::Bing::unsubscribe
virtual void unsubscribe()
Definition: bing.cpp:132
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
jsk_perception::Bing::pub_rects_
ros::Publisher pub_rects_
Definition: bing.h:123
jsk_perception::Bing::sub_
ros::Subscriber sub_
Definition: bing.h:122
rospack::ROSPack
img
img
class_list_macros.h
rospack::Rosstackage::crawl
void crawl(std::vector< std::string > search_path, bool force)
jsk_perception
Definition: add_mask_image.h:48
jsk_perception::Bing::binger_
cv::saliency::ObjectnessBING * binger_
Definition: bing.h:125
cv_bridge::toCvCopy
CvImagePtr toCvCopy(const sensor_msgs::CompressedImage &source, const std::string &encoding=std::string())
random_forest_client_sample.img_msg
img_msg
Definition: random_forest_client_sample.py:111
k
int k
rp
rospack::Rospack rp
node_scripts.pointit.p
p
Definition: pointit.py:231
jsk_perception::Bing::score_threshold_
double score_threshold_
Definition: bing.h:126
jsk_perception::Bing
Definition: bing.h:79
jsk_perception::Bing::max_num_
int max_num_
Definition: bing.h:127
count
int count
jsk_perception::Bing::apply
virtual void apply(const sensor_msgs::Image::ConstPtr &image_msg)
Definition: bing.cpp:137
threshold
nodelet::Nodelet
jsk_perception::Bing::subscribe
virtual void subscribe()
Definition: bing.cpp:125
sensor_msgs::image_encodings::TYPE_32FC1
const std::string TYPE_32FC1
cv_bridge.h
ROS_ERROR
#define ROS_ERROR(...)
rospack.h
cv_bridge::CvImage
ros::V_string
std::vector< std::string > V_string
jsk_perception::Bing::onInit
virtual void onInit()
Definition: bing.cpp:84


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