forked from fmrico/perception_asr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetectionTo3DfromDepthNode.cpp
More file actions
128 lines (103 loc) · 4.49 KB
/
DetectionTo3DfromDepthNode.cpp
File metadata and controls
128 lines (103 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2023 (c) Intelligent Robotics Lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <memory>
#include "perception_asr_stressoverflow/DetectionTo3DfromDepthNode.hpp"
#include "sensor_msgs/msg/image.hpp"
#include "vision_msgs/msg/detection2_d_array.hpp"
#include "vision_msgs/msg/detection3_d_array.hpp"
#include "message_filters/subscriber.h"
#include "message_filters/sync_policies/approximate_time.h"
#include "image_geometry/pinhole_camera_model.h"
#include "cv_bridge/cv_bridge.h"
#include "depth_image_proc/depth_traits.hpp"
#include "rclcpp/rclcpp.hpp"
namespace perception_asr_stressoverflow
{
using std::placeholders::_1;
using std::placeholders::_2;
DetectionTo3DfromDepthNode::DetectionTo3DfromDepthNode()
: Node("detection_to_3d_from_depth_node")
{
depth_sub_ = std::make_shared<message_filters::Subscriber<sensor_msgs::msg::Image>>(
this, "input_depth", rclcpp::SensorDataQoS().reliable().get_rmw_qos_profile());
detection_sub_ =
std::make_shared<message_filters::Subscriber<vision_msgs::msg::Detection2DArray>>(
this, "input_detection_2d", rclcpp::SensorDataQoS().reliable().get_rmw_qos_profile());
sync_ = std::make_shared<message_filters::Synchronizer<MySyncPolicy>>(
MySyncPolicy(10), *depth_sub_, *detection_sub_);
sync_->registerCallback(std::bind(&DetectionTo3DfromDepthNode::callback_sync, this, _1, _2));
info_sub_ = create_subscription<sensor_msgs::msg::CameraInfo>(
"camera_info", 1, std::bind(&DetectionTo3DfromDepthNode::callback_info, this, _1));
detection_pub_ = create_publisher<vision_msgs::msg::Detection3DArray>(
"output_detection_3d", rclcpp::SensorDataQoS().reliable());
}
void
DetectionTo3DfromDepthNode::callback_info(sensor_msgs::msg::CameraInfo::UniquePtr msg)
{
RCLCPP_INFO(get_logger(), "Camera info received");
model_ = std::make_shared<image_geometry::PinholeCameraModel>();
model_->fromCameraInfo(*msg);
info_sub_ = nullptr;
}
void
DetectionTo3DfromDepthNode::callback_sync(
const sensor_msgs::msg::Image::ConstSharedPtr & image_msg,
const vision_msgs::msg::Detection2DArray::ConstSharedPtr & detection_msg)
{
if (model_ == nullptr) {
RCLCPP_WARN(get_logger(), "Camera Model not yet available");
return;
}
if (image_msg->encoding != "16UC1" && image_msg->encoding != "32FC1") {
RCLCPP_ERROR(get_logger(), "The image type has not depth info");
return;
}
if (detection_pub_->get_subscription_count() > 0) {
vision_msgs::msg::Detection3DArray detections_3d_msg;
detections_3d_msg.header = detection_msg->header;
cv_bridge::CvImagePtr cv_depth_ptr = cv_bridge::toCvCopy(*image_msg, image_msg->encoding);
for (const auto & detection : detection_msg->detections) {
vision_msgs::msg::Detection3D detection_3d_msg;
detection_3d_msg.results = detection.results;
float depth;
if (image_msg->encoding == "16UC1") {
depth = depth_image_proc::DepthTraits<uint16_t>::toMeters(
cv_depth_ptr->image.at<uint16_t>(
cv::Point2d(detection.bbox.center.position.x, detection.bbox.center.position.y)));
} else if (image_msg->encoding == "32FC1") {
depth = cv_depth_ptr->image.at<float>(
cv::Point2d(detection.bbox.center.position.x, detection.bbox.center.position.y));
} else {
RCLCPP_ERROR(get_logger(), "Format not recognized");
}
if (std::isnan(depth)) {
continue;
}
cv::Point3d ray = model_->projectPixelTo3dRay(
model_->rectifyPoint(
cv::Point2d(
detection.bbox.center.position.x, detection.bbox.center.position.y)));
ray = ray / ray.z;
cv::Point3d point = ray * depth;
detection_3d_msg.bbox.center.position.x = point.x;
detection_3d_msg.bbox.center.position.y = point.y;
detection_3d_msg.bbox.center.position.z = point.z;
detections_3d_msg.detections.push_back(detection_3d_msg);
}
if (!detections_3d_msg.detections.empty()) {
detection_pub_->publish(detections_3d_msg);
}
}
}
} // namespace perception_asr_stressoverflow