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>
45 #include <opencv2/opencv.hpp>
46 #include <opencv2/saliency.hpp>
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);
60 std::string training_path;
63 rospack::Package *
p =
rp.get_pkg(
"jsk_perception");
65 ROS_ERROR(
"Package path of 'jsk_perception' does not found");
68 training_path =
p->path + std::string(
"/trained_data/ObjectnessTrainedModel");
76 if (
rp.
find(
"jsk_perception", path) ==
true) {
77 training_path = path + std::string(
"/trained_data/ObjectnessTrainedModel");
79 ROS_ERROR(
"Package path of 'jsk_perception' does not found");
83 if (!boost::filesystem::exists(training_path)) {
84 ROS_ERROR(
"Training data path '%s' does not exist", training_path.c_str());
88 binger_ =
new cv::saliency::ObjectnessBING();
89 binger_->setTrainingPath(training_path);
97 jsk_topic_tools::warnNoRemap(names);
106 const sensor_msgs::Image::ConstPtr&
img_msg)
110 cv::Mat
img = cv_ptr->image;
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)));
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());
127 jsk_recognition_msgs::RectArray rects_msg;
128 cv::Mat objectness_img = cv::Mat(
img.rows,
img.cols, CV_32FC1);
130 for (
size_t k=0;
k < saliency_map.size();
k++) {
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);
139 jsk_recognition_msgs::Rect rect;
142 rect.width = max_x - min_x;
143 rect.height = max_y - min_y;
144 rects_msg.rects.push_back(rect);
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];
157 rects_msg.header =
img_msg->header;