在OpenGL中将深度映射到颜色
Posted
技术标签:
【中文标题】在OpenGL中将深度映射到颜色【英文标题】:Mapping depth to color in OpenGL 【发布时间】:2018-01-08 04:29:10 【问题描述】:我有一个用彩色相机图像映射深度值的代码。
我使用 Realsense ZR300 来捕获 (x,y,z) 信息。
我现在的困难是我无法将深度映射到颜色信息。
从最近到最远,颜色从一种颜色变为具有不同颜色信息的另一种颜色。
我做了两个子图,喜欢在第一个子图中绘制深度颜色,在第二个子图中绘制正常颜色图像。
我的代码如下。
int main() try
// Create a context object. This object owns the handles to all connected realsense devices.
rs::context ctx;
printf("There are %d connected RealSense devices.\n", ctx.get_device_count());
if(ctx.get_device_count() == 0) return EXIT_FAILURE;
// This tutorial will access only a single device, but it is trivial to extend to multiple devices
rs::device * dev = ctx.get_device(0);
printf("\nUsing device 0, an %s\n", dev->get_name());
printf(" Serial number: %s\n", dev->get_serial());
printf(" Firmware version: %s\n", dev->get_firmware_version());
// Configure all streams to run at VGA resolution at 60 frames per second
dev->enable_stream(rs::stream::depth, 640, 480, rs::format::z16, 60);
dev->enable_stream(rs::stream::color, 640, 480, rs::format::rgb8, 60);
dev->start();
// Open a GLFW window to display our output
glfwInit();
GLFWwindow * win = glfwCreateWindow(1280, 480, "Depth & Color images", nullptr, nullptr);
glfwMakeContextCurrent(win);
while(!glfwWindowShouldClose(win))
// Wait for new frame data
glfwPollEvents();
dev->wait_for_frames();
glClear(GL_COLOR_BUFFER_BIT);
glPixelZoom(1, -1);
// Display depth data by linearly mapping depth between 0 and 2 meters to the red channel
glRasterPos2f(-1, 1);
rs::intrinsics depth_intrin = dev->get_stream_intrinsics(rs::stream::depth);
const uint16_t * depth_image = (const uint16_t *)dev->get_frame_data(rs::stream::depth);
float scale = dev->get_depth_scale();
glBegin(GL_POINTS);
for(int dy=0; dy<depth_intrin.height; ++dy)
for(int dx=0; dx<depth_intrin.width; ++dx)
uint16_t depth_value = depth_image[dy * depth_intrin.width + dx];
float depth_in_meters = depth_value * scale;
if(depth_value == 0) continue;
rs::float2 depth_pixel = (float)dx, (float)dy;
rs::float3 depth_point = depth_intrin.deproject(depth_pixel, depth_in_meters);
//////////////////Here I need to plot Depth to Color////////////////////////
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
glEnd();
//glPixelTransferf(GL_RED_SCALE, 0xFFFF * dev->get_depth_scale() / 2.0f);
//glDrawPixels(640, 480, GL_RED, GL_UNSIGNED_SHORT, dev->get_frame_data(rs::stream::depth));
//glPixelTransferf(GL_RED_SCALE, 1.0f);
// Display color image as RGB triples
glRasterPos2f(0, 1);
glDrawPixels(640, 480, GL_RGB, GL_UNSIGNED_BYTE, dev->get_frame_data(rs::stream::color));
glfwSwapBuffers(win);
return EXIT_SUCCESS;
catch(const rs::error & e)
// Method calls against librealsense objects may throw exceptions of type rs::error
printf("rs::error was thrown when calling %s(%s):\n", e.get_failed_function().c_str(), e.get_failed_args().c_str());
printf(" %s\n", e.what());
return EXIT_FAILURE;
【问题讨论】:
【参考方案1】:我按照以下方式进行操作,我有 x、y、d 信息,并且可以将深度映射绘制为 RED 颜色长达 4 米。
while(!glfwWindowShouldClose(win))
// Wait for new frame data
glfwPollEvents();
dev->wait_for_frames();
//FPS
auto t1 = std::chrono::high_resolution_clock::now();
time += std::chrono::duration<float>(t1-t0).count();
t0 = t1;
++frames;
if(time > 0.5f)
fps = frames / time;
frames = 0;
time = 0;
//
glClear(GL_COLOR_BUFFER_BIT);
glPixelZoom(1, -1);
// Display depth data by linearly mapping depth between 0 and 2 meters to the red channel
glRasterPos2f(-1, 1);
rs::intrinsics depth_intrin = dev->get_stream_intrinsics(rs::stream::depth);
const uint16_t * depth_image = (const uint16_t *)dev->get_frame_data(rs::stream::depth);
float scale = dev->get_depth_scale();
for(int dy=0; dy<depth_intrin.height; ++dy)
for(int dx=0; dx<depth_intrin.width; ++dx)
uint16_t depth_value = depth_image[dy * depth_intrin.width + dx];
float depth_in_meters = depth_value * scale;
glPixelTransferf(GL_RED_SCALE, 0xFFFF * dev->get_depth_scale() / 4.0f);
glDrawPixels(640, 480, GL_RED, GL_UNSIGNED_SHORT, dev->get_frame_data(rs::stream::depth));
glPixelTransferf(GL_RED_SCALE, 1.0f);
// glDrawPixels( 640, 480, GL_RGB, GL_UNSIGNED_INT, data );
// Display color image as RGB triples
glRasterPos2f(0, 1);
glDrawPixels(640, 480, GL_RGB, GL_UNSIGNED_BYTE, dev->get_frame_data(rs::stream::color));
//ss.str(""); ss << fps << " FPS";
//printf("FPS %s\n", ss.str().c_str());
draw_text(0, 0, ss.str().c_str());
glfwSwapBuffers(win);
【讨论】:
以上是关于在OpenGL中将深度映射到颜色的主要内容,如果未能解决你的问题,请参考以下文章
OpenGL 四 - 002OpenGL 图形渲染之颜色混合