4 #include <opencv2/core/mat.hpp> 
    5 #include <opencv2/core/types.hpp> 
   10 #include <rgbd_msgs/Project2DTo3D.h> 
   12 #include <ros/console.h> 
   13 #include <ros/duration.h> 
   15 #include <ros/master.h> 
   16 #include <ros/names.h> 
   17 #include <ros/node_handle.h> 
   18 #include <ros/service_client.h> 
   21 #include <sensor_msgs/RegionOfInterest.h> 
   25 #include <boost/circular_buffer.hpp> 
   36         if (req.stamp == ros::Time(0))
 
   42                 if ( (*it)->getTimestamp() <= req.stamp.toSec())
 
   50         ROS_ERROR(
"I could not find an images in my image buffer for timestamp %.2f", req.stamp.toSec());
 
   54     for (
const sensor_msgs::RegionOfInterest& roi : req.rois)
 
   56         const cv::Mat& depth = last_image->getDepthImage();
 
   58         cv::Rect roi_rgb(
static_cast<int>(roi.x_offset), 
static_cast<int>(roi.y_offset),
 
   59                          static_cast<int>(roi.width), 
static_cast<int>(roi.height));
 
   60         float rgb_depth_width_ratio = 
static_cast<float>(depth.cols) / 
static_cast<float>(last_image->getRGBImage().cols);
 
   61         cv::Rect roi_depth(rgb_depth_width_ratio * roi_rgb.tl(), rgb_depth_width_ratio * roi_rgb.br());
 
   62         cv::Point roi_depth_center = 0.5 * (roi_depth.tl() + roi_depth.br());
 
   64         cv::Rect roi_depth_capped(cv::Point(
std::max(0, roi_depth.x),
 
   66                                   cv::Point(
std::min(depth.cols - 1, roi_depth.br().x),
 
   67                                             std::min(depth.rows - 1, roi_depth.br().y)));
 
   69         cv::Mat depth_roi_capped = depth(roi_depth_capped);
 
   75         for (
int j = 0; j < depth_roi_capped.cols * depth_roi_capped.rows; ++j)
 
   77             float d = depth_roi_capped.
at<
float>(j);
 
   82         geometry_msgs::PointStamped point_msg;
 
   83         point_msg.header.frame_id = last_image->getFrameId();
 
   84         point_msg.header.stamp.fromSec(last_image->getTimestamp());
 
   87             ROS_ERROR(
"All depths within ROI are invalid! We will send a NAN point");
 
   88             point_msg.point.x = point_msg.point.y = point_msg.point.z = 
static_cast<double>(NAN);
 
   93             float median_depth = depths[depths.
size() / 2];
 
   98             rgbd::View view(*last_image, last_image->getDepthImage().cols);
 
  103             ROS_INFO(
"Pose (%.2f, %.2f, %.2f)", pos.
x, pos.
y, pos.
z);
 
  107         res.points.push_back(point_msg);
 
  115 int main(
int argc, 
char **argv)
 
  117     ros::init(argc, argv, 
"get_3d_point_from_image_roi");
 
  119     ros::NodeHandle nh_private(
"~");
 
  122     nh_private.getParam(
"rate", rate);
 
  127     client.
initialize(ros::names::resolve(
"rgbd"));
 
  132     ros::ServiceServer srv_project_2d_to_3d = nh.advertiseService(
"project_2d_to_3d", 
srvGet3dPointFromROI);
 
  133     ros::Time last_image_stamp;
 
  137     ros::WallTime last_master_check = ros::WallTime::now();
 
  142         if (ros::WallTime::now() >= last_master_check + ros::WallDuration(1))
 
  144             last_master_check = ros::WallTime::now();
 
  145             if (!ros::master::check())
 
  147                 ROS_FATAL(
"Lost connection to master");
 
  158                 ROS_DEBUG(
"New image added to buffer");