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>
33 for(
unsigned int i = 0; i < points.
size(); ++i)
37 cv::Mat filtered_depth_image;
38 segmenter_->calculatePointsWithin(
image, chull_shape, sensor_pose.
inverse() * up.
pose_map, filtered_depth_image);
48 z_values.
reserve(filtered_depth_image.rows * filtered_depth_image.cols);
49 for(
int y = 0; y < filtered_depth_image.rows; ++y)
51 for(
int x = 0; x < filtered_depth_image.cols; ++x)
53 float d = filtered_depth_image.at<
float>(y, x);
64 z_min = std::min<float>(z_min, p_map.
z);
65 z_max = std::max<float>(z_max, p_map.
z);
75 if (z_values.
empty())
return;
81 int lower_idx =
std::max(0,
static_cast<int>(z_values.
size() * 0.05));
82 int upper_idx =
std::min(
static_cast<int>(z_values.
size()-1),
83 static_cast<int>(z_values.
size() * 0.95));
85 z_min = z_values[lower_idx];
86 z_max = z_values[upper_idx];
88 double h = z_max - z_min;
109 for (
unsigned int p = 0; p < u1.
chull.
points.size(); ++p)
115 for (
unsigned int p = 0; p < u2.
chull.
points.size(); ++p)
135 ROS_INFO(
"mergoverlapping chulls: nr of updates: %lu", updates.
size());
144 for (uint i = 0; i < updates.
size(); ++i)
154 for (uint j = 0; j < updates.
size(); ++j)
165 ROS_DEBUG(
"Collition item %i with %i", i, j);
166 ROS_DEBUG(
"Item %i: xyz: %.2f, %.2f, %.2f, z_min: %.2f, z_max: %.2f", i, u1.
pose_map.
t.
getX(), u1.
pose_map.
t.
getY(), u1.
pose_map.
t.
getZ(), u1.
chull.
z_min, u1.
chull.
z_max);
167 ROS_DEBUG(
"Item %i: xyz: %.2f, %.2f, %.2f, z_min: %.2f, z_max: %.2f", j, u2.
pose_map.
t.
getX(), u2.
pose_map.
t.
getY(), u2.
pose_map.
t.
getZ(), u2.
chull.
z_min, u2.
chull.
z_max);
168 collission_map[i].push_back(j);
175 for (uint i = 0; i < updates.
size(); ++i)
188 ROS_DEBUG_COND(it == collission_map[i].begin(),
"Merging entity %i and xx", i);
189 ROS_DEBUG(
"Merging entity %i and %i", i, *it);
195 ROS_DEBUG(
"Adding update of entity %i", i);
199 ROS_DEBUG(
"Skipping update %i because of collision", i);
215 ROS_ERROR(
"Failed to read segmenter configuration group from config file, cannot initialize Updater");
216 throw std::runtime_error(
"Failed to read segmenter configuration group from config file");
223 ros::NodeHandle nh(
"~");
224 mask_pub_ = nh.advertise<sensor_msgs::Image>(
"segmentation_masks", 1);
225 cloud_pub_ = nh.advertise<sensor_msgs::PointCloud2>(
"point_cloud_ooo", 1);
244 cv::Mat filtered_depth_image;
245 cv::Mat filtered_rgb_image;
250 const cv::Mat& depth =
image->getDepthImage();
251 const cv::Mat& rgb =
image->getRGBImage();
260 if (!
req.area_description.empty())
266 bool fit_supporting_entity =
req.fit_supporting_entity;
271 area_description = it_area_descr->second;
272 fit_supporting_entity =
false;
275 area_description =
req.area_description;
285 if (i_space == std::string::npos)
287 entity_id = area_description;
291 area_name = area_description.
substr(0, i_space);
292 entity_id = area_description.
substr(i_space + 1);
302 res.
error <<
"No such entity: '" << entity_id.
str() <<
"'.";
305 else if (!e->has_pose())
307 res.
error <<
"Entity: '" << entity_id.
str() <<
"' has no pose.";
316 if (fit_supporting_entity)
335 new_pose = e->pose();
340 new_pose = e->pose();
348 fitZRP(*e->visual(), new_pose, *
image, sensor_pose_const, sensor_pose);
350 ROS_DEBUG_STREAM(
"Old sensor pose: " << sensor_pose_const);
351 ROS_DEBUG_STREAM(
"New sensor pose: " << sensor_pose);
357 if (!area_name.
empty())
362 if (it == e->volumes().
end())
364 res.
error <<
"No area '" << area_name <<
"' for entity '" << entity_id.
str() <<
"'.";
371 res.
error <<
"Could not load shape of area '" << area_name <<
"' for entity '" << entity_id.
str() <<
"'.";
379 segmenter_->calculatePointsWithin(*
image, shape, shape_pose, filtered_depth_image);
384 filtered_depth_image =
image->getDepthImage().clone();
397 segmenter_->removeBackground(filtered_depth_image, world_updated, cam_model, sensor_pose,
req.background_padding);
406 if (e->visual() || !e->has_pose() || e->convexHull().points.empty())
428 filtered_rgb_image =
segmenter_->preprocessRGBForSegmentation(rgb, filtered_depth_image);
466 if (p_2d.x < 0 || p_2d.y < 0 || p_2d.x >= depth.cols || p_2d.y >= depth.rows)
472 ROS_INFO(
"Entity not associated and not seen in the frustum, while seeable");
474 float d = depth.at<
float>(p_2d);
475 if (d > 0 && d == d && -p_3d.
z < d)
477 ROS_INFO_STREAM(
"We can shoot a ray through the center(" << d <<
" > " << -p_3d.
z <<
"), removing entity " << e->id());
488 if (!area_description.
empty())