Skip to content

Commit 6132b10

Browse files
committed
implemented temporal accumulation
1 parent 15913f0 commit 6132b10

File tree

7 files changed

+122
-45
lines changed

7 files changed

+122
-45
lines changed

‎opengl_ros/launch/depth_image_projection.launch‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
<arg name="grid_map_height" default="200"/>
1212
<arg name="grid_map_resolution" default="0.1"/>
1313
<arg name="grid_map_layer_height" default="3.0"/>
14-
<arg name="grid_map_accumulation_weight" default="0.004"/> <!-- 0.004 for 1/255 -->
14+
<arg name="grid_map_accumulation_weight" default="0.001"/>
15+
<arg name="grid_map_decay" default="0.5"/>
1516
<arg name="frame_rate" default="30"/>
1617

1718
<node pkg="opengl_ros" type="depth_image_projector"
1819
name="depth_image_projector" output="screen">
1920
<param name="depth_frame_id" value="d435_depth_optical"/>
2021
<param name="map_frame_id" value="base_link_hor"/>
22+
<param name="fixed_frame_id" value="map"/>
2123
<param name="tf_wait_duration" value="0.1"/>
2224

2325
<param name="depth_width" value="$(arg depth_width)"/>
@@ -33,6 +35,7 @@
3335
<param name="grid_map_resolution" value="$(arg grid_map_resolution)"/>
3436
<param name="grid_map_layer_height" value="$(arg grid_map_layer_height)"/>
3537
<param name="grid_map_accumulation_weight" value="$(arg grid_map_accumulation_weight)"/>
38+
<param name="grid_map_decay" value="$(arg grid_map_decay)"/>
3639

3740
<param name="vertex_shader" value="$(find opengl_ros_lib)/shader/vs_depth_image_projection.glsl"/>
3841
<param name="geometry_shader" value="$(find opengl_ros_lib)/shader/gs_depth_image_projection.glsl"/>

‎opengl_ros/src/depth_image_projector_nodecore.cpp‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@ DepthImageProjectorNode::DepthImageProjectorNode(const ros::NodeHandle& nh, cons
1313

1414
nh_.param<std::string>("depth_frame_id", depth_frame_id_, "d435_depth_optical");
1515
nh_.param<std::string>("map_frame_id", map_frame_id_, "base_link_hor");
16+
nh_.param<std::string>("fixed_frame_id", fixed_frame_id_, "map");
1617
nh_.param<double>("tf_wait_duration", tf_wait_duration_, 0.1);
1718

1819
int depthWidth, depthHeight;
1920
nh_.param<int>("depth_width" , depthWidth , 640);
2021
nh_.param<int>("depth_height", depthHeight, 360);
2122

22-
double gridMapLayerHeight, gridMapAccumulationWeight;
23+
double gridMapLayerHeight, gridMapAccumulationWeight, gridMapDecay;
2324
nh_.param<int>("grid_map_width" , gridMapWidth_ , 1000);
2425
nh_.param<int>("grid_map_height", gridMapHeight_, 1000);
2526
nh_.param<double>("grid_map_resolution", gridMapResolution_, 0.01);
2627
nh_.param<double>("grid_map_layer_height", gridMapLayerHeight, 1);
27-
nh_.param<double>("grid_map_accumulation_weight", gridMapAccumulationWeight, 1);
28+
nh_.param<double>("grid_map_accumulation_weight", gridMapAccumulationWeight, 0.001);
29+
nh_.param<double>("grid_map_decay", gridMapDecay, 0.5);
2830

2931
double minDepth, maxDepth, depthHitThreshold;
3032
int unknownDepthColor;
@@ -43,7 +45,7 @@ DepthImageProjectorNode::DepthImageProjectorNode(const ros::NodeHandle& nh, cons
4345
projector_ = std::make_unique<cgs::DepthImageProjector>(
4446
depthWidth, depthHeight,
4547
gridMapWidth_, gridMapHeight_, gridMapResolution_,
46-
gridMapLayerHeight, gridMapAccumulationWeight,
48+
gridMapLayerHeight, gridMapAccumulationWeight, gridMapDecay,
4749
minDepth, maxDepth, depthHitThreshold, unknownDepthColor,
4850
vertexShader, geometryShader, fragmentShader,
4951
vertexShaderScaling, fragmentShaderScaling
@@ -64,8 +66,12 @@ void DepthImageProjectorNode::depthCallback(const sensor_msgs::Image::ConstPtr&
6466
ROS_ERROR_STREAM("cv_bridge exception: " << e.what());
6567
return;
6668
}
69+
70+
//called first time
71+
if (!previousTimestamp_.isValid())
72+
previousTimestamp_ = cameraInfoMsg->header.stamp;
6773

68-
std::array<float, 16> depthToMap;
74+
std::array<float, 16> depthToMap, mapToPreviousMap;
6975
try
7076
{
7177
if (tfListener_.waitForTransform(map_frame_id_, cameraInfoMsg->header.frame_id,
@@ -76,18 +82,32 @@ void DepthImageProjectorNode::depthCallback(const sensor_msgs::Image::ConstPtr&
7682
cameraInfoMsg->header.stamp, transform);
7783
getTransformMatrixArray(transform, depthToMap);
7884
}
85+
86+
if (tfListener_.waitForTransform(map_frame_id_, fixed_frame_id_,
87+
cameraInfoMsg->header.stamp, ros::Duration(tf_wait_duration_)))
88+
{
89+
tf::StampedTransform tramsform;
90+
tfListener_.lookupTransform(
91+
map_frame_id_, previousTimestamp_,
92+
map_frame_id_, cameraInfoMsg->header.stamp,
93+
fixed_frame_id_, tramsform);
94+
getTransformMatrixArray(tramsform, mapToPreviousMap);
95+
}
7996
}
8097
catch (tf::TransformException& ex)
8198
{
8299
ROS_WARN("Transform Exception in depthCallback(). %s", ex.what());
83100
return;
84101
}
85102

103+
//Update previous timestamp
104+
previousTimestamp_ = cameraInfoMsg->header.stamp;
105+
86106
//Update projector parameters with camera info
87107
projector_->updateProjectionMatrix(
88-
{static_cast<float>(cameraInfoMsg->K[0]) , static_cast<float>(cameraInfoMsg->K[4])},
89-
{static_cast<float>(cameraInfoMsg->K[2]) , static_cast<float>(cameraInfoMsg->K[5])},
90-
depthToMap
108+
{static_cast<float>(cameraInfoMsg->K[0]), static_cast<float>(cameraInfoMsg->K[4])},
109+
{static_cast<float>(cameraInfoMsg->K[2]), static_cast<float>(cameraInfoMsg->K[5])},
110+
depthToMap, mapToPreviousMap
91111
);
92112

93113
//Perform projection
@@ -115,7 +135,6 @@ void DepthImageProjectorNode::depthCallback(const sensor_msgs::Image::ConstPtr&
115135
grid.data.reserve(size);
116136
std::copy(data, data + size, std::back_inserter(grid.data));
117137
mapPublisher_.publish(grid);
118-
119138
}
120139

121140
void DepthImageProjectorNode::getTransformMatrixArray(const tf::Transform& transform, std::array<float, 16>& matrix)

‎opengl_ros/src/depth_image_projector_nodecore.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ class DepthImageProjectorNode
2424
//tf
2525
std::string depth_frame_id_;
2626
std::string map_frame_id_;
27+
std::string fixed_frame_id_;
2728
tf::TransformListener tfListener_;
2829
double tf_wait_duration_;
30+
ros::Time previousTimestamp_{0};
2931

3032
//Other parameters
3133
int gridMapWidth_, gridMapHeight_;

‎opengl_ros_lib/include/depth_image_projector.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ class DepthImageProjector final
1919
DepthImageProjector(
2020
int depthWidth, int depthHeight,
2121
int gridMapWidth, int gridMapHeight, float gridMapResolution,
22-
float gridMapLayerHeight, float gridMapAccumulationWeight,
22+
float gridMapLayerHeight, float gridMapAccumulationWeight, float gridMapDecay,
2323
float minDepth, float maxDepth, float depthHitThreshold, int unknownDepthColor,
2424
const std::string& vertexShader, const std::string& geometryShader, const std::string& fragmentShader,
2525
const std::string& vertexScalingShader, const std::string& fragmentScalingShader);
2626
~DepthImageProjector();
2727

2828
void updateProjectionMatrix(
29-
const std::array<float, 2> depthFocalLength, const std::array<float, 2> depthCenter,
30-
const std::array<float, 16> depthToMap);
29+
const std::array<float, 2>& depthFocalLength, const std::array<float, 2>& depthCenter,
30+
const std::array<float, 16>& depthToMap, const std::array<float, 16>& mapToPreviousMap);
3131
void project(cv::Mat& dest, const cv::Mat& depth);
3232

3333
DepthImageProjector(const DepthImageProjector&) = delete;
Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,48 @@
11
#version 450 core
22
uniform vec2 resolution;
33
uniform sampler2D texture;
4+
uniform sampler2D accumulationTexture;
45
uniform int unknownDepthColor;
6+
uniform float gridMapDecay;
7+
uniform float gridMapResolution;
8+
uniform mat4 mapToPreviousMap;
59

610
in vec4 gl_FragCoord;
7-
out float fragmentColor;
11+
12+
layout(location = 0) out float fragmentColor;
813
//output 0 - 100 if valid depth is measured
914
//output `unknownDepthColor` if valid depth is not measured
1015
//output 255 (unknown) if ray does not reach to the pixel
1116

17+
layout(location = 1) out vec3 accumulationOutput;
18+
//output for temporal accumulation
19+
1220
void main(void)
1321
{
14-
float u = gl_FragCoord.x / resolution.x;
15-
float v = gl_FragCoord.y / resolution.y;
22+
//sample previous buffer
23+
vec4 previousFragCoord = mapToPreviousMap * vec4(gl_FragCoord.xy * gridMapResolution, 0, 1) / gridMapResolution;
24+
vec2 previousUv = previousFragCoord.xy / resolution;
25+
vec3 previousColor = texture2D(accumulationTexture, previousUv).xyz;
1626

17-
vec3 color = texture2D(texture, vec2(u, v)).xyz;
27+
//sample current buffer and accumulate
28+
vec2 uv = gl_FragCoord.xy / resolution;
29+
vec3 color = texture2D(texture, uv).xyz + gridMapDecay * previousColor;
1830
//x ... accumulated probability of object existance
19-
//y ... 1.0 if valid depth is measured in any ray
20-
//z ... 1.0 if any ray achieved at the pixel
31+
//y ... non-zero if valid depth is measured in any ray
32+
//z ... non-zero if any ray achieved at the pixel
2133

22-
if (color.z < 0.5)
34+
//output for temporal acccumulation
35+
accumulationOutput = color;
36+
37+
//output occupancy grid
38+
if (color.z == 0)
2339
{
2440
fragmentColor = 1.0;
2541
return;
2642
}
27-
28-
if (color.y < 0.5) {
43+
if (color.y == 0) {
2944
fragmentColor = unknownDepthColor / 255.0;
3045
return;
3146
}
32-
33-
fragmentColor = color.x * 100.0 / 255.0; //rescale to 0~100
47+
fragmentColor = clamp(color.x, 0, 1) * 100.0 / 255.0; //rescale to 0~100
3448
}

0 commit comments

Comments
 (0)