robot_to_mask_image.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/o2r 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 <opencv2/opencv.hpp>
38 #include <cv_bridge/cv_bridge.h>
40 
41 namespace jsk_perception
42 {
44  {
45  DiagnosticNodelet::onInit();
47  pnh_->param("max_robot_dist", max_robot_dist_, 10.0);
48  pub_ = advertise<sensor_msgs::Image>(*pnh_, "output", 1);
49  }
50 
52  {
53  sub_ = pnh_->subscribe("input/camera_info", 1,
55  }
56 
58  {
59  sub_.shutdown();
60  }
61 
62  void RobotToMaskImage::infoCallback(const sensor_msgs::CameraInfo::ConstPtr& info_msg)
63  {
64  vital_checker_->poke();
65  if (info_msg) {
67  model.fromCameraInfo(info_msg);
68 
69  cv::Mat mask_image = cv::Mat::zeros(info_msg->height, info_msg->width, CV_8UC1);
70  self_mask_->assumeFrame(info_msg->header);
71  for (int u = 0; u < info_msg->width; u++) {
72  for (int v = 0; v < info_msg->height; v++) {
73  // project to 3d
74  cv::Point uv(u, v);
75  cv::Point3d p = model.projectPixelTo3dRay(uv);
76  // check intersection with robot
77  // ROS_INFO_STREAM("uv( " << u << " , " << v << " ) intersection: " << self_mask_->getMaskIntersection(p.x * max_robot_dist_, p.y * max_robot_dist_, p.z * max_robot_dist_));
78  if (self_mask_->getMaskIntersection(p.x * max_robot_dist_, p.y * max_robot_dist_, p.z * max_robot_dist_) == robot_self_filter::OUTSIDE) {
79  mask_image.data[mask_image.step * v + mask_image.elemSize() * u] = 255;
80  }
81  }
82  }
83  pub_.publish(cv_bridge::CvImage(info_msg->header,
85  mask_image).toImageMsg());
86  }
87  }
88 
90  {
91  // genearte urdf model
92  double default_padding, default_scale;
93  pnh.param("self_see_default_padding", default_padding, 0.01);
94  pnh.param("self_see_default_scale", default_scale, 1.0);
95  std::vector<robot_self_filter::LinkInfo> links;
96 
97  if(!pnh.hasParam("self_see_links")) {
98  ROS_WARN("No links specified for self filtering.");
99  } else {
100  XmlRpc::XmlRpcValue ssl_vals;;
101  pnh.getParam("self_see_links", ssl_vals);
102  if(ssl_vals.getType() != XmlRpc::XmlRpcValue::TypeArray) {
103  ROS_WARN("Self see links need to be an array");
104  } else {
105  if(ssl_vals.size() == 0) {
106  ROS_WARN("No values in self see links array");
107  } else {
108  for(int i = 0; i < ssl_vals.size(); i++) {
109  robot_self_filter::LinkInfo li;
110  if(ssl_vals[i].getType() != XmlRpc::XmlRpcValue::TypeStruct) {
111  ROS_WARN("Self see links entry %d is not a structure. Stopping processing of self see links",i);
112  break;
113  }
114  if(!ssl_vals[i].hasMember("name")) {
115  ROS_WARN("Self see links entry %d has no name. Stopping processing of self see links",i);
116  break;
117  }
118  li.name = std::string(ssl_vals[i]["name"]);
119  if(!ssl_vals[i].hasMember("padding")) {
120  ROS_DEBUG("Self see links entry %d has no padding. Assuming default padding of %g",i,default_padding);
121  li.padding = default_padding;
122  } else {
123  li.padding = ssl_vals[i]["padding"];
124  }
125  if(!ssl_vals[i].hasMember("scale")) {
126  ROS_DEBUG("Self see links entry %d has no scale. Assuming default scale of %g",i,default_scale);
127  li.scale = default_scale;
128  } else {
129  li.scale = ssl_vals[i]["scale"];
130  }
131  links.push_back(li);
132  }
133  }
134  }
135  }
136  self_mask_ = boost::shared_ptr<robot_self_filter::SelfMask<pcl::PointXYZ> >(new robot_self_filter::SelfMask<pcl::PointXYZ>(tf_listener_, links));
137  }
138 }
139 
PLUGINLIB_EXPORT_CLASS(jsk_perception::RobotToMaskImage, nodelet::Nodelet)
void publish(const boost::shared_ptr< M > &message) const
int size() const
cv::Point3d projectPixelTo3dRay(const cv::Point2d &uv_rect) const
boost::shared_ptr< robot_self_filter::SelfMask< pcl::PointXYZ > > self_mask_
Type const & getType() const
#define ROS_WARN(...)
bool fromCameraInfo(const sensor_msgs::CameraInfo &msg)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
boost::shared_ptr< ros::NodeHandle > pnh_
jsk_topic_tools::VitalChecker::Ptr vital_checker_
virtual void infoCallback(const sensor_msgs::CameraInfo::ConstPtr &info_msg)
bool getParam(const std::string &key, std::string &s) const
GLfloat v[8][3]
bool hasParam(const std::string &key) const
virtual void initSelfMask(const ros::NodeHandle &pnh)
sensor_msgs::ImagePtr toImageMsg() const
#define ROS_DEBUG(...)


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Mon May 3 2021 03:03:27