1 #include <cv_bridge/cv_bridge.h>
15 #include <opencv2/highgui/highgui.hpp>
16 #include <pcl/point_cloud.h>
18 #include <ros/console.h>
19 #include <ros/node_handle.h>
20 #include <sensor_msgs/Image.h>
21 #include <sensor_msgs/PointCloud2.h>
35 ROS_ERROR(
"Failed to read segmenter configuration group from config file, cannot initialize Updater");
36 throw std::runtime_error(
"Failed to read segmenter configuration group from config file");
43 ros::NodeHandle nh(
"~");
44 mask_pub_ = nh.advertise<sensor_msgs::Image>(
"segmentation_masks_verbose", 1);
45 cloud_pub_ = nh.advertise<sensor_msgs::PointCloud2>(
"point_cloud_verbose", 1);
46 box_pub_ = nh.advertise<sensor_msgs::Image>(
"bounding_boxes_verbose", 1);
66 cv::Mat filtered_depth_image;
81 if (!
req.area_description.empty())
87 bool fit_supporting_entity =
req.fit_supporting_entity;
92 area_description = it_area_descr->second;
93 fit_supporting_entity =
false;
96 area_description =
req.area_description;
106 if (i_space == std::string::npos)
108 entity_id = area_description;
112 area_name = area_description.
substr(0, i_space);
113 entity_id = area_description.
substr(i_space + 1);
123 res.
error <<
"No such entity: '" << entity_id.
str() <<
"'.";
126 else if (!e->has_pose())
128 res.
error <<
"Entity: '" << entity_id.
str() <<
"' has no pose.";
137 if (fit_supporting_entity)
156 new_pose = e->pose();
161 new_pose = e->pose();
169 fitZRP(*e->visual(), new_pose, *
image, sensor_pose_const, sensor_pose);
171 ROS_DEBUG_STREAM(
"Old sensor pose: " << sensor_pose_const);
172 ROS_DEBUG_STREAM(
"New sensor pose: " << sensor_pose);
178 if (!area_name.
empty())
183 if (it == e->volumes().
end())
185 res.
error <<
"No area '" << area_name <<
"' for entity '" << entity_id.
str() <<
"'.";
192 res.
error <<
"Could not load shape of area '" << area_name <<
"' for entity '" << entity_id.
str() <<
"'.";
200 segmenter_->calculatePointsWithin(*
image, shape, shape_pose, filtered_depth_image);
205 filtered_depth_image =
image->getDepthImage().clone();
215 errc.
change(
"Kinect::Updater",
"update: preparing world model");
225 if (e->visual() || !e->has_pose() || e->convexHull().points.empty())
247 errc.
change(
"Kinect::Updater",
"update: clustering");
262 errc.
change(
"Kinect::Updater",
"update: association");
267 errc.
change(
"Kinect::Updater",
"update: remove unassociated entities");
281 ROS_INFO(
"Entity not associated and not seen in the frustum, while seeable");
284 if (d > 0 && d == d && -p_3d.
z < d)
286 ROS_INFO_STREAM(
"We can shoot a ray through the center(" << d <<
" > " << -p_3d.
z <<
"), removing entity " << e->id());
296 errc.
change(
"Kinect::Updater",
"update: store area description to segments");
297 if (!area_description.
empty())
318 ROS_ERROR_THROTTLE(1.0,
"publishSegmentationResults called but publishers are not active. Did you set verbose: true?");
323 cv::Mat visualization = rgb.clone();
324 cv::Mat box_visualization = rgb.clone();
325 for(
const auto& box : boxes)
327 cv::rectangle(box_visualization, box, cv::Scalar(0, 255, 0), 2);
330 sensor_msgs::ImagePtr box_msg = cv_bridge::CvImage(std_msgs::Header(),
"bgr8", box_visualization).toImageMsg();
331 box_msg->header.stamp = ros::Time::now();
336 cv::imwrite((temp_dir /
"visualization.png").
string(), visualization);
340 double min_val, max_val;
341 cv::minMaxLoc(filtered_depth_image, &min_val, &max_val);
346 depth_vis = cv::Mat::zeros(filtered_depth_image.size(), CV_8UC1);
351 filtered_depth_image.convertTo(depth_vis, CV_8UC1, 255.0 / max_val);
355 cv::applyColorMap(depth_vis, depth_color, cv::COLORMAP_JET);
356 cv::imwrite((temp_dir /
"visualization_depth_color.png").
string(), depth_color);
360 cv::imwrite((temp_dir /
"visualization_depth.png").
string(), depth_vis);
363 cv::imwrite((temp_dir /
"visualization_with_masks.png").
string(), visualization);
366 sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(),
"bgr8", visualization).toImageMsg();
367 msg->header.stamp = ros::Time::now();
369 typedef pcl::PointCloud<pcl::PointXYZRGB> PointCloud;
370 PointCloud::Ptr combined_cloud (
new PointCloud);
372 combined_cloud->header.frame_id =
"map";
381 pcl::PointXYZRGB pcl_point;
382 pcl_point.
x = p_map.
x;
383 pcl_point.y = p_map.
y;
384 pcl_point.z = p_map.
z;
388 combined_cloud->push_back(pcl_point);
394 pcl::PointXYZRGB pcl_point;
395 pcl_point.
x = p_map.
x;
396 pcl_point.y = p_map.
y;
397 pcl_point.z = p_map.
z;
401 combined_cloud->push_back(pcl_point);
405 sensor_msgs::PointCloud2 cloud_msg;
406 pcl::toROSMsg(*combined_cloud, cloud_msg);
407 cloud_msg.header.stamp = ros::Time::now();
408 cloud_msg.header.frame_id =
"map";