11#include " semantic_inference_ros/pointcloud_projection.h"
22
33#include < config_utilities/config.h>
4+ #include < semantic_inference/logging.h>
45
56#include < optional>
67
910#include < rclcpp/node.hpp>
1011#include < sensor_msgs/point_cloud2_iterator.hpp>
1112
12- #include " semantic_inference_ros/ros_log_sink.h"
13-
1413namespace semantic_inference {
1514
15+ using sensor_msgs::PointCloud2ConstIterator;
16+ using sensor_msgs::PointCloud2Iterator;
1617using sensor_msgs::msg::CameraInfo;
1718using sensor_msgs::msg::Image;
1819using sensor_msgs::msg::PointCloud2;
@@ -49,9 +50,9 @@ struct OutputPosIter {
4950 private:
5051 // techinically we could probably just have one iterator, but (small) chance that
5152 // someone sends a non-contiguous pointcloud
52- sensor_msgs:: PointCloud2Iterator<float > x_iter_;
53- sensor_msgs:: PointCloud2Iterator<float > y_iter_;
54- sensor_msgs:: PointCloud2Iterator<float > z_iter_;
53+ PointCloud2Iterator<float > x_iter_;
54+ PointCloud2Iterator<float > y_iter_;
55+ PointCloud2Iterator<float > z_iter_;
5556};
5657
5758struct InputPosIter {
@@ -71,9 +72,9 @@ struct InputPosIter {
7172 private:
7273 // techinically we could probably just have one iterator, but (small) chance that
7374 // someone sends a non-contiguous pointcloud
74- sensor_msgs:: PointCloud2ConstIterator<float > x_iter_;
75- sensor_msgs:: PointCloud2ConstIterator<float > y_iter_;
76- sensor_msgs:: PointCloud2ConstIterator<float > z_iter_;
75+ PointCloud2ConstIterator<float > x_iter_;
76+ PointCloud2ConstIterator<float > y_iter_;
77+ PointCloud2ConstIterator<float > z_iter_;
7778};
7879
7980struct InputLabelIterBase {
@@ -93,7 +94,7 @@ struct InputLabelIter : InputLabelIterBase {
9394 uint32_t get () const override { return *iter_; }
9495
9596 private:
96- sensor_msgs:: PointCloud2ConstIterator<T> iter_;
97+ PointCloud2ConstIterator<T> iter_;
9798};
9899
99100std::unique_ptr<InputLabelIterBase> iterFromFields (const PointCloud2& cloud,
@@ -222,8 +223,8 @@ struct LabelImageAdapter {
222223void recolorCloud (PointCloud2& output,
223224 const ImageRecolor& recolor,
224225 uint32_t unknown_label) {
225- auto labels = sensor_msgs:: PointCloud2ConstIterator<uint32_t >(output, " label" );
226- auto colors = sensor_msgs:: PointCloud2Iterator<uint8_t >(output, " rgba" );
226+ auto labels = PointCloud2ConstIterator<uint32_t >(output, " label" );
227+ auto colors = PointCloud2Iterator<uint8_t >(output, " rgba" );
227228 while (labels != labels.end ()) {
228229 const auto unknown = static_cast <uint32_t >(*labels) == unknown_label;
229230 const auto & color = unknown ? recolor.default_color : recolor.getColor (*labels);
@@ -259,6 +260,9 @@ void declare_config(ProjectionConfig& config) {
259260 field (config.override_labels , " override_labels" );
260261 field (config.allowed_labels , " allowed_labels" );
261262 field (config.input_remapping , " input_remapping" );
263+ field (config.out_of_view_alpha , " out_of_view_alpha" );
264+
265+ checkInRange<uint16_t >(config.out_of_view_alpha , 0 , 255 , " out_of_view_alpha" );
262266}
263267
264268std::optional<uint32_t > ProjectionConfig::remapInput (
@@ -274,22 +278,27 @@ std::optional<uint32_t> ProjectionConfig::remapInput(
274278
275279bool projectSemanticImage (const ProjectionConfig& config,
276280 const CameraInfo& intrinsics,
277- const cv::Mat& image ,
281+ const cv::Mat& labels ,
278282 const PointCloud2& cloud,
279283 const Eigen::Isometry3f& image_T_cloud,
280284 PointCloud2& output,
285+ const cv::Mat& color_image,
281286 const ImageRecolor* recolor) {
282287 image_geometry::PinholeCameraModel model;
283288 model.fromCameraInfo (intrinsics);
284289
285290 auto pos_in_iter = InputPosIter (cloud);
286- const LabelImageAdapter img_wrapper (image );
291+ const LabelImageAdapter img_wrapper (labels );
287292 // iterator over label field in input pointcloud if it exists
288293 InputLabelAdapter label_in_iter (cloud, config.input_label_fieldname );
289294
290- initOutput (cloud, output, recolor != nullptr );
295+ initOutput (cloud, output, recolor != nullptr || !color_image. empty () );
291296 auto pos_out_iter = OutputPosIter (output);
292- auto label_out_iter = sensor_msgs::PointCloud2Iterator<uint32_t >(output, " label" );
297+ auto label_out_iter = PointCloud2Iterator<uint32_t >(output, " label" );
298+ std::unique_ptr<PointCloud2Iterator<uint8_t >> color_out_iter;
299+ if (!color_image.empty ()) {
300+ color_out_iter = std::make_unique<PointCloud2Iterator<uint8_t >>(output, " rgba" );
301+ }
293302
294303 while (pos_in_iter) {
295304 const Eigen::Vector3f p_cloud = *pos_in_iter;
@@ -304,13 +313,28 @@ bool projectSemanticImage(const ProjectionConfig& config,
304313 v = std::round (pixel.y );
305314 }
306315
307- const auto in_view = u >= 0 && u < image .cols && v >= 0 && v < image .rows ;
316+ const auto in_view = u >= 0 && u < labels .cols && v >= 0 && v < labels .rows ;
308317 const uint32_t label_in =
309318 config.remapInput (*label_in_iter).value_or (config.unknown_label );
310319 if (in_view) {
311320 *label_out_iter = config.isOverride (label_in) ? label_in : img_wrapper (v, u);
321+ if (color_out_iter) {
322+ auto & curr_color_out = *color_out_iter;
323+ const auto & pixel = color_image.at <cv::Vec3b>(v, u);
324+ curr_color_out[0 ] = pixel[0 ];
325+ curr_color_out[1 ] = pixel[1 ];
326+ curr_color_out[2 ] = pixel[2 ];
327+ curr_color_out[3 ] = 255u ;
328+ }
312329 } else {
313330 *label_out_iter = config.isAllowed (label_in) ? label_in : config.unknown_label ;
331+ if (color_out_iter) {
332+ auto & curr_color_out = *color_out_iter;
333+ curr_color_out[0 ] = 0 ;
334+ curr_color_out[1 ] = 0 ;
335+ curr_color_out[2 ] = 0 ;
336+ curr_color_out[3 ] = config.out_of_view_alpha ;
337+ }
314338 }
315339
316340 if (!in_view && config.discard_out_of_view ) {
@@ -323,9 +347,12 @@ bool projectSemanticImage(const ProjectionConfig& config,
323347 ++pos_out_iter;
324348 ++label_in_iter;
325349 ++label_out_iter;
350+ if (color_out_iter) {
351+ ++(*color_out_iter);
352+ }
326353 }
327354
328- if (recolor) {
355+ if (recolor && color_image. empty () ) {
329356 recolorCloud (output, *recolor, config.unknown_label );
330357 }
331358
0 commit comments