在 OpenGL 中将深度映射到颜色
Mapping depth to color in OpenGL
我有一个代码将深度值与彩色相机图像映射。
我使用实感 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 %sn", dev->get_name());
printf(" Serial number: %sn", dev->get_serial());
printf(" Firmware version: %sn", 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(" %sn", e.what());
return EXIT_FAILURE;
}
我做了如下,我有x,y,d信息,可以将深度映射到最远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 %sn", ss.str().c_str());
draw_text(0, 0, ss.str().c_str());
glfwSwapBuffers(win);
}
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 将函数类成员映射到类本身内部
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 使用std::函数映射对象方法
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- C++映射分割错误(核心转储)
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 内联映射初始化的动态atexit析构函数崩溃
- 使用"std::unordereded_map"映射到"std::list"对象
- 在 OpenGL 中将深度映射到颜色
- RGB颜色映射为归一化值
- 将OpenCV中的像素颜色映射到预定的颜色列表中
- Kinect v2 将颜色坐标映射到相机空间
- 将颜色映射到间隔
- 实时颜色映射从c++数组
- C++:有没有一种简单的方法可以将十六进制颜色"0xff2a2620""映射到更具可读性的字符串"lovelyBrown"?
- 在C/ c++中访问X11颜色映射的标准方法是什么?
- BGL depth_first_search错误与颜色映射