Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "jsk_perception/bing.h"
00037 #include <ros/ros.h>
00038 #include <rospack/rospack.h>
00039 #include <boost/assign.hpp>
00040 #include <boost/filesystem.hpp>
00041 #include <sensor_msgs/image_encodings.h>
00042 #include <jsk_topic_tools/log_utils.h>
00043 #include <jsk_recognition_msgs/RectArray.h>
00044 #include <cv_bridge/cv_bridge.h>
00045 #include <opencv2/opencv.hpp>
00046 #include <opencv2/saliency.hpp>
00047 #include <vector>
00048 #include <algorithm>
00049
00050 namespace jsk_perception
00051 {
00052 void Bing::onInit()
00053 {
00054 DiagnosticNodelet::onInit();
00055 pub_rects_ = advertise<jsk_recognition_msgs::RectArray>(*pnh_, "output", 1);
00056 pub_objectness_ = advertise<sensor_msgs::Image>(*pnh_, "output/objectness", 1);
00057
00058 std::string training_path;
00059 #ifdef ROSPACK_EXPORT
00060 rospack::ROSPack rp;
00061 rospack::Package *p = rp.get_pkg("jsk_perception");
00062 if (p == NULL) {
00063 ROS_ERROR("Package path of 'jsk_perception' does not found");
00064 exit(1);
00065 } else {
00066 training_path = p->path + std::string("/trained_data/ObjectnessTrainedModel");
00067 }
00068 #else
00069 rospack::Rospack rp;
00070 ros::V_string search_path;
00071 rp.getSearchPathFromEnv(search_path);
00072 rp.crawl(search_path, 1);
00073 std::string path;
00074 if (rp.find("jsk_perception", path) == true) {
00075 training_path = path + std::string("/trained_data/ObjectnessTrainedModel");
00076 } else {
00077 ROS_ERROR("Package path of 'jsk_perception' does not found");
00078 exit(1);
00079 }
00080 #endif
00081 if (!boost::filesystem::exists(training_path)) {
00082 ROS_ERROR("Training data path '%s' does not exist", training_path.c_str());
00083 exit(1);
00084 }
00085
00086 binger_ = new cv::saliency::ObjectnessBING();
00087 binger_->setTrainingPath(training_path);
00088 onInitPostProcess();
00089 }
00090
00091 void Bing::subscribe()
00092 {
00093 sub_ = pnh_->subscribe("input", 1, &Bing::apply, this);
00094 ros::V_string names = boost::assign::list_of("~input");
00095 jsk_topic_tools::warnNoRemap(names);
00096 }
00097
00098 void Bing::unsubscribe()
00099 {
00100 sub_.shutdown();
00101 }
00102
00103 void Bing::apply(
00104 const sensor_msgs::Image::ConstPtr& img_msg)
00105 {
00106 cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(
00107 img_msg, img_msg->encoding);
00108 cv::Mat img = cv_ptr->image;
00109
00110
00111 double scale = 1.0;
00112 if (img.rows * img.cols > 250000) {
00113 scale = static_cast<double>(std::min(500. / img.rows, 500. / img.cols));
00114 cv::resize(img, img, cv::Size(static_cast<int>(scale * img.cols),
00115 static_cast<int>(scale * img.rows)));
00116 }
00117
00118 std::vector<cv::Vec4i> saliency_map;
00119 binger_->computeSaliency(img, saliency_map);
00120 std::vector<float> objectness_values = binger_->getobjectnessValues();
00121
00122 jsk_recognition_msgs::RectArray rects_msg;
00123 cv::Mat objectness_img = cv::Mat(img.rows, img.cols, CV_32FC1);
00124 for (size_t k=0; k < saliency_map.size(); k++) {
00125 int min_x = static_cast<int>(saliency_map[k][0] / scale);
00126 int min_y = static_cast<int>(saliency_map[k][1] / scale);
00127 int max_x = static_cast<int>(saliency_map[k][2] / scale);
00128 int max_y = static_cast<int>(saliency_map[k][3] / scale);
00129
00130 jsk_recognition_msgs::Rect rect;
00131 rect.x = min_x;
00132 rect.y = min_y;
00133 rect.width = max_x - min_x;
00134 rect.height = max_y - min_y;
00135 rects_msg.rects.push_back(rect);
00136
00137 for (size_t j=std::max(0, min_y); j < std::min(max_y, img.rows); j++) {
00138 for (size_t i=std::max(0, min_x); i < std::min(max_x, img.cols); i++) {
00139 objectness_img.at<float>(j, i) += objectness_values[k];
00140 }
00141 }
00142 }
00143
00144 rects_msg.header = img_msg->header;
00145 pub_rects_.publish(rects_msg);
00146
00147 pub_objectness_.publish(
00148 cv_bridge::CvImage(
00149 img_msg->header,
00150 sensor_msgs::image_encodings::TYPE_32FC1,
00151 objectness_img).toImageMsg());
00152 }
00153
00154 }
00155
00156 #include <pluginlib/class_list_macros.h>
00157 PLUGINLIB_EXPORT_CLASS(jsk_perception::Bing, nodelet::Nodelet);