14 #include <opencv2/highgui/highgui.hpp>
16 #include <ros/console.h>
17 #include <ros/node_handle.h>
18 #include <sensor_msgs/Image.h>
19 #include <sensor_msgs/PointCloud2.h>
32 ROS_ERROR(
"Failed to read segmenter configuration group from config file, cannot initialize Updater");
33 throw std::runtime_error(
"Failed to read segmenter configuration group from config file");
40 ros::NodeHandle nh(
"~");
41 mask_pub_ = nh.advertise<sensor_msgs::Image>(
"segmentation_masks_verbose", 1);
42 cloud_pub_ = nh.advertise<sensor_msgs::PointCloud2>(
"point_cloud_verbose", 1);
43 box_pub_ = nh.advertise<sensor_msgs::Image>(
"bounding_boxes_verbose", 1);
63 cv::Mat filtered_depth_image;
78 if (!
req.area_description.empty())
84 bool fit_supporting_entity =
req.fit_supporting_entity;
89 area_description = it_area_descr->second;
90 fit_supporting_entity =
false;
93 area_description =
req.area_description;
103 if (i_space == std::string::npos)
105 entity_id = area_description;
109 area_name = area_description.
substr(0, i_space);
110 entity_id = area_description.
substr(i_space + 1);
120 res.
error <<
"No such entity: '" << entity_id.
str() <<
"'.";
123 else if (!e->has_pose())
125 res.
error <<
"Entity: '" << entity_id.
str() <<
"' has no pose.";
134 if (fit_supporting_entity)
153 new_pose = e->pose();
158 new_pose = e->pose();
166 fitZRP(*e->visual(), new_pose, *
image, sensor_pose_const, sensor_pose);
168 ROS_DEBUG_STREAM(
"Old sensor pose: " << sensor_pose_const);
169 ROS_DEBUG_STREAM(
"New sensor pose: " << sensor_pose);
175 if (!area_name.
empty())
180 if (it == e->volumes().
end())
182 res.
error <<
"No area '" << area_name <<
"' for entity '" << entity_id.
str() <<
"'.";
189 res.
error <<
"Could not load shape of area '" << area_name <<
"' for entity '" << entity_id.
str() <<
"'.";
197 segmenter_->calculatePointsWithin(*
image, shape, shape_pose, filtered_depth_image);
202 filtered_depth_image =
image->getDepthImage().clone();
212 errc.
change(
"Kinect::Updater",
"update: preparing world model");
222 if (e->visual() || !e->has_pose() || e->convexHull().points.empty())
244 errc.
change(
"Kinect::Updater",
"update: clustering");
259 errc.
change(
"Kinect::Updater",
"update: association");
264 errc.
change(
"Kinect::Updater",
"update: remove unassociated entities");
278 ROS_INFO(
"Entity not associated and not seen in the frustum, while seeable");
281 if (d > 0 && d == d && -p_3d.
z < d)
283 ROS_INFO_STREAM(
"We can shoot a ray through the center(" << d <<
" > " << -p_3d.
z <<
"), removing entity " << e->id());
293 errc.
change(
"Kinect::Updater",
"update: store area description to segments");
294 if (!area_description.
empty())
315 ROS_ERROR_THROTTLE(1.0,
"publishSegmentationResults called but publishers are not active. Did you set verbose: true?");
320 cv::Mat visualization = rgb.clone();
321 cv::Mat box_visualization = rgb.clone();
322 for(
const auto& box : boxes)
324 cv::rectangle(box_visualization, box, cv::Scalar(0, 255, 0), 2);
327 sensor_msgs::ImagePtr box_msg = cv_bridge::CvImage(std_msgs::Header(),
"bgr8", box_visualization).toImageMsg();
328 box_msg->header.stamp = ros::Time::now();
333 cv::imwrite((temp_dir /
"visualization.png").
string(), visualization);
337 double min_val, max_val;
338 cv::minMaxLoc(filtered_depth_image, &min_val, &max_val);
343 depth_vis = cv::Mat::zeros(filtered_depth_image.size(), CV_8UC1);
348 filtered_depth_image.convertTo(depth_vis, CV_8UC1, 255.0 / max_val);
352 cv::applyColorMap(depth_vis, depth_color, cv::COLORMAP_JET);
353 cv::imwrite((temp_dir /
"visualization_depth_color.png").
string(), depth_color);
357 cv::imwrite((temp_dir /
"visualization_depth.png").
string(), depth_vis);
360 cv::imwrite((temp_dir /
"visualization_with_masks.png").
string(), visualization);
363 sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(),
"bgr8", visualization).toImageMsg();
364 msg->header.stamp = ros::Time::now();
366 typedef pcl::PointCloud<pcl::PointXYZRGB> PointCloud;
367 PointCloud::Ptr combined_cloud (
new PointCloud);
369 combined_cloud->header.frame_id =
"map";
378 pcl::PointXYZRGB pcl_point;
379 pcl_point.
x = p_map.
x;
380 pcl_point.y = p_map.
y;
381 pcl_point.z = p_map.
z;
385 combined_cloud->push_back(pcl_point);
391 pcl::PointXYZRGB pcl_point;
392 pcl_point.
x = p_map.
x;
393 pcl_point.y = p_map.
y;
394 pcl_point.z = p_map.
z;
398 combined_cloud->push_back(pcl_point);
402 sensor_msgs::PointCloud2 cloud_msg;
403 pcl::toROSMsg(*combined_cloud, cloud_msg);
404 cloud_msg.header.stamp = ros::Time::now();
405 cloud_msg.header.frame_id =
"map";