Skip to content

Commit d24f6ed

Browse files
committed
Use tf at depth_image_projector
Use tf to cut unnecessary dependency with realsense2_camera and make depth_image_projector enable to be used on not only realsense2 but other color & depth image devices.
1 parent 0c2b75b commit d24f6ed

File tree

5 files changed

+56
-19
lines changed

5 files changed

+56
-19
lines changed

‎opengl_ros/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ find_package(catkin REQUIRED COMPONENTS
1414
image_transport
1515
cv_bridge
1616
opengl_ros_lib
17-
realsense2_camera
17+
tf
1818
)
1919

2020
## System dependencies are found with CMake's conventions

‎opengl_ros/launch/depth_image_projection.launch‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141

4242
<node pkg="opengl_ros" type="depth_image_projector"
4343
name="depth_image_projector" output="screen">
44+
<param name="color_frame_id" value="d435_color_optical"/>
45+
<param name="depth_frame_id" value="d435_depth_optical"/>
46+
<param name="tf_wait_duration" value="0.1"/>
47+
4448
<param name="colorWidth" value="$(arg depth_width)"/>
4549
<param name="colorHeight" value="$(arg depth_height)"/>
4650
<param name="depthWidth" value="$(arg depth_width)"/>

‎opengl_ros/package.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
<build_depend>image_transport</build_depend>
4646
<build_depend>cv_bridge</build_depend>
4747
<build_depend>opengl_ros_lib</build_depend>
48+
<build_depend>tf</build_depend>
4849
<run_depend>opengl_ros_lib</run_depend>
50+
<run_depend>tf</run_depend>
4951

5052

5153
<!-- The export tag contains other, unspecified, tags -->

‎opengl_ros/src/depth_image_projector_nodecore.cpp‎

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ DepthImageProjectorNode::DepthImageProjectorNode(const ros::NodeHandle& nh, cons
1313
colorSubscriber_ = it_.subscribeCamera("color_in" , 1, &DepthImageProjectorNode::colorCallback, this);
1414
depthSubscriber_ = it_.subscribeCamera("depth_in" , 1, &DepthImageProjectorNode::depthCallback, this);
1515

16-
//TODO change to use TF and remove dependency on realsense2_camera package
17-
depthToColorSubscriber_ = nh_.subscribe<realsense2_camera::Extrinsics>("depth_to_color" , 1, &DepthImageProjectorNode::depthToColorCallback, this);
16+
// frame ids for tf which associates color frame with depth frame
17+
nh_.param<std::string>("color_frame_id", color_frame_id_, "d435_color_optical");
18+
nh_.param<std::string>("depth_frame_id", depth_frame_id_, "d435_depth_optical");
19+
nh_.param<double>("tf_wait_duration", tf_wait_duration_, 0.1);
1820

1921
int colorWidth, colorHeight;
2022
nh_.param<int>("colorWidth" , colorWidth , 640);
@@ -92,7 +94,9 @@ void DepthImageProjectorNode::depthCallback(const sensor_msgs::Image::ConstPtr&
9294
return;
9395
}
9496

95-
if (!depthToColorArrived_)
97+
// Update & check whether depthToColor is valid.
98+
bool is_conversion_valid = updateDepthToColor();
99+
if (!is_conversion_valid)
96100
{
97101
ROS_WARN_STREAM("extrinsics parameter not arrivied yet");
98102
return;
@@ -122,21 +126,42 @@ void DepthImageProjectorNode::depthCallback(const sensor_msgs::Image::ConstPtr&
122126
imagePublisher_.publish(outImage.toImageMsg());
123127
}
124128

125-
void DepthImageProjectorNode::depthToColorCallback(const realsense2_camera::Extrinsics::ConstPtr& depthToColorMsg)
129+
bool DepthImageProjectorNode::updateDepthToColor()
126130
{
127-
auto& r = depthToColorMsg->rotation;
128-
auto& t = depthToColorMsg->translation;
129-
latestDepthToColor_ = {
130-
static_cast<float>(r[0]), static_cast<float>(r[1]), static_cast<float>(r[2]), 0,
131-
static_cast<float>(r[3]), static_cast<float>(r[4]), static_cast<float>(r[5]), 0,
132-
static_cast<float>(r[6]), static_cast<float>(r[7]), static_cast<float>(r[8]), 0,
133-
static_cast<float>(t[0]), static_cast<float>(t[1]), static_cast<float>(t[2]), 1,
134-
}; //column major order
135-
136-
depthToColorArrived_ = true;
131+
// Calculation is performed only once since depthToColor does not change.
132+
if (depthToColorArrived_)
133+
{
134+
return true;
135+
}
136+
137+
try
138+
{
139+
if (tfListener_.waitForTransform(color_frame_id_, depth_frame_id_, ros::Time(0), ros::Duration(tf_wait_duration_)))
140+
{
141+
tf::StampedTransform transform;
142+
tfListener_.lookupTransform(color_frame_id_, depth_frame_id_, ros::Time(0), transform);
143+
144+
const tf::Matrix3x3& R = transform.getBasis();
145+
const tf::Vector3& t = transform.getOrigin();
146+
147+
latestDepthToColor_ = {
148+
static_cast<float>(R[0][0]), static_cast<float>(R[1][0]), static_cast<float>(R[2][0]), 0.0,
149+
static_cast<float>(R[0][1]), static_cast<float>(R[1][1]), static_cast<float>(R[2][1]), 0.0,
150+
static_cast<float>(R[0][2]), static_cast<float>(R[1][2]), static_cast<float>(R[2][2]), 0.0,
151+
static_cast<float>(t[0]), static_cast<float>(t[1]), static_cast<float>(t[2]), 1.0,
152+
}; //column major order
153+
depthToColorArrived_ = true;
154+
}
155+
}
156+
catch (tf::TransformException& ex)
157+
{
158+
ROS_WARN("Transform Exception in updateDepthToColor(). %s", ex.what());
159+
}
160+
161+
return depthToColorArrived_;
137162
}
138163

139164
void DepthImageProjectorNode::run()
140165
{
141166
ros::spin();
142-
}
167+
}

‎opengl_ros/src/depth_image_projector_nodecore.h‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
#include <ros/ros.h>
88
#include <sensor_msgs/Image.h>
9-
#include <realsense2_camera/Extrinsics.h>
109
#include <image_transport/image_transport.h>
1110
#include <cv_bridge/cv_bridge.h>
11+
#include <tf/transform_listener.h>
1212

1313
#include "depth_image_projector.h"
1414

@@ -20,6 +20,12 @@ class DepthImageProjectorNode
2020
ros::NodeHandle nh_;
2121
image_transport::ImageTransport it_;
2222

23+
// tf
24+
std::string color_frame_id_;
25+
std::string depth_frame_id_;
26+
tf::TransformListener tfListener_;
27+
double tf_wait_duration_;
28+
2329
//Publishers and Subscribers
2430
image_transport::Publisher imagePublisher_;
2531
image_transport::CameraSubscriber colorSubscriber_;
@@ -36,7 +42,7 @@ class DepthImageProjectorNode
3642
void depthCallback(const sensor_msgs::Image::ConstPtr& imageMsg, const sensor_msgs::CameraInfoConstPtr & cameraInfoMsg);
3743
bool depthToColorArrived_ = false;
3844
std::array<float, 16> latestDepthToColor_;
39-
void depthToColorCallback(const realsense2_camera::Extrinsics::ConstPtr& depthToColorMsg);
45+
bool updateDepthToColor();
4046

4147
public:
4248
DepthImageProjectorNode(const ros::NodeHandle& nh, const ros::NodeHandle& nh_private);
@@ -45,4 +51,4 @@ class DepthImageProjectorNode
4551

4652
} //opengl_ros
4753

48-
#endif
54+
#endif

0 commit comments

Comments
 (0)