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/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 
37 #include <opencv2/opencv.hpp>
38 #include <cv_bridge/cv_bridge.h>
40 
41 namespace jsk_perception
42 {
44  {
45  DiagnosticNodelet::onInit();
46  initSelfMask(*pnh_);
47  pnh_->param("max_robot_dist", max_robot_dist_, 10.0);
48  pub_ = advertise<sensor_msgs::Image>(*pnh_, "output", 1);
49  pub_camera_info_ = advertise<sensor_msgs::CameraInfo>(*pnh_, "output/info", 1);
50  onInitPostProcess();
51  }
52 
54  {
55  sub_ = pnh_->subscribe("input/camera_info", 1,
57  }
58 
60  {
61  sub_.shutdown();
62  }
63 
64  void RobotToMaskImage::infoCallback(const sensor_msgs::CameraInfo::ConstPtr& info_msg)
65  {
66  vital_checker_->poke();
67  if (info_msg) {
69  model.fromCameraInfo(info_msg);
70 
71  sensor_msgs::RegionOfInterest roi = info_msg->roi;
72  int height = roi.height ? roi.height : info_msg->height;
73  int width = roi.width ? roi.width : info_msg->width;
74  if (info_msg->binning_y > 0) {
75  height /= info_msg->binning_y;
76  }
77  if (info_msg->binning_x > 0) {
78  width /= info_msg->binning_x;
79  }
80 
81  cv::Mat mask_image = cv::Mat::zeros(height, width, CV_8UC1);
82  self_mask_->assumeFrame(info_msg->header);
83  for (int u = 0; u < width; u++) {
84  for (int v = 0; v < height; v++) {
85  // project to 3d
86  cv::Point uv(u, v);
87  cv::Point3d p = model.projectPixelTo3dRay(uv);
88  // check intersection with robot
89  // 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_));
90  if (self_mask_->getMaskIntersection(p.x * max_robot_dist_, p.y * max_robot_dist_, p.z * max_robot_dist_) == robot_self_filter::OUTSIDE) {
91  mask_image.data[mask_image.step * v + mask_image.elemSize() * u] = 255;
92  }
93  }
94  }
95  sensor_msgs::CameraInfo camera_info_msg;
96  camera_info_msg.header = info_msg->header;
97  camera_info_msg.width = width;
98  camera_info_msg.height = height;
99  camera_info_msg.distortion_model = info_msg->distortion_model;
100  camera_info_msg.roi.x_offset = 0;
101  camera_info_msg.roi.y_offset = 0;
102  camera_info_msg.roi.width = width;
103  camera_info_msg.roi.height = height;
104  camera_info_msg.roi.do_rectify = info_msg->roi.do_rectify;
105  cv::Matx33d K = model.intrinsicMatrix();
106  for (size_t i = 0; i < 3; ++i) for (size_t j = 0; j < 3; ++j) camera_info_msg.K[i * 3 + j] = K(i, j);
107  cv::Matx33d R = model.rotationMatrix();
108  for (size_t i = 0; i < 3; ++i) for (size_t j = 0; j < 3; ++j) camera_info_msg.R[i * 3 + j] = R(i, j);
109  cv::Matx34d P = model.projectionMatrix();
110  for (size_t i = 0; i < 3; ++i) for (size_t j = 0; j < 4; ++j) camera_info_msg.P[i * 4 + j] = P(i, j);
111  cv::Mat_<double> D = model.distortionCoeffs();
112  camera_info_msg.D.resize(D.rows * D.cols);
113  for (size_t i = 0; i < D.rows; ++i) for (size_t j = 0; j < D.cols; ++j) camera_info_msg.D[i * D.cols + j] = D(i, j);
116  mask_image).toImageMsg());
117  pub_camera_info_.publish(camera_info_msg);
118  }
119  }
120 
122  {
123  // genearte urdf model
124  double default_padding, default_scale;
125  pnh.param("self_see_default_padding", default_padding, 0.01);
126  pnh.param("self_see_default_scale", default_scale, 1.0);
127  std::vector<robot_self_filter::LinkInfo> links;
128 
129  if(!pnh.hasParam("self_see_links")) {
130  ROS_WARN("No links specified for self filtering.");
131  } else {
132  XmlRpc::XmlRpcValue ssl_vals;;
133  pnh.getParam("self_see_links", ssl_vals);
134  if(ssl_vals.getType() != XmlRpc::XmlRpcValue::TypeArray) {
135  ROS_WARN("Self see links need to be an array");
136  } else {
137  if(ssl_vals.size() == 0) {
138  ROS_WARN("No values in self see links array");
139  } else {
140  for(int i = 0; i < ssl_vals.size(); i++) {
142  if(ssl_vals[i].getType() != XmlRpc::XmlRpcValue::TypeStruct) {
143  ROS_WARN("Self see links entry %d is not a structure. Stopping processing of self see links",i);
144  break;
145  }
146  if(!ssl_vals[i].hasMember("name")) {
147  ROS_WARN("Self see links entry %d has no name. Stopping processing of self see links",i);
148  break;
149  }
150  li.name = std::string(ssl_vals[i]["name"]);
151  if(!ssl_vals[i].hasMember("padding")) {
152  ROS_DEBUG("Self see links entry %d has no padding. Assuming default padding of %g",i,default_padding);
153  li.padding = default_padding;
154  } else {
155  li.padding = ssl_vals[i]["padding"];
156  }
157  if(!ssl_vals[i].hasMember("scale")) {
158  ROS_DEBUG("Self see links entry %d has no scale. Assuming default scale of %g",i,default_scale);
159  li.scale = default_scale;
160  } else {
161  li.scale = ssl_vals[i]["scale"];
162  }
163  links.push_back(li);
164  }
165  }
166  }
167  }
169  }
170 }
171 
XmlRpc::XmlRpcValue::size
int size() const
jsk_perception::RobotToMaskImage::sub_
ros::Subscriber sub_
Definition: robot_to_mask_image.h:124
image_encodings.h
boost::shared_ptr
i
int i
cv_bridge::CvImage::toImageMsg
sensor_msgs::ImagePtr toImageMsg() const
jsk_perception::RobotToMaskImage::self_mask_
boost::shared_ptr< robot_self_filter::SelfMask< pcl::PointXYZ > > self_mask_
Definition: robot_to_mask_image.h:128
ros::NodeHandle::getParam
bool getParam(const std::string &key, bool &b) const
robot_self_filter::LinkInfo
jsk_perception::RobotToMaskImage::unsubscribe
virtual void unsubscribe()
Definition: robot_to_mask_image.cpp:91
ros::Subscriber::shutdown
void shutdown()
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
robot_self_filter::OUTSIDE
OUTSIDE
XmlRpc::XmlRpcValue::TypeStruct
TypeStruct
jsk_perception::RobotToMaskImage::pub_
ros::Publisher pub_
Definition: robot_to_mask_image.h:125
jsk_perception::RobotToMaskImage::tf_listener_
tf::TransformListener tf_listener_
Definition: robot_to_mask_image.h:129
class_list_macros.h
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(jsk_perception::RobotToMaskImage, nodelet::Nodelet)
jsk_perception
Definition: add_mask_image.h:48
jsk_perception::RobotToMaskImage
Definition: robot_to_mask_image.h:81
ROS_DEBUG
#define ROS_DEBUG(...)
jsk_perception::RobotToMaskImage::max_robot_dist_
double max_robot_dist_
Definition: robot_to_mask_image.h:131
robot_to_mask_image.h
node_scripts.craft.craft.model
model
Definition: craft.py:106
ros::NodeHandle::hasParam
bool hasParam(const std::string &key) const
robot_self_filter::LinkInfo::scale
double scale
ROS_WARN
#define ROS_WARN(...)
jsk_perception::RobotToMaskImage::infoCallback
virtual void infoCallback(const sensor_msgs::CameraInfo::ConstPtr &info_msg)
Definition: robot_to_mask_image.cpp:96
node_scripts.pointit.p
p
Definition: pointit.py:231
width
width
XmlRpc::XmlRpcValue::getType
const Type & getType() const
XmlRpc::XmlRpcValue::TypeArray
TypeArray
jsk_perception::RobotToMaskImage::pub_camera_info_
ros::Publisher pub_camera_info_
Definition: robot_to_mask_image.h:126
nodelet::Nodelet
robot_self_filter::LinkInfo::padding
double padding
image_geometry::PinholeCameraModel
sensor_msgs::image_encodings::MONO8
const std::string MONO8
cv_bridge.h
jsk_perception::RobotToMaskImage::onInit
virtual void onInit()
Definition: robot_to_mask_image.cpp:75
cv_bridge::CvImage
jsk_perception::RobotToMaskImage::initSelfMask
virtual void initSelfMask(const ros::NodeHandle &pnh)
Definition: robot_to_mask_image.cpp:153
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
robot_self_filter::SelfMask< pcl::PointXYZ >
height
height
robot_self_filter::LinkInfo::name
std::string name
D
D
jsk_perception::RobotToMaskImage::subscribe
virtual void subscribe()
Definition: robot_to_mask_image.cpp:85
XmlRpc::XmlRpcValue
v
GLfloat v[8][3]
info_msg
info_msg
ros::NodeHandle


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