从(视频)帧Intel Realsense获取RGB值时的指针异常

问题描述 投票:0回答:1

我正在尝试使用Realsense SDK从帧中获取不同的RGB值。这是用于带RGB的3D深度相机。根据https://github.com/IntelRealSense/librealsense/issues/3364我需要使用

int i = 100, j = 100; // fetch pixel 100,100
rs2::frame rgb = ...
auto ptr = (uint8_t*)rgb.get_data();
auto stride = rgb.as<rs2::video_frame>().stride();
cout << "R=" << ptr[3*(i * stride + j)];
cout << ", G=" << ptr[3*(i * stride + j) + 1];
cout << ", B=" << ptr[3*(i * stride + j) + 2];

在我的代码中,如果要获取像素(x,y)= 1000,1000的值,则会遇到指针异常。使用(x,y)= 100,100,它每次都可以工作...错误:Exception thrown: read access violation. ptr was 0x11103131EB9192A.

enter image description here

我将.h文件中的enable_stream设置为cfg.enable_stream(RS2_STREAM_COLOR, WIDTH_COLOR_FRAME, HEIGTH_COLOR_FRAME, RS2_FORMAT_RGB8, 15);

#define WIDTH_COLOR_FRAME   1920
#define HEIGTH_COLOR_FRAME  1080

这是我的代码。也许与RS2_FORMAT_RGB8有关?

frameset frames = pl.wait_for_frames();
frame color = frames.get_color_frame();

uint8_t* ptr = (uint8_t*)color.get_data();
int stride = color.as<video_frame>().get_stride_in_bytes();

int i = 1000, j = 1000; // fetch pixel 100,100

cout << "R=" << int(ptr[3 * (i * stride + j)]);
cout << ", G=" << int(ptr[3 * (i * stride + j) + 1]);
cout << ", B=" << int(ptr[3 * (i * stride + j) + 2]);
cout << endl;

提前感谢!

c++ pointers rgb intel realsense
1个回答
1
投票

跨度以字节为单位(行的长度以字节为单位),不需要乘以3。

cout << "  R= " << int(ptr[i * stride + (3*j)    ]);
cout << ", G= " << int(ptr[i * stride + (3*j) + 1]);
cout << ", B= " << int(ptr[i * stride + (3*j) + 2]);
© www.soinside.com 2019 - 2024. All rights reserved.