Skip to content

Commit cef8519

Browse files
committed
changed to output correct occupancy grid
1 parent 47f9602 commit cef8519

File tree

6 files changed

+68
-78
lines changed

6 files changed

+68
-78
lines changed

‎opengl_ros/launch/depth_image_projection.launch‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<arg name="depth_height" default="360"/>
66
<arg name="depth_min" default="0.105"/>
77
<arg name="depth_max" default="10"/>
8-
<arg name="depth_hit_threshold" default="0.9"/>
9-
<arg name="depth_unknown_color" default="1.0"/> <!-- set 0~1.0 for 0~255 -->
8+
<arg name="depth_hit_threshold" default="0.95"/>
9+
<arg name="depth_unknown_color" default="0"/> <!-- 0 to 255 -->
1010
<arg name="grid_map_width" default="200"/>
1111
<arg name="grid_map_height" default="200"/>
1212
<arg name="grid_map_resolution" default="0.025"/>
@@ -74,7 +74,7 @@
7474
<param name="geometry_shader" value="$(find opengl_ros_lib)/shader/gs_depth_image_projection.glsl"/>
7575
<param name="fragment_shader" value="$(find opengl_ros_lib)/shader/fs_depth_image_projection.glsl"/>
7676
<param name="vertex_shader_scaling" value="$(find opengl_ros_lib)/shader/vs_passthrough.glsl"/>
77-
<param name="fragment_shader_scaling" value="$(find opengl_ros_lib)/shader/fs_passthrough.glsl"/>
77+
<param name="fragment_shader_scaling" value="$(find opengl_ros_lib)/shader/fs_rgb_to_occupancy_grid.glsl"/>
7878

7979
<remap from="~depth_to_color" to="/d435/extrinsics/depth_to_color"/>
8080
<remap from="color_in" to="/d435/color/image_raw"/>

‎opengl_ros/src/depth_image_projector_nodecore.cpp‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ DepthImageProjectorNode::DepthImageProjectorNode(const ros::NodeHandle& nh, cons
3333
nh_.param<double>("gridMapLayerHeight", gridMapLayerHeight, 1);
3434
nh_.param<double>("gridMapAccumulationWeight", gridMapAccumulationWeight, 1);
3535

36-
double minDepth, maxDepth, depthHitThreshold, unknownDepthColor;
36+
double minDepth, maxDepth, depthHitThreshold;
37+
int unknownDepthColor;
3738
nh_.param<double>("minDepth", minDepth, 0.105);
3839
nh_.param<double>("maxDepth", maxDepth, 10);
3940
nh_.param<double>("depthHitThreshold", depthHitThreshold, 0.95);
40-
nh_.param<double>("unknownDepthColor", unknownDepthColor, 1.0); //255
41+
nh_.param<int>("unknownDepthColor", unknownDepthColor, 255);
4142

4243
std::string vertexShader, geometryShader, fragmentShader, vertexShaderScaling, fragmentShaderScaling;
4344
nh_.param<std::string>("vertex_shader" , vertexShader , "");
@@ -68,7 +69,7 @@ DepthImageProjectorNode::DepthImageProjectorNode(const ros::NodeHandle& nh, cons
6869
//projector_->uniform("svm_coef_b" , svm_coef_b);
6970
//projector_->uniform("svm_intercept", svm_intercept);
7071

71-
output_.create(gridMapHeight, gridMapWidth, CV_8UC3);
72+
output_.create(gridMapHeight, gridMapWidth, CV_8UC1);
7273
}
7374

7475
void DepthImageProjectorNode::colorCallback(const sensor_msgs::Image::ConstPtr& imageMsg, const sensor_msgs::CameraInfoConstPtr & cameraInfoMsg)
@@ -134,7 +135,7 @@ void DepthImageProjectorNode::depthCallback(const sensor_msgs::Image::ConstPtr&
134135
outImage.header.seq = cv_ptr->header.seq;
135136
outImage.header.stamp = cv_ptr->header.stamp;
136137
outImage.header.frame_id = "occupancy_grid"; //TODO set from parameter
137-
outImage.encoding = sensor_msgs::image_encodings::RGB8;
138+
outImage.encoding = sensor_msgs::image_encodings::MONO8;
138139
outImage.image = output_;
139140
imagePublisher_.publish(outImage.toImageMsg());
140141
}

‎opengl_ros_lib/include/depth_image_projector.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class DepthImageProjector final
2121
int depthWidth, int depthHeight,
2222
int gridMapWidth, int gridMapHeight, float gridMapResolution,
2323
float gridMapLayerHeight, float gridMapAccumulationWeight,
24-
float minDepth, float maxDepth, float depthHitThreshold, float unknownDepthColor,
24+
float minDepth, float maxDepth, float depthHitThreshold, int unknownDepthColor,
2525
const std::string& vertexShader, const std::string& geometryShader, const std::string& fragmentShader,
2626
const std::string& vertexScalingShader, const std::string& fragmentScalingShader);
2727
~DepthImageProjector();

‎opengl_ros_lib/shader/fs_depth_image_projection.glsl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ void main(void)
3939
//store accumulated value in R channel
4040
//store 1.0 in G channel if the pixel is on the line
4141
//always store 1.0 in B channel
42-
fragmentColor = vec3(accumulatedProbability, validDepthmeasured, 1.0);
42+
fragmentColor = vec3(accumulatedProbability, validDepthMeasured, 1.0);
4343
}
Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,34 @@
11
#version 450 core
2-
uniform float gridMapLayerHeight;
3-
uniform float gridMapAccumulationWeight;
4-
uniform float depthHitThreshold;
5-
uniform float unknownDepthColor;
6-
uniform vec2 validDepthInMeter;
2+
uniform vec2 resolution;
3+
uniform sampler2D texture;
4+
uniform int unknownDepthColor;
75

8-
out vec3 fragmentColor;
9-
//x ... accumulated probability of object existance
10-
//y ... 1.0 if valid depth is measured in any ray
11-
//z ... 1.0 if any ray achieved at the pixel
12-
13-
//TODO Remove entire color image process as color image is not used anymore
14-
uniform sampler2D colorTexture;
15-
uniform float threshold_l;
16-
uniform float svm_coef_a;
17-
uniform float svm_coef_b;
18-
uniform float svm_intercept;
19-
//TODO Remove entire color image process as color image is not used anymore
20-
21-
in float depth;
22-
in float hitDepth;
6+
in vec4 gl_FragCoord;
7+
out float fragmentColor;
8+
//output 0 - 100 if valid depth is measured
9+
//output `unknownDepthColor` if valid depth is not measured
10+
//output 255 (unknown) if ray does not reach to the pixel
2311

2412
void main(void)
2513
{
26-
//TODO multilayer output based on depth which represents height
27-
28-
//If the height is out of the range, discard the fragment.
29-
if (gl_FragCoord.z < -gridMapLayerHeight / 2 || gridMapLayerHeight / 2 < gl_FragCoord.z)
30-
discard;
31-
32-
float validDepthMeasured = 1.0;
33-
if (validDepthInMeter.y <= hitDepth)
34-
validDepthMeasured = 0.0;
35-
36-
float accumulatedProbability = 0.0;
37-
if (0 < validDepthMeasured && hitDepth * depthHitThreshold < depth)
38-
accumulatedProbability = gridMapAccumulationWeight;
39-
40-
//store accumulated value in R channel
41-
//store 1.0 in G channel if the pixel is on the line
42-
//always store 1.0 in B channel
43-
fragmentColor = vec3(accumulatedProbability, validDepthmeasured, 1.0);
44-
}
14+
float u = gl_FragCoord.x / resolution.x;
15+
float v = gl_FragCoord.y / resolution.y;
16+
17+
vec3 color = texture2D(texture, vec2(u, v)).xyz;
18+
//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
21+
22+
if (color.z < 0.5)
23+
{
24+
fragmentColor = 1.0;
25+
return;
26+
}
27+
28+
if (color.y < 0.5) {
29+
fragmentColor = unknownDepthColor / 255.0;
30+
return;
31+
}
32+
33+
fragmentColor = color.x * 100.0 / 255.0; //rescale to 0~100
34+
}

‎opengl_ros_lib/src/depth_image_projector.cpp‎

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct DepthImageProjector::Impl
4343
const float minDepth_;
4444
const float maxDepth_;
4545
const float depthHitThreshold_;
46-
const float unknownDepthColor_;
46+
const int unknownDepthColor_;
4747

4848
//first path
4949
std::array<cgs::gl::Shader, 3> shaders_;
@@ -73,7 +73,7 @@ struct DepthImageProjector::Impl
7373
int depthWidth, int depthHeight,
7474
int gridMapWidth, int gridMapHeight, float gridMapResolution,
7575
float gridMapLayerHeight, float gridMapAccumulationWeight,
76-
float minDepth, float maxDepth, float depthHitThreshold, float unknownDepthColor,
76+
float minDepth, float maxDepth, float depthHitThreshold, int unknownDepthColor,
7777
const std::string& vertexShader, const std::string& geometryShader, const std::string& fragmentShader,
7878
const std::string& vertexScalingShader, const std::string& fragmentScalingShader);
7979

@@ -92,7 +92,7 @@ DepthImageProjector::Impl::Impl(
9292
int depthWidth, int depthHeight,
9393
int gridMapWidth, int gridMapHeight, float gridMapResolution,
9494
float gridMapLayerHeight, float gridMapAccumulationWeight,
95-
float minDepth, float maxDepth, float depthHitThreshold, float unknownDepthColor,
95+
float minDepth, float maxDepth, float depthHitThreshold, int unknownDepthColor,
9696
const std::string& vertexShader, const std::string& geometryShader, const std::string& fragmentShader,
9797
const std::string& vertexScalingShader, const std::string& fragmentScalingShader) :
9898
context_(true),
@@ -130,7 +130,7 @@ DepthImageProjector::Impl::Impl(
130130
programScaling_(shaderScaling_),
131131
vboScaling_(SQUARE_VERTICIES, GL_STATIC_DRAW),
132132
eboScaling_(SQUARE_INDICIES, GL_STATIC_DRAW),
133-
textureOut_(GL_R8UI, gridMapWidth, gridMapHeight),
133+
textureOut_(GL_R8, gridMapWidth, gridMapHeight),
134134
intermediateSampler_(GL_NEAREST, GL_NEAREST, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE),
135135
fboScaling_(textureOut_)
136136
{
@@ -156,15 +156,15 @@ void DepthImageProjector::Impl::updateProjectionMatrix(
156156

157157
void DepthImageProjector::Impl::project(cv::Mat& dest, const cv::Mat& color, const cv::Mat& depth)
158158
{
159-
if (gridMapWidth_ != dest.cols || gridMapHeight_ != dest.rows || CV_8UC3 != dest.type())
159+
if (gridMapWidth_ != dest.cols || gridMapHeight_ != dest.rows || CV_8UC1 != dest.type())
160160
{
161161
ROS_ERROR_STREAM(
162162
"Destination image resolution does not match." << std::endl <<
163163
"width: texture=" << gridMapWidth_ << ", input=" << dest.cols << std::endl <<
164164
"height: texture=" << gridMapHeight_ << ", input=" << dest.rows << std::endl <<
165-
"channel: texture=" << 3 << ", input=" << dest.channels() << std::endl <<
165+
"channel: texture=" << 1 << ", input=" << dest.channels() << std::endl <<
166166
"elemSize1: texture=" << 1 << ", input=" << dest.elemSize1() << std::endl <<
167-
"type: texture=" << CV_8UC3 << ", input=" << dest.type());
167+
"type: texture=" << CV_8UC1 << ", input=" << dest.type());
168168
return;
169169
}
170170

@@ -239,45 +239,44 @@ void DepthImageProjector::Impl::project(cv::Mat& dest, const cv::Mat& color, con
239239
glDrawArrays(GL_POINTS, 0, verticies_.size());
240240
}
241241

242-
////second path
243-
//{
244-
// //Shaders setup
245-
// program_.use();
246-
// glUniform2f(glGetUniformLocation(program_.get(), "resolution"), width_, height_);
247-
// glUniform1i(glGetUniformLocation(program_.get(), "texture"), 0);
248-
// glUniform1f(glGetUniformLocation(program_.get() , "unknownDepthColor"), unknownDepthColor_);
242+
//second path
243+
{
244+
//Shaders setup
245+
programScaling_.use();
246+
glUniform2f(glGetUniformLocation(programScaling_.get(), "resolution"), gridMapWidth_, gridMapHeight_);
247+
glUniform1i(glGetUniformLocation(programScaling_.get(), "texture"), 0);
248+
glUniform1i(glGetUniformLocation(programScaling_.get(), "unknownDepthColor"), unknownDepthColor_);
249249

250-
// //Verticies setup
251-
// vaoScaling_.mapVariable(vboScaling_, glGetAttribLocation(program_.get(), "position"), 3, GL_FLOAT, 0);
252-
// vaoScaling_.mapVariable(eboScaling_);
250+
//Verticies setup
251+
vaoScaling_.mapVariable(vboScaling_, glGetAttribLocation(programScaling_.get(), "position"), 3, GL_FLOAT, 0);
252+
vaoScaling_.mapVariable(eboScaling_);
253253

254-
// //Disable flags
255-
// glDisable(GL_DEPTH_CLAMP);
256-
// glDisable(GL_BLEND);
254+
//Disable flags
255+
glDisable(GL_DEPTH_CLAMP);
256+
glDisable(GL_BLEND);
257257

258-
// textureIntermediate_.bindToUnit(0);
259-
// samplerIntermediate_.bindToUnit(0);
258+
textureIntermediate_.bindToUnit(0);
259+
intermediateSampler_.bindToUnit(0);
260260

261-
// fboScaling_.bind();
262-
// glViewport(0, 0, width_, height_);
261+
fboScaling_.bind();
262+
glViewport(0, 0, gridMapWidth_, gridMapHeight_);
263263

264-
// vaoScaling_.bind();
265-
// glDrawElements(GL_TRIANGLES, INDICIES.size(), GL_UNSIGNED_INT, nullptr);
266-
//}
264+
vaoScaling_.bind();
265+
glDrawElements(GL_TRIANGLES, SQUARE_INDICIES.size(), GL_UNSIGNED_INT, nullptr);
266+
}
267267

268268
glFinish();
269269

270270
//Read result
271-
//textureOut_.read(GL_RED, GL_UNSIGNED_BYTE, dest.data, dest.rows * dest.cols * dest.channels());
272-
textureIntermediate_.read(GL_RGB, GL_UNSIGNED_BYTE, dest.data, dest.rows * dest.cols * dest.channels());
271+
textureOut_.read(GL_RED, GL_UNSIGNED_BYTE, dest.data, dest.rows * dest.cols * dest.channels());
273272
}
274273

275274
DepthImageProjector::DepthImageProjector(
276275
int colorWidth, int colorHeight,
277276
int depthWidth, int depthHeight,
278277
int gridMapWidth, int gridMapHeight, float gridMapResolution,
279278
float gridMapLayerHeight, float gridMapAccumulationWeight,
280-
float minDepth, float maxDepth, float depthHitThreshold, float unknownDepthColor,
279+
float minDepth, float maxDepth, float depthHitThreshold, int unknownDepthColor,
281280
const std::string& vertexShader, const std::string& geometryShader, const std::string& fragmentShader,
282281
const std::string& vertexScalingShader, const std::string& fragmentScalingShader)
283282
try

0 commit comments

Comments
 (0)